Google chrome Chrome扩展-如何在清单版本3(MV3)中获取窗口对象?

Google chrome Chrome扩展-如何在清单版本3(MV3)中获取窗口对象?,google-chrome,google-chrome-extension,manifest.json,Google Chrome,Google Chrome Extension,Manifest.json,概述 我能够通过新的脚本成功地注入我的Javascript脚本 但是,我仍然无法通过这个注入脚本访问页面的窗口对象 我尝试过的 回到MV2,我曾经做过以下工作: async function getWindow() { return new Promise(function (resolve, reject) { const script = document.createElement('script'); script.text = `

概述

我能够通过新的脚本成功地注入我的Javascript脚本

但是,我仍然无法通过这个注入脚本访问页面的
窗口
对象

我尝试过的

回到MV2,我曾经做过以下工作:

async function getWindow() {
    return new Promise(function (resolve, reject) {
      const script = document.createElement('script');
      script.text = `
            const _window = JSON.stringify(window);
            document.body.insertAdjacentHTML("beforebegin", "<div id='dummy' style='display:none;width:0;height:0;pointer-events:none;'>"+_window+"</div>")
        `;

      document.getElementsByTagName('head')[0].appendChild(script);

      const _window = document.getElementById('dummy');
      const data = JSON.parse(_window.innerText)

      script.remove();
      resolve(data);
    });
  }

 (async () => {
    const _window = await getWindow();
    console.log(_window)
  })();

此外,我还将我的脚本添加到了
content\u scripts
web\u accessible\u resources

非常感谢您的帮助

谢谢大家!

1)为脚本元素使用单独的js文件,2)JSON.stringify(窗口)将由于循环引用而失败。3) 例如:
  "permissions": ["scripting", "activeTab", "webRequest"]