TypeError:内容脚本中未定义或为什么我不能在内容脚本中执行此操作?
我试图在Firefox中编写一个简单的扩展,其中我修改了X-Frame-Allow
头。
我简要地查看了文档,发现它们支持webRequest.onHeadersReceived.addListener()
。 但是,我无法让我的代码在收到标题时运行。
的manifest.json
{ "manifest_version": 2, "name": "xframeoptions", "version": "1.0", "description": "Set X-Frame-Options to ALLOW", "icons": { "48": "icons/icon.png" }, "permissions": [ "webRequest", "webRequestBlocking", "<all_urls>" ], "content_scripts": [ { "matches": ["<all_urls>"], "js": ["xframeoptions.js"] } ] }
xframeoptions.js
function rewriteHeader(e) { console.log(e.responseHeaders); for (var header of e.responseHeaders) { console.log(header.name + ":" + header.value); if (header.name == "X-Frame-Options") { header.value = 'ALLOW'; modified = true; break; } } return {responseHeaders: e.responseHeaders}; } console.log("Initializing xframeoptions extension ...test"); browser.webRequest.onHeadersReceived.addListener( rewriteHeader, {urls: ['<all_urls>']}, ["blocking", "responseHeaders"] );
如何通过Firefox的WebExtensions修改响应标题?
内容脚本无法访问您正在使用的API
您正尝试从内容脚本执行此操作。 你需要从后台脚本来做这件事。 内容脚本可以访问WebExtensions API的一小部分。 可用的API包括(来自MDN内容脚本页面 ):
从
extension
:
getURL()
inIncognitoContext
从
runtime
:
connect()
getManifest()
getURL()
onConnect
onMessage
sendMessage()
从
i18n
:
getMessage()
getAcceptLanguages()
getUILanguage()
detectLanguage()
一切从
storage
。
这不包括你正在尝试使用的API(例如webRequest
)。
改变你的manifest.json来使用一个background
页面
您应该更改您的manifest.json ,而不是为您的xframeoptions.js有一个content_scripts
键,使用如下所示的background
脚本运行它:
"background": { "scripts": [ "xframeoptions.js" ] },
扩展分为后台脚本和内容脚本
如果您需要在内容脚本中调用API的信息,则需要使用消息传递来在内容脚本和后台脚本之间进行通信 。 但是,通常可以将完整的逻辑移动到在后台上下文中运行的脚本(后台脚本,popup脚本,选项页面脚本等)。 到底需要什么,取决于你想要用脚本完成什么。
在后台上下文中可用的所有特权API之间的function分离和对内容脚本中可用的网页内容的访问(具有对特权API的非常有限的访问)以及它们之间的asynchronous消息传递是扩展如何构build的基础 。 你需要围绕这个架构来devise你的扩展。
这个问题可能导致一些可能的错误
这个问题可能会导致一些可能的错误。 以下是可能的错误的不完整列表:
TypeError: browser.alarms is undefined TypeError: browser.bookmarks is undefined TypeError: browser.browserAction is undefined TypeError: browser.browsingData is undefined TypeError: browser.commands is undefined TypeError: browser.contextMenus is undefined TypeError: browser.contextualIdentities is undefined TypeError: browser.cookies is undefined TypeError: browser.devtools.inspectedWindow is undefined TypeError: browser.downloads is undefined TypeError: browser.events is undefined TypeError: browser.extension.getBackgroundPage is undefined TypeError: browser.extension.getExtensionTabs is undefined TypeError: browser.extension.getViews is undefined TypeError: browser.extension.isAllowedFileSchemeAccess is undefined TypeError: browser.extension.isAllowedIncognitoAccess is undefined TypeError: browser.extension.lastError is undefined TypeError: browser.extension.onRequest is undefined TypeError: browser.extension.onRequestExternal is undefined TypeError: browser.extension.sendRequest is undefined TypeError: browser.extension.setUpdateUrlData is undefined TypeError: browser.extension.ViewType is undefined TypeError: browser.extensionTypes is undefined TypeError: browser.history is undefined TypeError: browser.i18n.LanguageCode is undefined TypeError: browser.identity is undefined TypeError: browser.idle is undefined TypeError: browser.management is undefined TypeError: browser.notifications is undefined TypeError: browser.omnibox is undefined TypeError: browser.pageAction is undefined TypeError: browser.privacy is undefined TypeError: browser.runtime.connectNative is undefined TypeError: browser.runtime.getBackgroundPage is undefined TypeError: browser.runtime.getBrowserInfo is undefined TypeError: browser.runtime.getPackageDirectoryEntry is undefined TypeError: browser.runtime.getPlatformInfo is undefined TypeError: browser.runtime.id is undefined TypeError: browser.runtime.lastError is undefined TypeError: browser.runtime.MessageSender is undefined TypeError: browser.runtime.onBrowserUpdateAvailable is undefined TypeError: browser.runtime.onConnectExternal is undefined TypeError: browser.runtime.onInstalled is undefined TypeError: browser.runtime.OnInstalledReason is undefined TypeError: browser.runtime.onMessageExternal is undefined TypeError: browser.runtime.onRestartRequired is undefined TypeError: browser.runtime.OnRestartRequiredReason is undefined TypeError: browser.runtime.onStartup is undefined TypeError: browser.runtime.onSuspend is undefined TypeError: browser.runtime.onSuspendCanceled is undefined TypeError: browser.runtime.onUpdateAvailable is undefined TypeError: browser.runtime.openOptionsPage is undefined TypeError: browser.runtime.PlatformArch is undefined TypeError: browser.runtime.PlatformInfo is undefined TypeError: browser.runtime.PlatformNaclArch is undefined TypeError: browser.runtime.PlatformOs is undefined TypeError: browser.runtime.Port is undefined TypeError: browser.runtime.reload is undefined TypeError: browser.runtime.requestUpdateCheck is undefined TypeError: browser.runtime.RequestUpdateCheckStatus is undefined TypeError: browser.runtime.sendNativeMessage is undefined TypeError: browser.runtime.setUninstallURL is undefined TypeError: browser.sessions is undefined TypeError: browser.sidebarAction is undefined TypeError: browser.tabs is undefined TypeError: browser.thing is undefined TypeError: browser.topSites is undefined TypeError: browser.webNavigation is undefined TypeError: browser.webRequest is undefined TypeError: browser.windows is undefined TypeError: chrome.alarms is undefined TypeError: chrome.bookmarks is undefined TypeError: chrome.browserAction is undefined TypeError: chrome.browsingData is undefined TypeError: chrome.commands is undefined TypeError: chrome.contextMenus is undefined TypeError: chrome.contextualIdentities is undefined TypeError: chrome.cookies is undefined TypeError: chrome.devtools.inspectedWindow is undefined TypeError: chrome.downloads is undefined TypeError: chrome.events is undefined TypeError: chrome.extension.getBackgroundPage is undefined TypeError: chrome.extension.getExtensionTabs is undefined TypeError: chrome.extension.getViews is undefined TypeError: chrome.extension.isAllowedFileSchemeAccess is undefined TypeError: chrome.extension.isAllowedIncognitoAccess is undefined TypeError: chrome.extension.lastError is undefined TypeError: chrome.extension.onRequest is undefined TypeError: chrome.extension.onRequestExternal is undefined TypeError: chrome.extension.sendRequest is undefined TypeError: chrome.extension.setUpdateUrlData is undefined TypeError: chrome.extension.ViewType is undefined TypeError: chrome.extensionTypes is undefined TypeError: chrome.history is undefined TypeError: chrome.i18n.LanguageCode is undefined TypeError: chrome.identity is undefined TypeError: chrome.idle is undefined TypeError: chrome.management is undefined TypeError: chrome.notifications is undefined TypeError: chrome.omnibox is undefined TypeError: chrome.pageAction is undefined TypeError: chrome.privacy is undefined TypeError: chrome.runtime.connectNative is undefined TypeError: chrome.runtime.getBackgroundPage is undefined TypeError: chrome.runtime.getBrowserInfo is undefined TypeError: chrome.runtime.getPackageDirectoryEntry is undefined TypeError: chrome.runtime.getPlatformInfo is undefined TypeError: chrome.runtime.id is undefined TypeError: chrome.runtime.lastError is undefined TypeError: chrome.runtime.MessageSender is undefined TypeError: chrome.runtime.onBrowserUpdateAvailable is undefined TypeError: chrome.runtime.onConnectExternal is undefined TypeError: chrome.runtime.onInstalled is undefined TypeError: chrome.runtime.OnInstalledReason is undefined TypeError: chrome.runtime.onMessageExternal is undefined TypeError: chrome.runtime.onRestartRequired is undefined TypeError: chrome.runtime.OnRestartRequiredReason is undefined TypeError: chrome.runtime.onStartup is undefined TypeError: chrome.runtime.onSuspend is undefined TypeError: chrome.runtime.onSuspendCanceled is undefined TypeError: chrome.runtime.onUpdateAvailable is undefined TypeError: chrome.runtime.openOptionsPage is undefined TypeError: chrome.runtime.PlatformArch is undefined TypeError: chrome.runtime.PlatformInfo is undefined TypeError: chrome.runtime.PlatformNaclArch is undefined TypeError: chrome.runtime.PlatformOs is undefined TypeError: chrome.runtime.Port is undefined TypeError: chrome.runtime.reload is undefined TypeError: chrome.runtime.requestUpdateCheck is undefined TypeError: chrome.runtime.RequestUpdateCheckStatus is undefined TypeError: chrome.runtime.sendNativeMessage is undefined TypeError: chrome.runtime.setUninstallURL is undefined TypeError: chrome.sessions is undefined TypeError: chrome.sidebarAction is undefined TypeError: chrome.tabs is undefined TypeError: chrome.thing is undefined TypeError: chrome.topSites is undefined TypeError: chrome.webNavigation is undefined TypeError: chrome.webRequest is undefined TypeError: chrome.windows is undefined
- 在Firefox中打印PDF
- animationscrollTop不工作在Firefox
- jQuery的event.preventDefault()不工作在Firefox(包括JSFiddle)
- Firefox的“查看源代码”是否可以设置为不发出新的GET请求?
- 我如何以编程方式点击JavaScript中的元素?
- 如何使用Selenium禁用Firefox的不可信连接警告?
- Firefox 4的onBeforeUnload自定义消息
- jQuery $ .ajax(),$ .post在Firefox中发送“OPTIONS”作为REQUEST_METHOD
- 为什么Firefox不支持<audio>中的MP3文件格式