我通过chrome扩展程序上传文件作为表单数据,我的代码如下所示.这里的问题是文件浏览窗口打开一秒钟然后消失.
该问题仅出现在Mac OS中.
manifest.json的:
"background": {
"scripts": ["jszip.js", "background.js"]
},
Run Code Online (Sandbox Code Playgroud)
background.js:
chrome.runtime.onMessage.addListener(function (msg) {
if (msg.action === 'browse')
{
var myForm=document.createElement("FORM");
var myFile=document.createElement("INPUT");
myFile.type="file";
myFile.id="selectFile";
//myFile.onclick="openDialog()";
myForm.appendChild(myFile);
var myButton=document.createElement("INPUT");
myButton.name="submit";
myButton.type="submit";
myButton.value="Submit";
myForm.appendChild(myButton);
document.body.appendChild(myForm);
}
});
Run Code Online (Sandbox Code Playgroud)
popup.js:
window.onload = function () {
chrome.runtime.sendMessage({
action: 'browse'
});
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的chrome扩展程序开发导入功能,但是这个任务似乎比预期更困难.
其实我的想法是用一个
<input type="file" id="myInput">
Run Code Online (Sandbox Code Playgroud)
要加载文件,然后在该元素上添加更改侦听器:
element.addEventListener('change', function(evt){
read_single_file(evt,tab);
}
Run Code Online (Sandbox Code Playgroud)
现在我面临几个问题:
我找到了另一种解决方案,建议将文件逻辑添加到后台页面中,如果弹出窗口失去焦点,则不会销毁.然后我在后台添加了这个功能:
file_import = function(element, tab) {
element.addEventListener('change', function(evt){
read_single_file(evt,tab);
}, false);
element.click();
console.log("uffa");
}
Run Code Online (Sandbox Code Playgroud)然后我更新了我的popup.js来调用后台方法:
get_current_tab(function(tab){
var BGPage = chrome.extension.getBackgroundPage();
BGPage.file_import(document.getElementById('myInput'), tab);
});
Run Code Online (Sandbox Code Playgroud)
以这种方式,file_import由弹出窗口调用,它将更改侦听器添加到myInput元素,并打开文件打开对话框但是...打开文件对话框弹出消失,以及与之相关的所有内容,然后......同样的问题.
所以我决定尝试在后台页面中创建一个新的输入文件元素,并从中触发click事件,但显然它不起作用.
所以,我被困在这里,没有关于如何解决这个问题的好主意.或者至少我心里很少,但我不知道它们是否有效,或者它们只是解决方法.
总结我的问题是:
我研究这个问题已经有一段时间了,但没有成功。希望有人能对此有所了解!
我工作的地方认证/授权正在被谷歌的以外进行(谷歌的Chrome扩展(浏览器操作)chrome.identity.launchwebauthflow与interactive设置为true)。我们已经为某些用户(但不是全部)成功进行了身份验证/授权流程。以下是有趣的结果:
chrome-extension://pathofextension)。用户点击授权按钮,成功获取授权码,将其兑换为访问令牌,即可继续使用该应用程序。我们认为这是客户端问题,但我们都使用相同的 Chrome 版本。返回身份验证代码时,什么会导致扩展程序的弹出窗口关闭?我们不能让开发者控制台保持打开状态以查看是否出现任何错误,因为当开发者控制台打开时它工作得很好。我们正在使用$.ajaxSetup({ cache:false })以确保为 ajax 请求禁用缓存。
这是 chrome.identity.launchwebauthflow 调用的片段(最初从弹出窗口调用):
chrome.identity.launchWebAuthFlow({url:url,interactive:true},function(response) {
console.log('OAuth Response: '+response);
if (response) {
var authCode = encodeURIComponent(response.substring(response.indexOf('=')+1));
console.log('Auth Code: '+authCode);
userAuthorization(authCode);
} else {
authorizeButton();
}
});
Run Code Online (Sandbox Code Playgroud)
尝试应用后台代码解决方案后编辑的代码:
弹出脚本现在调用后台脚本:
chrome.runtime.sendMessage({type:'authorize',url:url},function(response) {
console.log(chrome.runtime.lastError);
console.log(response);
if (response && response.authCode) {
userAuthorization(response.authCode); …Run Code Online (Sandbox Code Playgroud)