【发布时间】:2021-07-30 04:12:38
【问题描述】:
如何通过 Manifest v3 访问页面的窗口变量。
在 V2 中,我可以让内容脚本通过将 javascript 注入主页面来访问页面的窗口变量。但是,javascript 代码的 appendChild() 不适用于 V3。
我尝试使用 scripting.executeScript 但这仍然无法访问 window 变量(DOM 是可访问的)。
例如,下面的代码应该在 frameid 0 上注入,但顶部 javascript 上下文的窗口仍然不可读:
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
// since only one tab should be active and in the current window at once
// the return variable should only have one entry
const activeTab = tabs[0];
const activeTabId = activeTab.id; // or do whatever you need
chrome.scripting.executeScript({
target: { tabId: activeTabId, frameIds: [0] },
files: [filePath],
});
});
【问题讨论】:
-
为您的 DOM
script元素的src使用单独的 js 文件,而不是textContent。并通过 web_accessible_resources 公开该文件。
标签: google-chrome-extension chrome-extension-manifest-v3