admin管理员组文章数量:1426855
I am creating an extension that will launch an external script based on highlighted text. So, far, the script works, except I am having issues closing the newly created window.
In my background.html, I have the following:
<script>
function executeScript(selection) {
var queryText = 'script:' + selectedText;
chrome.tabs.create({url: queryText});
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.remove(tab.id);
});
}
</script>
My problem is with the setup above, it closes the tab before the "url" loads, so it never executes the script.
If I take out the getSelected lines (lines 5-7), it opens the tab and runs the script perfectly. I am trying to just get the syntax to close the tab automatically after it executes.
Thanks!
I am creating an extension that will launch an external script based on highlighted text. So, far, the script works, except I am having issues closing the newly created window.
In my background.html, I have the following:
<script>
function executeScript(selection) {
var queryText = 'script:' + selectedText;
chrome.tabs.create({url: queryText});
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.remove(tab.id);
});
}
</script>
My problem is with the setup above, it closes the tab before the "url" loads, so it never executes the script.
If I take out the getSelected lines (lines 5-7), it opens the tab and runs the script perfectly. I am trying to just get the syntax to close the tab automatically after it executes.
Thanks!
Share Improve this question asked Jun 24, 2010 at 18:46 TribalmTribalm 273 silver badges6 bronze badges 3-
I don't know for sure, but you're probably looking for some kind of
onLoad
event. – Sasha Chedygov Commented Jun 24, 2010 at 18:48 -
Is there a reason you're not using Javascript's
eval
function to execute the script in the background page directly rather than opening and closing a new tab? – Chetan Commented Jun 24, 2010 at 19:41 - because when the url goes of script:foo, it launches and passes the variable foo to the script. I am trying to acplish the same functionality I have with a firefox plugin within chrome. – Tribalm Commented Jun 24, 2010 at 21:09
3 Answers
Reset to default 1Not exactly sure what you are trying to acplish, but if you want to close a tab after a script has run you should have the script send a "Close Me" request to background.html using chrome.extension.sendRequest
.
You might be better off using chrome.tabs.executeScript, which allows you to pass a function (in which you could close the tab) which gets called after the script has finished running.
I would like to post a followup question to this issue:
I want to make a Chrome Extension which passes on the URL (and eventually a selected Text) of a given Tab to my program (in OS X).
The problem is that Chrome closes the tab immediately after it opened it and does not really load the URL. The only way to make this happen is by inserting the alert-mand, which is not practicable.
I also tried it with
chrome.tabs.onCreated.addListener
but it's the same result, and setTimeout does not seem to be registered at all.
My code:
chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.create({url:"myApp:add?url="+encodeURIComponent(tab.url)}, function(tab){ alert("Your URL is added to myApp"); chrome.tabs.remove(tab.id); }); });
I would need the exact same result, only without the alert box.... thank you for your help!
This worked for me:
chrome.tabs.create({ url: yourUrl },function(tab){
setTimeout(function(){chrome.tabs.remove(tab.id);}, 200);
});
本文标签: javascriptGoogle Chrome extensioncreate and remove a tab automaticallyStack Overflow
版权声明:本文标题:javascript - Google Chrome extension - create and remove a tab automatically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483808a2660292.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论