Reputation: 7128
When I do this:
var a = 'moz-extension://c5b86449-4457-4a39-a758-958faf23fc72/popup.html'
chrome.tabs.query({ url: a }, function(x){});
I get an error:
Invalid match pattern: 'moz-extension://c5b86449-4457-4a39-a758-958faf23fc72/popup.html'
SingleMatchPattern() MatchPattern.jsm:49
this.MatchPattern() MatchPattern.jsm:103
self.tabs.query() ext-tabs.js:556
callAsyncFunction() Extension.jsm:422
inject/stub() Schemas.jsm:1002
However, that is the URL that I need to match on in the chrome.tabs.query
call.
Any suggestions?
Upvotes: 2
Views: 1592
Reputation: 9714
In Firefox v48 the error message no longer appears, however the moz-extension:...
protocol is not yet supported.
chrome.tabs.query
returns 0
in x.length
if search like this "url": "moz-extension:..."
, see example:
chrome.tabs.query({ url: "moz-extension://..." }, function(x){
console.log(x.length);
});
But there is an alternative way to get around the problem, you can search by all tabs without "url:" param, see the following examples:
function getTab(pageFromAddon, callback)
{
var uri = chrome.extension.getURL(pageFromAddon);
chrome.tabs.query({}, function(tabs) {
var tabData;
if (tabs && tabs.length) {
for (var i = tabs.length - 1; i >= 0; i--) {
if (tabs[i].url === uri) {
tabData = tabs[i];
break;
}
}
}
callback(tabData);
});
}
Usage:
//get id, url, title and other info from tab (or false)
console.log(getTab("view/page.html"));
function openOrUpdateTab(pageFromAddon)
{
var uri = chrome.extension.getURL(url);
chrome.tabs.query({}, function(tabs) {
var tabId;
if (tabs && tabs.length) {
for (var i = tabs.length - 1; i >= 0; i--) {
if (tabs[i].url === uri) {
tabId = tabs[i].id;
break;
}
}
}
if (tabId) {
chrome.tabs.update(tabId, { "active": true });
} else {
chrome.tabs.create({ "url": uri });
}
});
return uri;
}
Usage:
//If openned refresh tab, if not create new tab
var id = openOrUpdateTab("view/page.html");
//get url from new or refreshed tab
console.log(url);
For using these functions you need add "tabs"
to permissions:
(manifest.json
) like this:
...
"permissions": [
"<all_urls>",
"tabs"
]
}
Upvotes: 3