Chrome 扩展程序 - 在新打开的选项卡中不起作用

问题描述 投票:0回答:4

我正在开发一个 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');
    }
});

背景.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();
        });
    }
});
javascript google-chrome google-chrome-extension google-chrome-app
4个回答
1
投票

尝试将以下代码添加到您的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>

1
投票

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


0
投票

问题可能出在你的清单权限上。

新标签页不使用

http://
而是使用
chrome://newtab
,因此您可能需要将其添加到您的权限中才能使您的扩展正常工作。


0
投票

任何人在此线程中有任何更新。我也有同样的问题

© www.soinside.com 2019 - 2024. All rights reserved.