1

I have chrome extension, which add new div to page body. But it doesnt work when i`m trying to run it on newtab page.

Here is my background.js:

chrome.browserAction.onClicked.addListener(function(tab) {  
    chrome.tabs.executeScript(null, {file: "commonAddWidget.js"};
}); 
askona
  • 390
  • 1
  • 5
  • 16

1 Answers1

0

I found that it can be resolved by adding content script, and sending a message to it after clicking on extension icon on browser.

background.js:

chrome.browserAction.onClicked.addListener(function(tab) {  
chrome.tabs.query({'active': true, 'lastFocusedWindow': true}, function (tabs) {
    var url = tabs[0].url;
    if (url.indexOf('chrome://newtab') > -1) {
        chrome.tabs.query({active: true, currentWindow: true},     function(tabs) {
          chrome.tabs.sendMessage(tabs[0].id, {someObject: someValue});
        });
    }
}); }); 

contentScript.js:

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {// this code can modify NewTab page, and will modify only it });
askona
  • 390
  • 1
  • 5
  • 16
  • 1
    Does it really work though? Content scripts are not supposed to be able to access new tab page. What are your extension's permissions? – Xan Mar 22 '16 at 14:06