简体   繁体   English

chrome扩展 - 页面重定向后的chrome.tabs.executeScript

[英]chrome extension - chrome.tabs.executeScript after page redirect

I'm trying to make a Google chrome extension that injects a script upon clicking the extension icon. 我正在尝试制作一个Google Chrome扩展程序,在单击扩展程序图标时会注入​​脚本。 However, i want the same script to be injected whenever I load/go to another page, without having to click on the extension icon again. 但是,我希望每当我加载/转到另一个页面时都会注入相同的脚本,而不必再次单击扩展图标。

background.js background.js

chrome.browserAction.onClicked.addListener(function (tab) {
 if (tab.url.startsWith("https://")){ 
    chrome.tabs.executeScript(tab.id, {
        "file": "js/contentscript.js"
    }, function () {
        console.log("Script Executed .. "); // Notification on Completion
    });
 }
 else{
    alert('Error');
 }
});

manifest.json i want to execute the script on browserAction so adding the script on content_scripts is not an option i would like to pursue manifest.json 我想在browserAction上执行脚本,所以在content_scripts上添加脚本不是我想追求的选项

"permissions": ["tabs", "windows", "notifications", "activeTab"],
"version": "2.0",
"browser_action": 
{
    "name": "Click to activate extension",
    "default_icon": "images/icon.png",
},
"background":
{
    "scripts":["js/background.js"]
},
"icons":
{
    "16": "images/icon16.png",
    "48": "images/icon48.png",
    "128": "images/icon128.png"
},
"content_scripts": 
[{
    "js": ["js/jquery-2.1.4.min.js"],
    "matches": [ "https://*/*" ],
    "run_at": "document_end"
}]

contenscript.js - only a sample script, but gets my point across contenscript.js - 只是一个示例脚本,但得到了我的观点

alert("here");
setTimeout(function(){window.open("www.google.com");});

PS This is my first time asking a question in stackoverflow, please be gentle with me. PS这是我第一次在stackoverflow中提问,请对我温柔。 :D :d

You could listen to webNavigation.onCompleted event, which fires when a document, including the resources it refers to, is completely loaded and initialized. 您可以收听webNavigation.onCompleted事件,该事件在文档(包括其引用的资源)被完全加载和初始化时触发。

Compared with tabs.onUpdated event, you could use event filters to restrict your event notifications. tabs.onUpdated事件相比,您可以使用事件过滤器来限制事件通知。 For this part, you could check my answer in this post Running an extension in background on page load/refresh for more details. 对于此部分,您可以在此文章中检查我的答案在页面加载/刷新在后台运行扩展以获取更多详细信息。

manifest.json 的manifest.json

{
    "name": "36735306",
    "version": "1.0",
    "description": "Your description here",
    "manifest_version": 2,
    "background": {
        "scripts": ["background.js"]
    },
    "web_accessible_resources": ["content.js"],
    "permissions": [
        "webNavigation",
        "<all_urls>"
    ]
}

background.js background.js

chrome.webNavigation.onCompleted.addListener(function(details) {
    if(details.frameId === 0) {
        chrome.tabs.executeScript(details.tabId, {"file": "content.js"}); 
    }
});

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

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