chrome.tabs.executeScript 上出现未知错误

发布于 2024-12-26 02:17:39 字数 1776 浏览 3 评论 0原文

我需要在外部页面上运行脚本。

我正在尝试使用 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

Dropbox OAuth

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,因此尝试删除对 j​​Query

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

Dropbox OAuth

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2025-01-02 02:17:39

我不确定您是否想要将脚本注入打开窗口的选项卡或刚刚打开的新选项卡。无论如何,我努力回答以下两个问题。首先,请注意,您不应尝试将脚本加载到窗口对象中。该窗口可以包含多个选项卡,每个选项卡都有自己的脚本环境。将脚本插入新打开的窗口的选项卡中。

结果 1: 将脚本注入到刚刚打开的选项卡中

下面的代码应将脚本加载到窗口的所有选项卡中,因为 win.tabs 提供了选项卡数组。对于新打开的窗口,通常只有一个选项卡。

chrome.windows.create({url: "https://google.com", type:"popup"}, function(win){
    chrome.tabs.executeScript(win.id.tabs, 
                              { code: "console.log('new tab context');" });
});

结果 2: 将脚本注入打开窗口的选项卡中

记录打开新窗口的选项卡的 id,然后在回调中注入脚本

var openingTabId = ASSIGN_THE_TAB_ID;
chrome.windows.create({url: "https://google.com", type:"popup"}, function(win){
    chrome.tabs.executeScript(openingTabId, 
                              { code: "console.log('opening tab context');" });
});

请注意,我使用了 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.

chrome.windows.create({url: "https://google.com", type:"popup"}, function(win){
    chrome.tabs.executeScript(win.id.tabs, 
                              { code: "console.log('new tab context');" });
});

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

var openingTabId = ASSIGN_THE_TAB_ID;
chrome.windows.create({url: "https://google.com", type:"popup"}, function(win){
    chrome.tabs.executeScript(openingTabId, 
                              { code: "console.log('opening tab context');" });
});

Notice that I used the code object to pass code without using a script file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文