为什么我的Chrome扩展程序不起作用?

时间:2016-01-20 00:40:43

标签: google-chrome-extension

我花了好几个小时试图找出我的Chrome扩展程序有什么问题。我知道我的function.js代码很好,因为它适用于我链接到的html页面,但也许它不适合chrome扩展? 感谢所有帮助

manifest.json

{
      "manifest_version": 2,

      "name": "Font Find",
      "description": "This extension allows you to click on any text, and find out what font it is written in.",
      "version": "1.0",
      "background": {
              "page": "popup.html",
              "persistent": true
          },

    "permissions": [
      "activeTab",
      "http://*/*",
      "https://*/*"
      ],

      "browser_action": {
        "default_title": "Find the Font",
        "default_icon": "icon.png"//,
    //    "default_popup": "popup.html"
      },

          "background": { "scripts": ["jquery.min.js"] },


        "content_scripts": [
            {
                "matches": [
                    "http://*/*",
                    "https://*/*"
                ],
                "js": [
                   "function.js"
                ]
            }
        ]
    }

popup.html档案(请注意,它未设置为弹出窗口)

                            <!DOCTYPE HTML>
            <html>
                <head>
                    <!-- <link rel="stylesheet" href="popup.css"/> -->

                    <script src="jquery.min.js"></script>
                    <script src="popup.js"></script> 
                    <script src="function.js"></script> 

                </head>

            </html>

function.js档案

             "use strict";

             $("body").append('Test');

            document.body.ondblclick = function(event){

                //event.stopPropagation();
                var target = event.target || event.srcElement;

                alert( "What you clicked on:" + $(target).text() + "   It's font-family is: " + $(target).attr("font-family") );

            }

function.js档案

chrome.browserAction.onClicked.addListener(function (tab) {
    chrome.tabs.executeScript({
        file: "function.js"
    });
});

1 个答案:

答案 0 :(得分:0)

首先,编辑manifest.json文件。添加以下代码:

"content_scripts": [{
    "matches": [
        "http://*/*",
        "https://*/*"
    ], "js": [
        "jquery.min.js",
        "function.js"
    ], "run_at": "document_start"
}]

现在,请在function.js文件中使用此代码。

document.addEventListener("DOMContentLoaded", function() {
    document.body.addEventListener("dblclick", function(e) {
        var target = e.target;
        alert("What you clicked on: " + $(target).text() + "   It's font-family is: " + $(target).attr("font-family"));
    }, true);
});

我测试了这段代码并且它有效。我希望它有所帮助。