After creating a tab by passing the URL of its own protocol to the WebExtensions chrome.tab.create, the OS launches an app that supports its own protocol.
Then I want to remove unnecessary tabs.
The callback method passed to the second argument of chrome.tab.create is not invoked.
/**background.js**/
functionOpenNiconicoProtocol(niocnicoUrl="niconico://sm9")
{
chrome.tabs.create({url:niocicoUrl, active:false}, OnTabCreated);
}
function OnTabCreated(tabInfo)
{
console.log("tabid" + tabInfo.id);
chrome.tabs.remove(tabInfo.id,()=>
{
if(chrome.runtime.lastError){
console.log("failed:"+chrome.runtime.lastError);
} else{
console.log("removed tab");
}
});
}
Can't I get a callback for tabs.create with my own protocol URL?
Source code containing manifest.json
https://gist.github.com/tor4kichi/5ea925832be5887a873ac8e7c8394ba5
Test Environment
Windows 10 AU Applied
Firefox Developer Edition 51.0a2 (2016-10-27) (32-bit)
Self-resolved.
For custom URI schemas, callbacks passed to the second argument of chrome.tabs.create are not invoked (we could not determine if this was a bug or specification).
Obtain tabs created with custom URI schemas in chrome.tabs.query and close them manually.
One caveat is that if the custom URI schema does not contain the host element, the urlPattern in chrome.tabs.query({url:urlPattern}) may determine the invalid pattern.
function OpenNiconicoProtocol (niocnicoUrl)
{
chrome.tabs.query({currentWindow:true, active:true}, function(maybeCurrentTab){
varcurrentTab=maybeCurrentTab[0];
// open as a new tab
chrome.tabs.create({url:niocicoUrl, active:false});
// Custom Scheme URL Does Not Call Back Tabs.create
// close unnecessary tabs in a little while
setTimeout(function(){
// The url you give to query is a custom scheme and
// For URL patterns without host parts
// Invalid pattern and unsupported
// Obtain all tabs and avoid unnecessary tabs sequentially
chrome.tabs.query({},(tabs)=>
{
if (tabs&tabs.length)
{
for (vartabIndex intabs)
{
vartab=tabs [tabIndex];
varisRemoveTarget=false;
if(tab.url&isNiconicoSchemeUrl(tab.url))
{
isRemoveTarget=true;
}
else if (currentTab.index<tabIndex)
{
// Pages that are not rendered as http are:
// Tab.url seems to remain about:blank (check Firefox)
// Target blank page tabs that appear to be newer than the current tab
if(tab.url&tab.url=="about:blank")
{
isRemoveTarget=true;
}
}
if(isRemoveTarget)
{
chrome.tabs.remove(tab.id,()=>
{
if(chrome.runtime.lastError){
console.log("failed:"+chrome.runtime.lastError);
} else{
console.log("removed tab");
}
});
}
}
}
});
}, 1000);
});
}
Reference
To Close Tabs Before Opening Links in an External Program from Chrome Extensions
https://stackoverflow.com/questions/18564750/chrome-extension-close-tab-that-opened-an-external-program-before
chrome.tabs.query
https://developer.chrome.com/extensions/tabs#method-query
url pattern
https://developer.chrome.com/extensions/match_patterns
© 2024 OneMinuteCode. All rights reserved.