[英]Chrome Extension: URL not opening in new tab
因此,我得到了一个动态URL,该URL要在新选项卡中打开。
PS:我目前正在用内容脚本编写此内容。 例如
var newURL = document.querySelectorAll("a")[lengthA].href
我想在新标签页中打开它。 在javascript中,我们简单地执行window.open以在新选项卡中打开URL,但是它不起作用,然后我用google搜索了相同的内容,然后看到要在新选项卡中打开URL,我们需要这样做。
chrome.tabs.create({ url: newURL });
所以我也一样
var newURL = document.querySelectorAll("a")[lengthA].href
chrome.tabs.create({ url: newURL });
但是,即使那样也不行。 我在这里做错了什么?
为了您正在做的事情,我做了很多扩展,我使用了这个:
function MyFunc() {
var win = window.open("https://www.google.com", '_blank');
win.focus;
}
如果您添加这样的事件监听器
document.addEventListener('DOMContentLoaded', function () {
document.getElementById("MyButton").addEventListener("mousedown", MyFunc);
});
它应该完美地工作
内容脚本无法访问所有chrome API,因此您需要向事件页面发送消息。
在您的内容脚本中,这样做可以向事件页面发送消息
chrome.runtime.sendMessage({otab: "sendx", xname: newURL});
现在,在您的活动页面中,您需要接收消息以打开它。
chrome.runtime.onMessage.addListener(function(request, sender, senderResponse){
if (request.otab == "sendx") {
chrome.tabs.create({url: request.xname });
}
})
请参阅此文档https://developer.chrome.com/extensions/content_scripts
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.