首页 > 解决方案 > Chrome 扩展程序仅适用于“chrome://extensions”

问题描述

我正在做一个 chrome 扩展。如果满足特定条件,我想更改活动选项卡中的 URL(重定向)。这是我的代码:

   function redirect_to(desire_url){
        chrome.tabs.query({active:true,windowType:"normal", currentWindow: true},function(tabs){
                chrome.tabs.update(tabs[0].id,{url:desire_url});
                console.log('redirect to'+desire_url);
        });
}


 (function(){
        redirect_to('http://google.com');

})();

我的 manifest.json 文件

{
        "name":"My Extension",
        "description":"Just a simple extension",
        "version":"1.0",
        "manifest_version":2,
        "background":{
                        "scripts":["background.js"]
        },
        "content_scripts":[
                {
                        "matches":[
                                "<all_urls>"
                        ],
                        "js":["popup.js"]
                }
        ],
        "browser_action":{
                "default_icon":"icon.png",
                "default_popup":"popup.html"
        },
        "permissions":[
                "tabs",
                "activeTab",
                "*://*/"
        ]
}

在“chrome://extensions” URL 中,它工作得很好。但是,在“https://blabla.com”之类的其他 URL 中,它不起作用。我知道这个问题已经有人问过了。但是,该解决方案对我不起作用。谢谢。

标签: javascripthtmlredirectgoogle-chrome-extensionbackground-process

解决方案


推荐阅读