Chrome Extension to fetch URL of current tab and open new tab based on re-writing URL -


i want use more complex re-write of url simplicity right want accomplish more basic. if i'm on https://www.google.com , click button extension, want open new tab http://www.google.com/images

i found separate questions fetching url , opening new tab, haven't been able find combining two.

this have right now:

manifest.json:

{   "manifest_version": 2,    "name": "rewrite",   "version": "0.1",    "browser_action": {     "default_icon": "icon.png"   },   "background": {     "scripts": ["background.js"],     "persistent": false   },   "permissions": ["https://*/*", "http://*/*", "tabs"] } 

background.js:

var currenturl;  chrome.tabs.query({ currentwindow: true, active: true }, function (tabs) {   currenturl = tabs[0]; });  chrome.browseraction.onclicked.addlistener(function(activetab) {     var newurl = currenturl + "/images";     chrome.tabs.create({ url: newurl }); }); 

i've tried various forms of such having chrome.browseraction.onclicked.addlistener block nested inside of chrome.tabs.query block , vice versa none seem work.

with current version, tab opens url chrome-extension://bffolfmjagnpglbmjkgajodjlfpekaeo/[object%20object]/images "webpage not found" error.

actually, think figured out. seems work if add '.url' after tabs[0] in background.js:

var currenturl;  chrome.tabs.query({ currentwindow: true, active: true }, function (tabs) {   currenturl = tabs[0].url; });  chrome.browseraction.onclicked.addlistener(function(activetab) {     var newurl = currenturl + "/images";     chrome.tabs.create({ url: newurl }); }); 

although looks cleaner:

chrome.browseraction.onclicked.addlistener(function(activetab) {     chrome.tabs.query({ currentwindow: true, active: true }, function (tabs) {         var newurl = tabs[0].url + "/images";         chrome.tabs.create({ url: newurl });     }); }); 

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -