为什么chrome.extension.getBackgroundPage()返回null? [英] Why does chrome.extension.getBackgroundPage() return null?

查看:514
本文介绍了为什么chrome.extension.getBackgroundPage()返回null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 manifest.json 文件,如下所示:

  {
name:Zend Debugger Extension,
version:0.1,
background_page:background.html,
permissions:[
cookies,tabs,http:// * / *,https:// * / *
],
browser_action:{
default_title: 启动Zend Debugger,
default_icon:icon.png,
popup:popup.html
}
}

以下是我的 background.html

 < HTML> 
< script>
函数testRequest(){
console.log(test Request received);
}
< / script>
< / html>

而我的 popup.html

 < script> 
function debug(target){
if(target.id =='thisPage'){
console.log('sending request');
chrome.extension.getBackgroundPage()。testRequest();
}
}
< / script>

< div onclick =debug(this)id =thisPage>当前页面< / div>

但是, background.html 页似乎无法访问。我得到这个错误:

 未捕获的TypeError:无法调用null的方法'testRequest'

当我检查 chrome.extension.getBackgroundPage()时,我得到一个空值。我想我的清单文件中有一个错误,但我看不到我做错了什么。



谢谢。

解决方案

您缺少背景权限,请查看我的chrome扩展程序的manifest.json文件:

  {
content_scripts:[
{
matches:[http:// * / *],
js:[jquery.js,asserts.js]
}
],
name:Test Extension,
version :1.0,
description:将js注入网页的测试扩展。,
background_page:background.html,
options_page: options.html,
browser_action:{
default_icon:icon.png,
popup:popup.html
},
权限:[
标签,
http:// * / *,https:// * / *,< all_url>,background





$ b

编辑:
你确定你有表现力ound.html 文件放在同一个文件夹中作为所有你的chrome扩展的文件吗?如果是,请尝试从扩展管理页面重新加载你的扩展,我记得有一个错误,我的扩展没有重新加载,所以我去了我的背景页面的开发工具,并从控制台执行 window.location.reload(true); ,修复了它。请回复如果这工作,我会继续研究

I have a manifest.json file that looks like this:

{
  "name": "Zend Debugger Extension",
  "version": "0.1",
  "background_page": "background.html",
  "permissions": [
    "cookies", "tabs", "http://*/*", "https://*/*"
  ],
  "browser_action": {
    "default_title": "Launch Zend Debugger",
    "default_icon": "icon.png",
    "popup": "popup.html"
  }
}

Here's my background.html:

<html>
    <script>
    function testRequest() {
        console.log("test Request received");
    }
    </script>
</html>

And my popup.html:

<script>
function debug(target) {
    if (target.id == 'thisPage') {
        console.log('sending request');
        chrome.extension.getBackgroundPage().testRequest();
    }
}
</script>

<div onclick="debug(this)" id="thisPage">Current Page</div>

However, the background.html page doesn't seem to be accessible. I'm getting this error:

Uncaught TypeError: Cannot call method 'testRequest' of null

When I inspect chrome.extension.getBackgroundPage() I get a null value. I'm thinking I have made a mistake in my manifest file, but I can't see what I've done wrong.

Thanks.

解决方案

You are missing the background permission, take a look at my manifest.json file of my chrome extension:

{
    "content_scripts": [
    {
      "matches": ["http://*/*"],
      "js": ["jquery.js", "asserts.js"]
    }
  ],
  "name": "Test Extension",
  "version": "1.0",
  "description": "A test extension to inject js to a webpage.",
  "background_page": "background.html",
  "options_page": "options.html",
  "browser_action": {
    "default_icon": "icon.png",
    "popup": "popup.html"
  },
  "permissions": [
    "tabs", 
    "http://*/*", "https://*/*", "<all_url>", "background"
  ]
}

EDIT: Are you sure you have the background.html file on the same folder as all your chrome-extension's files?, and if so, try reloading your extension from the extension management page, I remember once had a bug that my extension didn't reload, so I went to the developer's tools for my background page and executed window.location.reload(true); from console, that fixed it. Please respond if this worked, I will keep researching

这篇关于为什么chrome.extension.getBackgroundPage()返回null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆