I have to update a url using chrome extension.
Here is my manifest file
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "cwins.png"
},
"permissions": [
"http://www.google.co.in/",
"http://www.google.co.in/#hl=en&biw=1920&bih=955&q=anushka&aq=f&aqi=g10&aql=&oq=&fp=5f42a1c1d2fc35ec"
],
"content_scripts": [
{
"matches": ["http://www.google.co.in/"],
"js": ["jquery.js", "myscript.js"]
}
]
}
here is my content_scripts (myscripts.js)
alert('hi');
chrome.tabs.getSelected({}, function(tab) {
chrome.tabs.update(tab.id, {
url: 'http://www.google.co.in/#hl=en&biw=1920&bih=955&q=anushka&aq=f&aqi=g10&aql=&oq=&fp=5f42a1c1d2fc35ec'
});
});
alert('bye');
ITS NOT UPDATING THE URL
I can see a couple issues: you can’t access
chrome.tabsin a content script, andchrome.tabs.getSelectedrequires the “tabs” permission.Setting
window.locationshould work in the content script though, so if that’s all you need to do, myscripts.js could be as simple as:If you do need to use
chrome.tabs.update, you can access it from a background page. Basically, you would need to set up anonRequestevent handler in the background page, and send a request usingchrome.extension.sendRequestfrom the content script.So basically you’d have this for your content script (myscript.js):
and your background page would have a script element with something like this: