Javascript 在 Google Chrome 扩展程序上打开一个新标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8457382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a new tab on Google Chrome Extension
提问by orange
This is my background.html file, It works fine when opening in current tab but I want it to open in new tab, what am I doing wrong?
这是我的 background.html 文件,在当前选项卡中打开时它工作正常,但我希望它在新选项卡中打开,我做错了什么?
<html>
<head>
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
var action_url = "javascript:location.href='http://www.reddit.com/submit?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)";
chrome.tabs.create(tab.id, {url: action_url}, function(tab));
});
</script>
</head>
</html>
回答by abraham
You should read the chrome.tabs.create
documentationagain. You are passing it invald parameters. You are also using location
which is from the background.html
document not the webpage document the code is expecting instead of the tab
parameter passed to the chrome.browserAction.onClicked
listener.
您应该再次阅读chrome.tabs.create
文档。您正在向它传递 invald 参数。您还使用了location
来自background.html
文档而不是代码期望的网页文档,而不是tab
传递给chrome.browserAction.onClicked
侦听器的参数。
<html>
<head>
<script>
// Called when the user clicks on the browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
var action_url = "http://www.reddit.com/submit?url=" + encodeURIComponent(tab.href) + '&title=' + encodeURIComponent(tab.title);
chrome.tabs.create({ url: action_url });
});
</script>
</head>
</html>
回答by user219882
You can try this
你可以试试这个
<html>
...
<body>
<script>
function createTab() {
chrome.tabs.create({url: "http://www.stackoverflow.com"});
}
</script>
<a href="#" onclick="createTab();">Create a new tab</a>
</body>
</html>
回答by Mikett
<html>
<head>
<script type="text/javascript">
window.master = ({
newtab: function(url, callback) {
callback = callback === true ? (function() { this.close(); }) : callback;
try {
chrome.tabs.create({
url: url
});
if(typeof callback === "function") { callback.call(this, url); }
} catch(e) {
/* Catch errors due to possible permission issues. */
}
},
link: function(event, close) {
event = event ? event : window.event;
event.preventDefault();
this.newtab(event.href, close);
},
close: function() { window.self.close(); }
});
</script>
</head>
<body>
<!-- Usage is simple:
HTML:
<a href="http://example.com/" onclick="master.link(event)" />
JavaScript:
master.newtab("http://example.com/", true);
-->
</body>
</html>
If you insist on using a popup and want it to close as soon as it is opened, then use what is above. Simply add the link string and a true
boolean to the master.newtab
function to have it open the new tab and then close the popup.
如果您坚持使用弹出窗口并希望它在打开后立即关闭,请使用上面的内容。只需将链接字符串和一个true
布尔值添加到master.newtab
函数中,即可打开新选项卡,然后关闭弹出窗口。
If you change your mind about closing the popup, you can replace the true
boolean with a function to execute if the new tab was created without any errors. You can also use the master.link
function for calling the master.newtab
function from an anchor element.
如果您改变主意关闭弹出窗口,您可以true
用一个函数替换布尔值,如果新选项卡创建时没有任何错误,则要执行。您还可以使用该函数从锚元素master.link
调用该master.newtab
函数。
The best thing about using Chrome Extensions is you never have to worry about support issues! :D
使用 Chrome 扩展程序的最大好处是您永远不必担心支持问题!:D