让谷歌插件单独一个窗口运行
示例
一、manifest.json配置默认popup
{
"manifest_version": 3,
"name": "插件测试",
"description": "将Vue项目放到插件插件中运行",
"version": "0.0.1",
"icons": {
"16": "statics/imgs/favicon_16.png",
"36": "statics/imgs/favicon_36.png",
"48": "statics/imgs/favicon_48.png",
"128": "statics/imgs/favicon_128.png"
},
"action": {
"default_icon": {
"16": "statics/imgs/favicon_16.png",
"36": "statics/imgs/favicon_36.png",
"48": "statics/imgs/favicon_48.png",
"128": "statics/imgs/favicon_128.png"
},
"default_popup": "views/popup.html"
},
"content_security_policy": {
"extension_pages": "style-src 'self' 'unsafe-inline';script-src 'self'; object-src 'self' ;",
"sandbox": "sandbox allow-scripts; script-src 'self'; object-src 'self' "
}
}
二、popup执行脚本
<!-- 目前仅仅是为了执行脚本 -->
<script src="../js/index.js"></script>
三、index.js脚本打开新的popup
// 当脚本被执行时
window.onload = () => {
openpage();
function openpage() {
// 获取当前窗口
chrome.windows.getCurrent((currentWindow) => {
// 聚焦一下当前窗口,主要是为了关闭popup
chrome.windows.update(currentWindow.id, { focused: true });
// 默认宽高和位置
let newWindowWidth = 1024;
let newWindowHeight = 600;
let newWindowLeft = currentWindow.left + currentWindow.width - newWindowWidth;
let newWindowTop = 0;
chrome.windows.create({
url: "../views/dist/index.html", // 一个Vue项目打包后的入口文件
type: "popup",
width: newWindowWidth,
height: newWindowHeight,
left: newWindowLeft,
top: newWindowTop
}, (createdWindow) => {
// 聚焦到新窗口
chrome.windows.update(createdWindow.id, { focused: true });
});
})
}
}
四、完整代码:Qmagician/extensionsSingleWindow (gitee.com)https://gitee.com/qmagician/extensions-single-window