Chrome扩展程序 - 背景/事件JavaScript无响应或加载

时间:2017-05-03 04:08:59

标签: javascript jquery google-chrome google-chrome-extension

新的扩展,我原本是试图加载cookie,但实现我的EventPage.js似乎没有加载 - 因为基本的console.log(“你好”)没有加载。

我正在通过Chrome“加载解压扩展程序”加载扩展程序,其中包含清单,内容和后台脚本。

下面附带的代码就是我用来查看是否可以从事件页面获取控制台日志的代码。只有内容脚本加载,即使我按了一个键并且已记录。

反转片:58

有什么建议吗?

Manifest.SON

{
"manifest_version": 2,
"name": "Facebook  Extension  ",
"version": "1.0",
"description": "",
"permissions": ["storage", "webNavigation", "activeTab", "tabs", "cookies",  
"*://*.facebook.com/*"], 

"background": [
{
"scripts": ["event_Page.js"],
"persistent" : false
}
],

"content_scripts": [
{
 "matches": ["*://*.youtube.com/*"],
"css": ["style.css"],
"js": ["jquery-2.1.0.min.js", "talkToEvent.js"]
}

talkToEvent.js

   console.log("Hello World!s");
   $(document).ready(function() {
   console.log("DOM READY!");
  $(document.documentElement).keydown(function (e) {
    console.log("Key Has Been Pressed!");
    chrome.runtime.sendMessage({Message: "getTextFile"}, function (response) {
        ;
    })
  })
});

event_Page.js

     console.log("Atleast reached background.js")
     chrome.runtime.onMessage.addListener (
    function (request, sender, sendResponse) {
    console.log("Reached Background.js");
    if (request.Message == "getTextFile") {
        console.log("Entered IF Block");
        $.get("http://localhost:63342/Projects/StackOverflow/ChromeEXT/helloWorld1", function(response) {
            console.log(response);

            // to send back your response  to the current tab
            chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
                chrome.tabs.sendMessage(tabs[0].id, {fileData: response}, function(response) {
                    ;
                });
            });


        })
    }
    else {
        console.log("Did not receive the response!!!")
    }
}
);

1 个答案:

答案 0 :(得分:0)

您的manifest.json不正确。

根据event pages docs它应该是一个对象,但在你的清单中它是一个数组。

更正 manifest.json

{
  "manifest_version": 2,
  "name": "Facebook  Extension  ",
  "version": "1.0",
  "description": "",
  "permissions": [
    "storage",
    "webNavigation",
    "activeTab",
    "tabs",
    "cookies",
    "*://*.facebook.com/*"
  ],
  "background": {
    "scripts": [
      "event_Page.js"
    ],
    "persistent": false
  },

  "content_scripts": [
    {
      "matches": [
        "*://*.youtube.com/*"
      ],
      "css": [
        "style.css"
      ],
      "js": [
        "jquery-2.1.0.min.js",
        "talkToEvent.js"
      ]
    }
  ]
}

还修复了错过的逗号和关闭括号。