Chrome Gmail 扩展在某些情况下不起作用。为什么?
Chrome Gmail extension does not work in certain circumstances. Why?
我正在开发 Chrome 扩展。当在 Chrome 中打开本地 PDF 文件时,通过单击扩展图标,它会打开一个带有 gmail.com 的新选项卡(和撰写视图)并将打开的文件附加到新电子邮件中。
使用 "normal" Gmail 帐户,它工作正常。问题是,如果我使用别名发件人(例如个人和企业)的帐户,附加并不总是发生。有时它会附加文件,有时不会。如果失败,控制台中会显示以下错误消息:www.inboxsdk.com/build/platform-implementation.js:18 错误记录:错误:找不到 dropzone
代码有什么问题?
app.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();
})
background.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([文件]) 使用这个:
composeView.getBodyElement().focus();
setTimeout(function() {composeView.attachFiles([file])}, 1000);
或者,或者:
composeView.attachInlineFiles([file]);
我正在开发 Chrome 扩展。当在 Chrome 中打开本地 PDF 文件时,通过单击扩展图标,它会打开一个带有 gmail.com 的新选项卡(和撰写视图)并将打开的文件附加到新电子邮件中。
使用 "normal" Gmail 帐户,它工作正常。问题是,如果我使用别名发件人(例如个人和企业)的帐户,附加并不总是发生。有时它会附加文件,有时不会。如果失败,控制台中会显示以下错误消息:www.inboxsdk.com/build/platform-implementation.js:18 错误记录:错误:找不到 dropzone
代码有什么问题?
app.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();
})
background.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([文件]) 使用这个:
composeView.getBodyElement().focus();
setTimeout(function() {composeView.attachFiles([file])}, 1000);
或者,或者:
composeView.attachInlineFiles([file]);