简体   繁体   English

Chrome 扩展程序 - 在新打开的标签页中不起作用

[英]Chrome Extension - Not working in the newly opened tab

I am developing a chrome extension, which is working great in all the scenario except in the new tab.我正在开发一个 chrome 扩展,它在除新选项卡之外的所有场景中都运行良好。

ie, the extension works only when a website is opened for eg.即,该扩展程序仅在为例如打开网站时才有效。 stackoverflow.com. stackoverflow.com。 When I do ctrl+t and click my extension icon, it doesn't works.当我按 ctrl+t 并单击我的扩展程序图标时,它不起作用。

I am doing something wrong?我做错了什么? or Is it the browser behavior?还是浏览器行为?

I have added my code for you reference.我已经添加了我的代码供您参考。

Manifest显现

{
    "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 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背景.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();
        });
    }
});

try add the code below to your manifest.json.尝试将下面的代码添加到您的 manifest.json。

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

blank.html: (create your own version) 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>

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

The problem probably lies in your manifest permissions.问题可能在于您的清单权限。

The new tab page does not use http:// but chrome://newtab , so you may need to add it to your permissions for your extension to work.新标签页不使用http://而是chrome://newtab ,因此您可能需要将其添加到您的权限中才能使您的扩展程序正常工作。

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

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