Javascript无法在网页Chrome扩展程序上使用

时间:2018-08-01 07:36:46

标签: javascript jquery html google-chrome-extension

我刚刚开始学习Chrome扩展程序,并希望运行一个脚本以在任何网站上查找上标。 如果只是html页面(本地)或popup.html中的内容,则一切正常,但不适用于外部实时网站。 它只是不会在popup.html之外执行。控制台没有显示任何错误。请帮助。

popup.html

  <!doctype html>
  <html>
  <head>
    <title>Find Many Strings</title>
    <style>
    body {
      min-width: 357px;
      margin: 5px;
      background: black;
      color: white;
      overflow-x: hidden;
      font-family: 'Droid Sans', arial, sans-serif;
    }
    </style>

    <script src="jquery.js"></script>

    <script src="thecode.js"></script>

  </head>
  <body>
  <sup>&reg;</sup>
          <sup>&reg;</sup>
          <sup>&reg;</sup>
          <sub>&reg;</sub>
          <sub>&reg;</sub>
  <select id="t1">
                    <option value="&trade;">&trade;</option>
                    <option value="&reg;" >&reg;</option>
                </select></br></br>
                  <button id="myFunction">Search Superscript</button>

  </body>
  </html>

find.js

function scriptsearch(){
 // document.getElementById("myFunction").addEventListener("click", myFunction);

    var e = document.getElementById("t1");
    var strUser = e.options[e.selectedIndex].value;

        var i;
      for (i = 0; i < strUser.length; i++) {
      var list = document.getElementsByTagName("SUP");


        }
          var y;
        for (y = 0; y < list.length; y++) {
          if(list[y].innerHTML.indexOf(strUser) !== -1) {
          $(list[y]).css("backgroundColor", "red");
          alert(1);
          $(list[y]).addClass("ok");

        }
        }

          }

manifest.json

{
  "update_url": "https://clients2.google.com/service/update2/crx",

    "manifest_version": 2,
    "name": "Find Superscript",
    "version": "1.0",
    "description": "Search and highlight superscripts on webpages",
    "browser_action": {
      "default_icon": "icon.png",
      "default_popup": "popup.html"
    },
    "content_scripts": [ {
        "matches": [ "<all_urls>" ],
        "js": [ "jquery.js", "find.js" ]
     } ],
    "permissions": [
    "tabs", "http://*/*", "https://*/*"
    ]
  }

thecode.js

function hello() {
    chrome.tabs.executeScript({
      file: 'find.js'
    }); 
  }


  document.addEventListener('DOMContentLoaded', function () {
     //document.querySelector('button').addEventListener('click', scriptsearch);
    var scriptButton = document.getElementById('myFunction');
    scriptButton.addEventListener('click', hello);
  });

0 个答案:

没有答案