Chrome扩展程序-在新打开的标签页中无法工作

dgenwo3n  于 12个月前  发布在  Go
关注(0)|答案(3)|浏览(137)

我正在开发一个Chrome扩展,它在所有场景中都工作得很好,除了在新标签中。
即,扩展仅在网站被打开用于例如stackoverflow.com.当我按ctrl+t并单击扩展图标时,它不起作用。
我做错了什么吗?还是浏览器行为?
我已经添加了我的代码供您参考。

清单

{
    "manifest_version": 2,

    "background": {
        "scripts": ["scripts/background.js"],
        "persistent": false
    },

    "content_scripts":[{
        "matches" : ["<all_urls>"],
        "js": ["scripts/jquery-2.1.0-min.js", "scripts/init.js"],
        "run_at": "document_end"
    }],

    "permissions": [
        "storage", "activeTab", "http://*/*", "https://*/*"
    ],

    "browser_action": {
        "default_icon": "images/plugin-icon-24.png"
    },

    "web_accessible_resources": [
        "*.html",
        "images/*.gif",
        "images/*.png"
    ]
}

init.js

chrome.storage.sync.get('logged_in', function(status){
    if(status.logged_in){
        chrome.runtime.sendMessage('LOGGED_IN');
    } else {
        chrome.runtime.sendMessage('NOT_LOGGED_IN');
    }
});

background.js

var add_resource = function(){
    chrome.tabs.executeScript({
        file: 'scripts/plugin.js'
    });
    chrome.tabs.insertCSS({
        file: 'styles/plugin.css'
    });
};

chrome.runtime.onMessage.addListener(function(message){ 
    alert(message);
    /*This alerts comes even in the newly opened tab. 
    But the script is not getting executed.*/

    if(message == 'LOGGED_IN'){
        add_resource();
    } else {
        chrome.browserAction.onClicked.addListener(function(tab){
            add_resource();
        });
    }
});
klr1opcd

klr1opcd1#

尝试将下面的代码添加到您的manifest.json。

"chrome_url_overrides": {
    "newtab": "blank.html"
}

blank.html:(创建您自己的版本)

<html>
 <head>
  <title>Blank New Tab</title>
  <style>
  div {
    color: #cccccc;
    vertical-align: 50%;
    text-align: center;
    font-family: sans-serif;
    font-size: 300%;
  }
  </style>
 </head>
 <body>
  <div style="height:40%"></div>
  <div>Blank New Tab&trade;</div>
 </body>
</html>
kmpatx3s

kmpatx3s2#

在“权限”中添加**“Chrome:///(用于新标签页),然后打开chrome并后藤chrome://flags/#extensions-on-chrome-urls**并启用此设置。

lsmepo6l

lsmepo6l3#

问题可能出在您的清单权限上。
新的标签页不使用http://,而是使用chrome://newtab,因此您可能需要将其添加到您的权限中,以便扩展工作。

相关问题