如何更改Google Chrome扩展程序中的网页内容

时间:2012-11-26 20:21:08

标签: google-chrome-extension message

我正在撰写扩展程序,它将在网页上强调英文单词的重音。 点击弹出窗口中的“搜索”按钮后,我就陷入困境,似乎没有任何事情发生。

以下是该方案:

  1. 用户双击网页上的单词。
  2. 整个单词都被标记了。
  3. 用户点击Chrome浏览器栏上的扩展程序图标。
  4. 显示一个弹出窗口。弹出窗口中的输入字段用标记的单词填充。
  5. 用户添加重音。 IE浏览器。如果标记的单词是'boundary',则在弹出的输入字段中将显示:'boudary'。然后用户将输入值修改为:'boudary,bo'(不含引号)。
  6. 用户点击弹出窗口中的“搜索”按钮。
  7. 页面上“边界”字样中的字母“bo”正在加下划线。
  8. 的manifest.json

    {
      "content_scripts": [ {
        "js": [ "jquery.js", "jquery.highlight-4.js", "selection.js" ],
        "matches": [ "\u003Call_urls\u003E" ]
      } ],
      "name": "Mark accent",
      "version": "1.0",
      "manifest_version": 2,
      "options_page": "options.html",
      "description": "Marks accent in english words for selected word on page",
      "browser_action": {
        "default_icon": "icon.png",
        "default_popup": "popup.html"
      },
      "icons": {
        "128": "icon.png"
      },
      "permissions": [ "tabs", "http://*/*", "https://*/*", "storage", "file:///*" ]
    }
    

    app.js

    chrome.tabs.getSelected(null, function(tab) {
      chrome.tabs.sendRequest(tab.id, {method: "getSelection"}, function (response) {
        $("#t1").val(response.data);
        console.log('input t1 value: ' + $("#t1").val(response.data));
      });
    });
    $("#t1").keypress(function(event) {
      if ( event.which == 13 ) {
        $("#search_btn").click();
      }
    });
    $("#t1").focus();
    
    function search(that) {
      var token = new String (t1.value);
      chrome.tabs.executeScript(null,
        {code:"$(document.body).highlight('"+token+"','text-decoration:underline')"});
      window.close();
    }
    

    selection.js

    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
      if (request.method == "getSelection")
        sendResponse({data: window.getSelection().toString()});
      else
        sendResponse({}); // snub them.
    });
    

    popup.html

    <style>
    body {
    overflow: hidden; margin: 5px; padding: 0px; background: black;color: white;
    width: 300px; font-family: 'Droid Sans', arial, sans-serif;
    }
    </style>
    Please enter the word to highlight :
    <input type="text" id="t1"/>
      <button onclick="search(this)" id="search_btn">Search</button>
      <button onclick="hl_clear(this)" id="clear_btn">Clear all highlights</button>
      <script src="jquery.js"></script>
    <script src="jquery.highlight-4.js"></script>
    <script src="app.js"></script>
    

    jquery.highlight-4.js

    jQuery.fn.highlight = function(pat, fbgcolor) {
      function innerHighlight(node, pat, fbgcolor) {
        var skip = 0;
        var array = pat.split(',');
        var sWord = array[0];
        var accent = array[1];
        if (node.nodeType == 3) {
          var pos = node.data.toUpperCase().indexOf(sWord);
          if (pos >= 0) {
            var middlebit = node.splitText(pos);
            var endbit = middlebit.splitText(sWord.length);
            var pos2 = middlebit.data.toUpperCase().indexOf(accent);
            if (pos2 >= 0) {
              var spannode = document.createElement('span');
              spannode.className = 'highlight';
              fbgcolor += ";padding: 0px; margin: 0px;";
              spannode.setAttribute('style', fbgcolor);
              var middlebit2 = middlebit.splitText(pos2);
              var endbit2 = middlebit2.splitText(accent.length);
              var middleclone2 = middlebit2.cloneNode(true);
              spannode.appendChild(middleclone2);
              middlebit2.parentNode.replaceChild(spannode, middlebit2);
            }
            skip = 1;
          }
        }
        else if (node.nodeType == 1 && node.childNodes && 
                 !/(script|style)/i.test(node.tagName)) {
          for (var i = 0; i &lt; node.childNodes.length; ++i) {
            i += innerHighlight(node.childNodes[i], pat, fbgcolor);
          }
        }
        return skip;
      }
      return this.each(function() {
        innerHighlight(this, pat.toUpperCase(), fbgcolor);
      });
    };
    

1 个答案:

答案 0 :(得分:1)

经过如此多的修改并消除了非Chrome扩展相关内容。您可以将内容添加到此骨架中。

不要在html中添加内联脚本 <button onclick="search(this)" id="search_btn">Search</button>

enter image description here

代码的基本框架:

<强> 的manifest.json

{
  "content_scripts": [ {
    "js": [ "jquery.js", "jquery.highlight-4.js", "selection.js" ],
    "matches": [ "<all_urls>" ]
  } ],
  "name": "Mark accent",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Marks accent in english words for selected word on page",
  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },
  "icons": {
    "128": "icon.png"
  },
  "permissions": [ "tabs", "<all_urls>" ]
}

<强> app.js

function search(that) {
    console.log("Search is clicked");
  var token = document.getElementById("t1").value;
  console.log(token);
  chrome.tabs.executeScript(null,
    {code:"highlight();"});
  //window.close();
}
window.onload=function (){
    document.getElementById("search_btn").onclick=search;

};

<强> selection.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  if (request.method == "getSelection")
    sendResponse({data: window.getSelection().toString()});
  else
    sendResponse({}); // snub them.
});

<强> popup.html

<html>
<head>
<style>
body {
overflow: hidden; margin: 5px; padding: 0px; background: black;color: white;
width: 300px; font-family: 'Droid Sans', arial, sans-serif;
}
</style>
  <script src="jquery.js"></script>
<script src="app.js"></script>
<body>
Please enter the word to highlight :
<input type="text" id="t1"/>
  <button id="search_btn">Search</button>
  <button id="">Clear all highlights</button>
</body>
</html>

<强> jquery.highlight-4.js

function highlight(){
    console.log("Highlight is called");
}

如果它仍然失败,请告诉我。