moustacheman
moustacheman

Reputation: 1464

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.

i.e., the extension works only when a website is opened for eg. stackoverflow.com. When I do ctrl+t and click my extension icon, it doesn't works.

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

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();
        });
    }
});

Upvotes: 2

Views: 4344

Answers (3)

Utkarsh
Utkarsh

Reputation: 11

Inside "permissions" add "chrome://*/*" (for new tabs), then open chrome and goto chrome://flags/#extensions-on-chrome-urls and enable this setting.

Upvotes: 1

Rokin
Rokin

Reputation: 41

try add the code below to your manifest.json.

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

blank.html: (create your own version)

<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>

Upvotes: 1

Valentin Klinghammer
Valentin Klinghammer

Reputation: 1349

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.

Upvotes: 1

Related Questions