javascript - Can an extension close a tab? -
this in continuation of: script close current tab in chrome
i'm trying in extension instead of tampermonkey script, have "matches" : ["*://*.youtube.com/*", "*://youtube.com/*"],
in manifest file, , js script chrome.tabs.remove(tab.id);
or window.close();
both don't close youtube.com page open.
could it's impossible close tab extension?
chrome.tabs
not available content scripts, if that's in code, fails exception.
window.close
has caveat:
this method allowed called windows opened script using
window.open()
method. if window not opened script, following error appears in javascript console: scripts may not close windows not opened script.
i'm not sure if applies content scripts - suppose does.
you can make work adding background script (that has access chrome.tabs
) , either detect navigation there, or message background script context script it.
messaging background:
// content script chrome.runtime.sendmessage({closethis: true}); // background script chrome.runtime.onmessage.addlistener(function(message, sender, sendresponse) { if(message.closethis) chrome.tabs.remove(sender.tab.id); });
i recommend adding
"run_at": "document_start"
content script's configuration, fires earlier.better yet, don't need content script. either rely on
chrome.tabs
events, orchrome.webnavigation
api. need host permission (e.g."*://*.youtube.com/*"
) work (and"webnavigation"
if use it).
Comments
Post a Comment