【问题标题】:Chrome Extension - Not working in the newly opened tabChrome 扩展程序 - 在新打开的选项卡中不起作用
【发布时间】:2014-03-04 13:42:15
【问题描述】:

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

【问题讨论】:

    标签: javascript google-chrome google-chrome-extension google-chrome-app


    【解决方案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>
    

    【讨论】:

    • 这无助于通过 background.js 注入脚本,我认为,我们不能通过浏览器操作覆盖新标签页。非常感谢您的建议,它真的很有帮助。
    • stackoverflow.com/questions/4996194/… '请记住,内容脚本不会注入任何chrome:// 或扩展库页面。'
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      问题可能在于您的清单权限。

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

      【讨论】:

      • 我试过了,上面写着'Permission 'chrome://newtab' is unknown or URL pattern is malformed.' 另外,我尝试了所有可能的方式,比如'*://*', 'chrome://*', 'chrome://*/', 等等。没有运气!
      • 同样的警告Permission '*' is unknown or URL pattern is malformed.!
      猜你喜欢
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 2016-11-04
      • 1970-01-01
      • 2011-01-25
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多