最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why isn't chrome.pageCapture.saveAsMHTML working in my Google Chrome Extension? - Stack Overflow

programmeradmin0浏览0评论

In my Google Chrome Extension, background.js defines this function:

function submitMHTML() {
  console.log("entered submitMHTML()");
  chrome.tabs.query(
    {active: true, lastFocusedWindow: true},
    function(array_of_Tabs) {
      if (array_of_Tabs.length > 0) {
        var tab = array_of_Tabs[0];
        console.log("submitMHTML() found the active tab has an ID of " + tab.id);
        chrome.pageCapture.saveAsMHTML(
          tab.id,
          function(mhtml) {
            var xhr = new XMLHttpRequest(), formData = new FormData();  
            formData.append("mhtml", mhtml);
            formData.append("surveyID", localStorage["ID"]);
            xhr.open("POST", "http://localhost:3000/task/mhtml", true);
            xhr.setRequestHeader('Authorization', 'Token token=<redacted>');
            xhr.send(formData);
            console.log("submitMHTML() sent mhtml to server");
          }
        )
      }
    }
  );
}

Why, then, am I seeing this in my console?

entered submitMHTML()
submitMHTML() found the active tab has an ID of 450
extensions::uncaught_exception_handler:8 Error in response to tabs.query: Error: Invocation of form pageCapture.saveAsMHTML(integer, function) doesn't match definition pageCapture.saveAsMHTML(object details, function callback)
    at Object.callback (chrome-extension://nmlggmkodifcibdmpdaohpmhljbkgpdb/background.js:194:28)
    at submitMHTML (chrome-extension://nmlggmkodifcibdmpdaohpmhljbkgpdb/background.js:188:15)
    at submitResult (chrome-extension://nmlggmkodifcibdmpdaohpmhljbkgpdb/background.js:249:5)
    at HTMLButtonElement.<anonymous> (chrome-extension://nmlggmkodifcibdmpdaohpmhljbkgpdb/popup.js:25:78)

BTW, the line numbers in the console log line up like this:

  • 194: chrome.pageCapture.saveAsMHTML(
  • 188: chrome.tabs.query
  • 249: submitMTHML(); (inside another function)

chrome.pageCapture.saveAsMHTML() is defined here. The function returns a blob, which I should be able attach to a form in this way. I've provided the necessary permissions in the manifest.

In my Google Chrome Extension, background.js defines this function:

function submitMHTML() {
  console.log("entered submitMHTML()");
  chrome.tabs.query(
    {active: true, lastFocusedWindow: true},
    function(array_of_Tabs) {
      if (array_of_Tabs.length > 0) {
        var tab = array_of_Tabs[0];
        console.log("submitMHTML() found the active tab has an ID of " + tab.id);
        chrome.pageCapture.saveAsMHTML(
          tab.id,
          function(mhtml) {
            var xhr = new XMLHttpRequest(), formData = new FormData();  
            formData.append("mhtml", mhtml);
            formData.append("surveyID", localStorage["ID"]);
            xhr.open("POST", "http://localhost:3000/task/mhtml", true);
            xhr.setRequestHeader('Authorization', 'Token token=<redacted>');
            xhr.send(formData);
            console.log("submitMHTML() sent mhtml to server");
          }
        )
      }
    }
  );
}

Why, then, am I seeing this in my console?

entered submitMHTML()
submitMHTML() found the active tab has an ID of 450
extensions::uncaught_exception_handler:8 Error in response to tabs.query: Error: Invocation of form pageCapture.saveAsMHTML(integer, function) doesn't match definition pageCapture.saveAsMHTML(object details, function callback)
    at Object.callback (chrome-extension://nmlggmkodifcibdmpdaohpmhljbkgpdb/background.js:194:28)
    at submitMHTML (chrome-extension://nmlggmkodifcibdmpdaohpmhljbkgpdb/background.js:188:15)
    at submitResult (chrome-extension://nmlggmkodifcibdmpdaohpmhljbkgpdb/background.js:249:5)
    at HTMLButtonElement.<anonymous> (chrome-extension://nmlggmkodifcibdmpdaohpmhljbkgpdb/popup.js:25:78)

BTW, the line numbers in the console log line up like this:

  • 194: chrome.pageCapture.saveAsMHTML(
  • 188: chrome.tabs.query
  • 249: submitMTHML(); (inside another function)

chrome.pageCapture.saveAsMHTML() is defined here. The function returns a blob, which I should be able attach to a form in this way. I've provided the necessary permissions in the manifest.

Share Improve this question edited Aug 25, 2015 at 2:48 steven_noble asked Aug 25, 2015 at 1:56 steven_noblesteven_noble 4,21310 gold badges46 silver badges78 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

The error explains it. Instead of chrome.pageCapture.saveAsMHTML(tab.id, callback), use chrome.pageCapture.saveAsMHTML({ tabId: tab.id }, callback)

Although this problem has passed for a long time, I have only recently tried this API.

chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
  chrome.pageCapture.saveAsMHTML({ tabId: tab.id }, ArrayBuffer => {
    const aBlob = new Blob([ArrayBuffer], { type: 'text/plain' });

    const link = document.createElement('a');
    link.href = window.URL.createObjectURL(aBlob);
    link.download = 'aaa.mhtml';
    link.click();
    window.URL.revokeObjectURL(link.href);
  });
});

I found here a good solution for saving the page content solution

chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
  chrome.pageCapture.saveAsMHTML({ tabId: tab.id }, async (blob) => {
    const content = await blob.text();
    const url = "data:application/x-mimearchive;base64," + btoa(content);
    chrome.downloads.download({
        url,
        filename: 'filename.mhtml'
    });
  });
});
发布评论

评论列表(0)

  1. 暂无评论