Chrome Gmail扩展程序在某些情况下不起作用.为什么?



我正在开发Chrome扩展程序。在Chrome中打开本地PDF文件时,通过单击扩展图标,它会打开一个带有 gmail.com(和撰写视图(的新标签页,并将打开的文件附加到新电子邮件中。

使用"普通"Gmail帐户,它可以正常工作。问题是,如果我使用带有别名发件人(例如个人和企业(的帐户,则附加并不总是发生。有时它会附加文件,有时不会。如果失败,控制台中会出现以下错误消息:www.inboxsdk.com/build/platform-implementation.js:18 记录错误:错误:无法找到拖放区

代码有什么问题?

应用.js

var content = {
init() {
//this is gmail page
InboxSDK.load(2, 'sdk_Add-Attachment_df7347fce8').then((sdk) => {
chrome.runtime.sendMessage({
message: "getFile",
}, (response) => {
if (response.file != 'No File Found') {
sdk.Compose.registerComposeViewHandler((composeView) => {
chrome.runtime.sendMessage({
message: "getFile",
}, (response) => {
console.log("uploaded file", response.file);
if (response.file != 'No File Found') {
var fileData = content.base64ToArrayBuffer(response.file);
var file = new File([fileData], response.fileName, {
type: response.fileType
});
composeView.attachFiles([file]);
chrome.runtime.sendMessage({
message: "deleteFile",
});
}
});
});
sdk.Compose.openNewComposeView().then((composeView) => {
console.log("composeView is", composeView);
});
} else {
console.log("No file to add");
}
});
});
},
base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
};
$(document).ready(() => {
content.init();
})

背景.js:

chrome.browserAction.onClicked.addListener(function(tab) {
if (tab.url.indexOf("file://") != -1) {
//this is file page
localStorage.setItem('FileUrl', tab.url);
console.log('Saved file to be attached', tab.url);
window.open('https://mail.google.com');
} else {
alert("Please open tab with url beginning with file:// and try again.");
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message == "getFile") {
var request = new XMLHttpRequest();
var fileUrl = localStorage.getItem('FileUrl');
if (fileUrl != 'undefined' && fileUrl != null) {
var fileName = fileUrl.split('/').pop();
request.open('GET', fileUrl, true);
request.responseType = 'blob';
request.onload = function() {
var reader = new FileReader();
reader.readAsDataURL(request.response);
reader.onload = function(e) {
console.log('DataURL:', e.target.result);
const byteString = atob(e.target.result.split('base64,')[1]);
const ab = new ArrayBuffer(byteString.length);
const ia = new Uint8Array(ab);
for (let i = 0; i < byteString.length; i += 1) {
ia[i] = byteString.charCodeAt(i);
}
const newBlob = new Blob([ab], {
type: 'application/pdf'
});
sendResponse({
file: e.target.result.split('base64,')[1],
fileName: fileName,
fileType: 'application/pdf'
});
};
};
request.send();
} else {
sendResponse({
file: 'No File Found'
});
}
return true;
} else if (request.message == "deleteFile") {
localStorage.removeItem('FileUrl');
return true;
}
});

而不是 composeView.attachFiles([file]( 使用以下命令:

composeView.getBodyElement().focus();
setTimeout(function() {composeView.attachFiles([file])}, 1000);

或者,或者:

composeView.attachInlineFiles([file]);

相关内容

  • 没有找到相关文章

最新更新