Google Chrome document.title extensions not working

here is the code in manifest.json

{ "name": "Page Title changer", "version": "1.0", "description": "Change the <title></title> of a page", "browser_action": { "default_icon": "icon.png" }, "content_scripts": [ { "matches": ["http://*/*"], "js": ["changetitle.js"] } ] } 

and here is the code from changetitle.js file

 chrome.browserAction.onClicked.addListener(function() { document.title = 'new page title'; }); 

I donโ€™t understand why it doesnโ€™t work, I checked the documents in Google code while writing this extension.

+4
source share
3 answers

As described in the documentation , you cannot use the chrome.* API in content scripts, with the exception of some chrome.extension.* Methods.

However, this does not limit you, as you can use messaging to call your script content from your background page. For instance:

background.html

 <script type="application/javascript"> chrome.browserAction.onClicked.addListener(function() { chrome.tabs.getSelected(function (tab) { chrome.tabs.sendRequest(tab.id, {title: 'new page title'}, function (response) {}); }); }); </script> 

changetitle.js

 chrome.extension.onRequest.addListener(function (request, sender, sendResponse) { document.title = request.title; }); 

To use this technique, you will of course need tabs permission.

+3
source

This will work in any URI scheme

manifest.json

 { "name": "Page Title changer", "version": "1.0", "description": "Change the <title></title> of a page", "browser_action": { "default_icon": "icon.png" }, "background_page": "background.html", "permissions": [ "tabs" ] } 

background.html

 chrome.browserAction.onClicked.addListener(function () { chrome.tabs.executeScript(null, { file: "changetitle.js" }); }); 

changetitle.js

 document.title = 'new page title'; 
+2
source

Try the following:

 chrome.browserAction.onClicked.addListener(function () { chrome.tabs.executeScript(null, { code: "document.title = 'new page title'" }); }); 
+1
source

All Articles