Reputation: 1975
I'm trying to make a Chrome extension which runs a script whenever the user opens a new tab.
I have the following basic code which is supposed to just paint the page red whenever the extension button is clicked.
It seems to work fine when I navigate to an actual website (ex: here on stackoverflow.com, click my extension icon, page becomes red). However, if I just create a brand new tab and click the button, the popup loads but the page never changes color.
manifest.json:
{
"manifest_version": 2,
"name": "ConsoleTap",
"version": "0.1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "menu.html"
},
"permissions": [
"tabs",
"<all_urls>",
"https://ajax.googleapis.com/"
]
}
menu.html:
<!doctype html>
<html>
<head>
<script src="menu.js"></script>
</head>
<body>
hello
</body>
</html>
menu.js:
document.addEventListener('DOMContentLoaded', function() {
chrome.tabs.executeScript(null,{code:"document.body.style.backgroundColor='red'"});
});
Any ideas on why the page won't update the background color on a new tab? I'm guessing the DOMContentLoaded
is never fired or it's listening after the load occurs somehow?
Upvotes: 2
Views: 2917
Reputation: 73566
In Chrome 60 and older it was still possible to inject content scripts into the child frames of the default new tab page because it consists of several frames so the restriction only applies to the top level frame. For the default newtab (not some newtab extension) we can match its frame url (https://www.google.com/_/chrome/newtab*
) to inject a content script which will activate once a message from the popup is received:
manifest.json:
{
"name": "executeScript",
"version": "1.0",
"content_scripts": [{
"matches": ["https://www.google.com/_/chrome/newtab*"],
"js": ["newtabcontent.js"],
"run_at": "document_start"
}],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": [
"activeTab"
],
"manifest_version": 2
}
newtabcontent.js:
chrome.runtime.onMessage.addListener(function(msg) {
if (msg == "activate") {
document.body.style.background = 'red';
document.body.style.backgroundColor = 'red';
}
});
popup.js:
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
if (tabs[0].url == "chrome://newtab/") {
chrome.tabs.sendMessage(tabs[0].id, "activate");
} else {
chrome.tabs.executeScript({
code: "document.body.style.backgroundColor='red'",
allFrames: true
});
}
});
Notes:
chrome.tabs.executeScript
can't be used on newtab page because it instantly fails for the main frame due to unsupported scheme chrome://
and doesn't even proceed to execute on child frames.Upvotes: 5