[英]Why isn't chrome.pageCapture.saveAsMHTML working in my Google Chrome Extension?
在我的 Google Chrome 擴展中, background.js
定義了這個函數:
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");
}
)
}
}
);
}
那么,為什么我會在控制台中看到這個?
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)
順便說一句,控制台日志中的行號排列如下:
chrome.pageCapture.saveAsMHTML(
chrome.tabs.query
submitMTHML();
(在另一個函數中) chrome.pageCapture.saveAsMHTML()
在此處定義。 該函數返回一個 blob,我應該能夠以這種方式將其附加到表單上。 我已經在清單中提供了必要的權限。
錯誤解釋了它。 而不是chrome.pageCapture.saveAsMHTML(tab.id, callback)
,使用chrome.pageCapture.saveAsMHTML({ tabId: tab.id }, callback)
雖然這個問題已經過去很久了,但我最近才嘗試過這個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);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.