Reputation: 21
I actually want to use this with a more complex re-write of the URL but for simplicity right now I want to accomplish something more basic. So if I'm on https://www.google.com and click the button for my extension, I want it open a new tab with http://www.google.com/images
I found separate questions about fetching the URL and opening a new tab, but I haven't been able to find anything about combining the two.
This is what I 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 this such as having the chrome.browserAction.onClicked.addListener block nested inside of the chrome.tabs.query block and vice versa but none seem to work.
With the current version, the tab opens but the URL is chrome-extension://bffolfmjagnpglbmjkgajodjlfpekaeo/[object%20Object]/images with a "webpage is not found" error.
Upvotes: 1
Views: 1262
Reputation: 21
Actually, I think I figured it out. It seems to work if I 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 this 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 });
});
});
Upvotes: 1