让谷歌插件单独一个窗口运行

示例

一、manifest.json配置默认popup,其中host_permissions属性可用于跨域请求,就是当前端请求后端接口时可能会出现跨域的情况,然后将接口域名部分加到host_permissions就可以了,多个用逗号隔开。

{
  "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"
  },
  "host_permissions": ["https://example.com/index.php", "https://www.baidu.com"],
  "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)icon-default.png?t=N7T8https://gitee.com/qmagician/extensions-single-window

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QmagicianRX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值