简体   繁体   English

Chrome 扩展代码 vs 内容脚本 vs 注入脚本

[英]Chrome extension code vs Content scripts vs Injected scripts

I am trying to get my Chrome Extension to run the function init() whenever a new page is loaded, but I am having trouble trying to understand how to do this.我试图让我的 Chrome 扩展程序在加载新页面时运行函数init() ,但我在尝试理解如何执行此操作时遇到了麻烦。 From what I understand, I need to do the following in background.html:据我了解,我需要在 background.html 中执行以下操作:

  1. Use chrome.tabs.onUpdated.addListener() to check when the page is changed使用chrome.tabs.onUpdated.addListener()检查页面何时更改
  2. Use chrome.tabs.executeScript to run a script.使用chrome.tabs.executeScript运行脚本。

This is the code I have:这是我的代码:

//background.html
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    chrome.tabs.executeScript(null, {code:"init();"});
});

//script.js
function init() {
    alert("It works!");
}

I am also wondering if the init() function will have access to my other functions located in other JS files?我还想知道 init() 函数是否可以访问位于其他 JS 文件中的其他函数?

JavaScript code in Chrome extensions can be divided in the following groups: Chrome 扩展程序中的 JavaScript 代码可以分为以下几组:

  • Extension code - Full access to all permitted chrome.* APIs.扩展代码 - 完全访问所有允许的chrome.* API。
    This includes the background page , and all pages which have direct access to it via chrome.extension.getBackgroundPage() , such as the browser pop-ups .这包括背景页面,以及所有可以通过chrome.extension.getBackgroundPage()直接访问它的页面,例如浏览器弹出窗口

  • Content scripts (via the manifest file or chrome.tabs.executeScript ) - Partial access to some of the chrome APIs , full access to the page's DOM ( not to any of the window objects, including frames). 内容脚本(通过清单文件或chrome.tabs.executeScript ) - 部分访问一些chrome API ,完全访问页面的 DOM(不是任何window对象,包括框架)。
    Content scripts run in a scope between the extension and the page.内容脚本在扩展和页面之间的范围内运行。 The global window object of a Content script is distinct from the page/extension's global namespace.内容脚本的全局window对象不同于页面/扩展的全局命名空间。

  • Injected scripts (via this method in a Content script) - Full access to all properties in the page.注入脚本(通过内容脚本中的此方法) - 对页面中所有属性的完全访问权限。 No access to any of the chrome.* APIs.无法访问任何chrome.* API。
    Injected scripts behave as if they were included by the page itself, and are not connected to the extension in any way.注入的脚本表现得好像它们被页面本身包含一样,并且没有以任何方式连接到扩展。 See this post to learn more information on the various injection methods.请参阅此帖子以了解有关各种注射方法的更多信息。

To send a message from the injected script to the content script, events have to be used.要将消息从注入的脚本发送到内容脚本,必须使用事件。 See this answer for an example.有关示例,请参阅此答案 Note: Message transported within an extension from one context to another are automatically (JSON)-serialised and parsed .注意:在扩展中从一个上下文传输到另一个上下文的消息会自动 (JSON) 序列化和解析


In your case, the code in the background page ( chrome.tabs.onUpdated ) is likely called before the content script script.js is evaluated.在您的情况下,可能会在评估内容脚本script.js之前调用后台页面 ( chrome.tabs.onUpdated ) 中的代码。 So, you'll get a ReferenceError , because init is not .所以,你会得到一个ReferenceError ,因为init不是。

Also, when you use chrome.tabs.onUpdated , make sure that you test whether the page is fully loaded, because the event fires twice: Before load, and on finish:此外,当您使用chrome.tabs.onUpdated ,请确保测试页面是否已完全加载,因为该事件会触发两次:加载前和完成时:

//background.html
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
    if (changeInfo.status == 'complete') {
        // Execute some script when the page is fully (DOM) ready
        chrome.tabs.executeScript(null, {code:"init();"});
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM