首页 > 解决方案 > 如何检查浏览器扩展内容脚本是否已加载到后台页面中包含的外部 iframe 上?

问题描述

假设我的扩展程序有一个后台页面,该页面注入了一个外部 iframe,该 iframe 又运行一个内容脚本,如下所示。你会建议我用什么机制来检查后台脚本接收到的消息是否onConnect来自onMessage后台页面中加载的内容脚本,而不是来自任何其他永恒网页?

背景.js

import browser from 'webextension-polyfill';

var iframe = document.createElement("iframe");
iframe.src = 'https://example.com/favicon.ico';
document.body.appendChild(iframe);

清单.json

{
  "name": "My Extension",
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  },
  "manifest_version": 2,
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
  "content_scripts": [
    {
      "matches": ["https://example.com/favicon.ico"],
      "js": ["content.js"],
      "all_frames": true
    }
  ],
  "permissions": [
    "*://example.com/*",
    "webRequest",
    "webRequestBlocking"
  ]
}

标签: google-chrome-extensionfirefox-addon

解决方案


推荐阅读