点击按钮,Chrome扩展弹出一个新窗口

Chrome Extension pop up a new window on button click

本文关键字:新窗口 窗口 一个 按钮 Chrome 扩展      更新时间:2023-09-26

我正在创建一个Chrome扩展,您可以在其中单击一个按钮来启动您之前创建的HTML页面。这是我的按钮:

var btn = document.createElement("BUTTON");       
var t = document.createTextNode("Click Me");
btn.addEventListener("click", popUpWindow);
btn.appendChild(t);                              
document.getElementsByClassName("text")[0].appendChild(btn); 

这是我的功能:

function popUpWindow() {
    window.open("window-child.html", "Accept", "width=400,height=300,0,status=0,")
}

这是我的html页面

<html> 
    <head> 
        <title>Demo of child window</title>  
    </head>  
    <body>   
        This is child window 
    </body>
</html>

在manifest.json中,我添加了:

"web_accessible_resources": [ "window-child.html" ]

但当我点击按钮时,它会将我重定向到https://www.linkedin.com/window-child.html,给出:

404找不到页面

使用chrome.extension.getURL()生成表单的URL

chrome扩展名://[PACKAGE ID]/[PATH]

代码:

var url = chrome.extension.getURL("window-child.html");
window.open(url);

使用"_blank"目标名称作为窗口的第二个参数。打开:

window.open("window-child.html","_blank", "width=400,height=300,0,status=0")