TypeError:[API]在内容脚本中未定义或为什么我不能在内容脚本中执行此操作?

时间:2016-12-06 12:59:20

标签: javascript firefox firefox-webextensions

我试图在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修改响应标头?

2 个答案:

答案 0 :(得分:12)

内容脚本无法访问您正在使用的API

您正尝试从content script执行此操作。您需要从background script执行此操作。内容脚本可以访问WebExtensions API的一小部分。可用的API包括(来自MDN Content Scripts page):

  

来自extension

           

来自runtime

           

来自i18n

           

来自storage的一切。

这不包括您尝试使用的API(例如webRequest)。

更改 manifest.json 以使用background页面

您应该将 manifest.json 更改为 xframeoptions.js content_scripts密钥,并将其作为background运行脚本使用类似的东西:

"background": {
    "scripts": [
        "xframeoptions.js"
    ]
},

扩展分为后台脚本和内容脚本

如果您需要在内容脚本中调用API的信息,则需要使用message passing to communicate between the content script and your background script。但是,通常可以将完整逻辑移动到在后台上下文中运行的脚本(后台脚本,弹出脚本,选项页脚本等)。确切地说,将需要什么取决于您希望用脚本完成的任务。

在后台上下文中可用的所有特权API之间的功能分离以及对内容脚本中可用的网页内容的访问(对特权API的访问非常有限),以及它们之间的异步消息传递,是{{3 }}。您需要围绕此架构设计扩展。

此问题可能导致的一些错误

此问题可能导致许多可能的错误。以下是可能错误的不完整列表:

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

答案 1 :(得分:1)

This question带我到了这里。我的browserAction出现了问题。此行位于我的background.js中,用于处理对扩展程序图标的点击:

browser.browserAction.onClicked.addListener(handleClick);

该行给了我这个错误:

  

类型错误:browser.browserAction未定义

什么是问题?我只是忘了在manifest.json中定义browser_action

"browser_action": {
    "default_icon": "my-icon.png"
}

也许这暗示可能是别人有帮助...; - )