Change the URL in chrome after the page has loaded -
i've written following script change url of tab in chrome, can't figure out how automatically run on every page.
var nytimes = /.*nytimes\.com.*/; var patt = /(&gwh=).*$/; function updateurl(tab){ if(tab.url.match(nytimes)) { var newurl = tab.url.replace(patt,""); chrome.tabs.update(tab.id, {url: newurl}); } } chrome.tabs.onupdated.addlistener(function(tab) {updateurl(tab);});
i put background page, isn't working. need put code somewhere else run?
i suggest read content scripts. you're looking need understand have limited access chrome.* api, you'll have use message passing in order use current functionality. however, using content scripts can make simpler using 1 of proposed solutions.
solution 1
assuming want send redirect same url every time, can configure extension run content script on ny times site. example;
content script: content.js
location = 'http://example.com';
solution 2
however, if redirect url can vary many want abstract logic in background page. example;
content script: content.js
// or can pass more specific section of url (e.g. `location.pathname`) chrome.extension.sendrequest({href: location.href}, function(data) { location = data.url; });
background page: background.js
chrome.extension.onrequest.addlistener(function(request, sender, sendresponse) { sendresponse({ url: geturl(request.href) // todo: `geturl` method containing logic... }); });
important!
regardless of approach go need request permission run content script on target site in manifest file.
manifest: manifest.json
{ ... "content_scripts": [ { "js": ["content.js"], "matches": ["*://*.nytimes.com/*"], "run_at": "document_start" } ], ... }
Comments
Post a Comment