最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Google Chrome extension - create and remove a tab automatically - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 1

Not 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);
                                    });
发布评论

评论列表(0)

  1. 暂无评论