chrome.tabs.executeScript 上出现未知错误
我需要在外部页面上运行脚本。
我正在尝试使用 Dropbox API(仅限 JavaScript 和 HTML)。 我正在使用 JsOAuth 来处理 OAuth。
代码
此应用程序是 Google Chrome 的一对类型打包应用。
授权
//Request token
chrome.windows.create({url: url, type:"popup"}, function(win){
chrome.tabs.executeScript(win.id, { file: "contentScript.js" }, function(){
console.log("Callback executeScript!!");
});
});
url = 请求令牌 URL
contentScript .js
$(document).ready(function() {
console.log("Script injected!!!");
})
控制台出错
tabs.executeScript 期间出错:未知错误。 chromeHidden.handleResponseextensions/schema_ generated_bindings.js:94 openAuthoriseWindowscripts.js:297 g.fetchRequestTokenjsOAuth-1.3.3.min.js:1 g.init.request.q.onreadystatechange
尝试
由于外部页面无法使用 jQuery,因此尝试删除对 jQuery
contentScript.js
console.log("Script injected!!!");
控制台中的错误
Error during tabs.executeScript: Unknown error.
chromeHidden.handleResponse
的引用另一种尝试是通过代码注入脚本:
//Request token
chrome.windows.create({url: url, type:"popup"}, function(win){
chrome.tabs.executeScript(win.id, { code: "console.log('Script injected!!')" }, function(){
console.log("Callback executeScript!!");
});
});
但是错误和上面一样
I need to run a script on an external page.
I'm trying to consume the Dropbox API (JavaScript and HTML only).
I'm using JsOAuth to work with OAuth.
Code
This application is a pair of type Packaged Apps to Google Chrome.
Authorise
//Request token
chrome.windows.create({url: url, type:"popup"}, function(win){
chrome.tabs.executeScript(win.id, { file: "contentScript.js" }, function(){
console.log("Callback executeScript!!");
});
});
url = Request token url
contentScript.js
$(document).ready(function() {
console.log("Script injected!!!");
})
Error in console
Error during tabs.executeScript: Unknown error.
chromeHidden.handleResponseextensions/schema_generated_bindings.js:94
openAuthoriseWindowscripts.js:297
g.fetchRequestTokenjsOAuth-1.3.3.min.js:1
g.init.request.q.onreadystatechange
Attempts
As the external page can not jQuery, an effort was to remove the reference to jQuery
contentScript.js
console.log("Script injected!!!");
Error in console
Error during tabs.executeScript: Unknown error.
chromeHidden.handleResponse
Another attempt was to inject the script via code:
//Request token
chrome.windows.create({url: url, type:"popup"}, function(win){
chrome.tabs.executeScript(win.id, { code: "console.log('Script injected!!')" }, function(){
console.log("Callback executeScript!!");
});
});
But the error was the same as above
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您是否想要将脚本注入打开窗口的选项卡或刚刚打开的新选项卡。无论如何,我努力回答以下两个问题。首先,请注意,您不应尝试将脚本加载到窗口对象中。该窗口可以包含多个选项卡,每个选项卡都有自己的脚本环境。将脚本插入新打开的窗口的选项卡中。
结果 1: 将脚本注入到刚刚打开的选项卡中
下面的代码应将脚本加载到窗口的所有选项卡中,因为
win.tabs
提供了选项卡数组。对于新打开的窗口,通常只有一个选项卡。结果 2: 将脚本注入打开窗口的选项卡中
记录打开新窗口的选项卡的 id,然后在回调中注入脚本
请注意,我使用了
code
对象在不使用脚本文件的情况下传递代码。I'm not sure whether you are wanting to inject the script into the tab opening the window, or the new tab you just opened. In any event, I made an effort to answer both questions below. First, please note that you should not attempt to load the script into the window object. The window can contain multiple tabs, and each tab has their own scripting environment. Inject your script into a tab of the newly opened window.
Outcome 1: Injecting the Script into the tab you Just Opened
The code below should load the script into all of the tabs of a window since
win.tabs
gives an array of tabs. For a newly opened window, there is usually only one tab.Outcome 2: Injecting the Script into the tab opening the window
Record the id of the tab opening the new window, then inject the script on the callback
Notice that I used the
code
object to pass code without using a script file.