且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在页面加载时在chrome扩展中运行js脚本

更新时间:2023-12-05 18:38:22

所以我认为简单的解决方案就是创建一个内容脚本并在那里等到页面加载:

So I think that the simple solution is just to create a content script and there to wait until the page is load :

manifest.json

manifest.json

{

 "manifest_version": 2,
 "name": "Getting started example",
"description": "This extension shows a Google Image search result for the   current page",
"version": "1.0",
"content_scripts": [
 {
    //Set your address you want the extension will work in mataches!!! 
 "matches": ["http://mail.google.com/*", "https://mail.google.com/*"],
  "js": ["content.js"],
  "run_at": "document_end"
 }
],  
"permissions": [
              "activeTab",
"https://ajax.googleapis.com/"
             ],
 "browser_action": {
 "default_icon": "icon.png",
 "default_popup": "popup.html"
 }

}

content.js

content.js

window.onload=function(){
     console.log("page load!");
}

您还可以使用background.js和内容页面之间的消息传递检查选项卡是否已加载,但对于您的情况,我认为这已经足够了。

You could also use with message passing between background.js and your content page and to check that tab is loaded but for your case I think it's enough.