Javascript Chrome Gmail扩展在某些情况下不起作用。为什么?
我正在做一个Chrome扩展。当本地PDF文件在Chrome中打开时,通过单击扩展名图标,它会打开一个带有gmail.com的新选项卡(和撰写视图),并将打开的文件附加到新的电子邮件中 有了一个“普通”的Gmail帐户,它就可以正常工作了。问题是,如果我使用别名发送者的帐户(例如个人和企业),附加并不总是发生。有时它附加文件,有时不附加。如果失败,控制台中会出现以下错误消息:www.inboxsdk.com/build/platform implementation.js:18错误记录:错误:找不到dropzone 代码有什么问题 app.jsJavascript Chrome Gmail扩展在某些情况下不起作用。为什么?,javascript,google-chrome-extension,inbox,Javascript,Google Chrome Extension,Inbox,我正在做一个Chrome扩展。当本地PDF文件在Chrome中打开时,通过单击扩展名图标,它会打开一个带有gmail.com的新选项卡(和撰写视图),并将打开的文件附加到新的电子邮件中 有了一个“普通”的Gmail帐户,它就可以正常工作了。问题是,如果我使用别名发送者的帐户(例如个人和企业),附加并不总是发生。有时它附加文件,有时不附加。如果失败,控制台中会出现以下错误消息:www.inboxsdk.com/build/platform implementation.js:18错误记录:错误:找
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();
})
var内容={
init(){
//这是gmail页面
加载(2,'sdk\添加附件\ df7347fce8')。然后((sdk)=>{
chrome.runtime.sendMessage({
消息:“getFile”,
},(回应)=>{
如果(response.file!=“未找到文件”){
sdk.Compose.registerComposeViewHandler((composeView)=>{
chrome.runtime.sendMessage({
消息:“getFile”,
},(回应)=>{
console.log(“上传文件”,response.file);
如果(response.file!=“未找到文件”){
var fileData=content.base64ToArrayBuffer(response.file);
var file=新文件([fileData],response.fileName{
类型:response.fileType
});
composeView.attachFiles([file]);
chrome.runtime.sendMessage({
消息:“删除文件”,
});
}
});
});
sdk.Compose.openNewComposeView()。然后((composeView)=>{
log(“composeView is”,composeView);
});
}否则{
log(“没有要添加的文件”);
}
});
});
},
base64到ArrayBuffer(base64){
var binaryString=window.atob(base64);
var binaryLen=binaryString.length;
var bytes=新的Uint8Array(二进制数);
对于(var i=0;i{
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;
}
});
chrome.browserAction.onClicked.addListener(函数(选项卡){
if(tab.url.indexOf(“文件:/”)!=-1){
//这是文件页
setItem('FileUrl',tab.url);
log('Saved file to attached',tab.url);
打开窗户https://mail.google.com');
}否则{
警报(“请打开url以file://开头的选项卡,然后重试。”);
}
});
chrome.runtime.onMessage.addListener((请求、发送方、发送响应)=>{
if(request.message==“getFile”){
var request=new XMLHttpRequest();
var fileUrl=localStorage.getItem('fileUrl');
if(fileUrl!=“未定义”&&fileUrl!=null){
var fileName=fileUrl.split('/').pop();
打开('GET',fileUrl,true);
request.responseType='blob';
request.onload=函数(){
var reader=new FileReader();
reader.readAsDataURL(request.response);
reader.onload=函数(e){
log('DataURL:',e.target.result);
constbytestring=atob(e.target.result.split('base64')[1]);
const ab=新阵列缓冲区(byteString.length);
常数ia=新的UINT8阵列(ab);
for(设i=0;i
不要使用composeView.attachFiles([file]),请使用以下命令:
composeView.getBodyElement().focus();
setTimeout(function() {composeView.attachFiles([file])}, 1000);
或者,或者:
composeView.attachInlineFiles([file]);
不要使用composeView.attachFiles([file]),而是使用以下命令:
composeView.getBodyElement().focus();
setTimeout(function() {composeView.attachFiles([file])}, 1000);
或者,或者:
composeView.attachInlineFiles([file]);
看起来很有效,非常感谢!你能解释一下为什么它能解决这个问题吗?在我发现这个问题之前,我注意到,在一个新的对话框中,输入焦点位于“收件人”或“主题”字段,而不是主区域。当我手动聚焦主区域并执行脚本时,一切正常。我猜当这个区域没有被关注的时候,dropzone还没有被使用/创建,所以这个小黑客只是触发了一些代码来创建/初始化它。看起来正在工作,非常感谢!你能解释一下为什么它能解决这个问题吗?在我发现这个问题之前,我注意到,在一个新的对话框中,输入焦点位于“收件人”或“主题”字段,而不是主区域。当我手动聚焦主区域并执行脚本时,一切正常。我猜当这个区域没有被关注时,dropzone还没有被使用/创建,所以这个小黑客只是触发一些代码来创建/初始化它。