简体   繁体   English

Google Chrome扩展程序-自动创建和删除标签

[英]Google Chrome extension - create and remove a tab automatically

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: 在我的background.html中,我具有以下内容:

<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. 我的问题是上面的设置,它在“ url”加载之前关闭了选项卡,因此它从不执行脚本。

If I take out the getSelected lines (lines 5-7), it opens the tab and runs the script perfectly. 如果我删除了getSelected行(第5-7行),它将打开选项卡并完美运行脚本。 I am trying to just get the syntax to close the tab automatically after it executes. 我正在尝试获取语法,以便在执行后自动关闭选项卡。

Thanks! 谢谢!

This worked for me: 这对我有用:

chrome.tabs.create({ url: yourUrl },function(tab){
                                                setTimeout(function(){chrome.tabs.remove(tab.id);}, 200);
                                    });

Not exactly sure what you are trying to accomplish, 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 . 不确定要执行的操作,但是如果要在脚本运行后关闭选项卡,则应让脚本使用chrome.extension.sendRequest向background.html发送“ Close Me”请求。

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. 使用chrome.tabs.executeScript可能会更好,它允许您传递一个函数(可以关闭选项卡),该函数在脚本运行完后会被调用。

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). 我想制作一个Chrome扩展程序,该扩展程序将给定选项卡的URL(最终是选定的文本)传递给我的程序(在OS X中)。

The problem is that Chrome closes the tab immediately after it opened it and does not really load the URL. 问题在于Chrome浏览器在打开标签页后会立即关闭该标签页,并不会真正加载该网址。 The only way to make this happen is by inserting the alert-command, which is not practicable. 实现此目的的唯一方法是插入alert-command,这是不切实际的。

I also tried it with 我也尝试过

chrome.tabs.onCreated.addListener chrome.tabs.onCreated.addListener

but it's the same result, and setTimeout does not seem to be registered at all. 但是结果是一样的,而且setTimeout似乎根本没有被注册。

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); }); }); chrome.browserAction.onClicked.addListener(function(tab){chrome.tabs.create({url:“ myApp:add?url =” + encodeURIComponent(tab.url)}},function(tab){alert(“您的网址是已添加到myApp“); chrome.tabs.remove(tab.id);});});

I would need the exact same result, only without the alert box.... thank you for your help! 我需要完全相同的结果,只是没有警报框...。谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM