gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 03:30:04 25 4
gpt4 key购买 nike

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

使用“普通”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();
})

背景.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]);

关于javascript - Chrome Gmail 扩展程序在某些情况下不起作用。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com