4

我正在编写扩展程序,它将强调网页上英语单词的重音。单击弹出窗口上的“搜索”按钮后,我被卡住了,似乎没有任何反应。

这是场景:

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

清单.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:///*" ]
}

应用程序.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();
}

选择.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);
  });
};
4

1 回答 1

1

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

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

在此处输入图像描述

代码的基本骨架:

清单.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>" ]
}

应用程序.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;

};

选择.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");
}

让我知道它是否仍然失败。

于 2012-11-27T07:26:31.070 回答