Google Chrome扩展程序可突出显示并提醒网页上的所有链接

时间:2012-11-29 12:50:00

标签: javascript google-chrome-extension

我是Google Chrome扩展程序的新手。我(特别)打算制作一个browser action扩展程序,点击后会打开页面上的所有链接(网址),当我们Google Search任何主题时。 到现在为止,我能够制作 highlights alerts (javascript alert function )链接在任何网页上 所需的manifest.jsonscript文件如下: -


manifest.json==>

{
  "name": "Highlight then alert.",
  "description": "Highlight and alert links on a webpage.",
  "version": "1.1",
  "background": {
    "scripts": ["write.js"]
  },
  "permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],
  "browser_action": {
      "default_title": "highlighter",
      "default_icon": "highlight_16x16.png"
  },
  "manifest_version": 2
}

write.js==>

chrome.browserAction.onClicked.addListener(function(tab) {

var action_url1 = "javascript:(function() {
{1}}


{for(i=0;i<document.links.length;i++)alert(document.links[i]);})();"; {
{1}}


var action_url2 = "javascript:(function()
{for(i=0;i<document.links.length;i++)document.links[i].style.backgroundColor='#F00';})();";
chrome.tabs.update(tab.id, {url: action_url1});


现在,我正在编写一个简单的chrome.tabs.update(tab.id, {url: action_url2}); javascript代码,将它们存储在所示的不同变量中,然后将它们传递到Chrome对象方法中 - }); 执行inlinechrome.tabs.update()操作 我需要通过编写绝对javascript函数而不是通过变量来找出执行相同操作的方法 建议?

2 个答案:

答案 0 :(得分:3)

示例骷髅会突出显示Google搜索扩展程序中的所有链接,您可以根据需要自定义。如果您需要更多信息,请告诉我

enter image description here

<强> 的manifest.json

{
  "name": "Show Links",
  "description": "Show links in a page",
  "version": "0.1",
  "minimum_chrome_version": "16.0.884",
  "permissions": [
    "experimental", "tabs", "downloads","<all_urls>"
  ],
  "browser_action": {
    "default_icon": "icon.jpg",
    "default_popup": "popup.html"
  },
  "manifest_version": 2
}

<强> popup.html

<html>
<head>
<script src='popup.js'></script>
</head>
<body>
<table id='links'>
  <tr>
    <th><input type='checkbox' checked id='toggle_all'></th>
  </tr>
</table>
</body>
</html>

<强> popup.js

var allLinks = [];
var visibleLinks = [];

// Display all visible links.
function showLinks() {
  var linksTable = document.getElementById('links');
  while (linksTable.children.length > 1) {
    linksTable.removeChild(linksTable.children[linksTable.children.length - 1])
  }
  for (var i = 0; i < visibleLinks.length; ++i) {
    var row = document.createElement('tr');
    var col0 = document.createElement('td');
    var col1 = document.createElement('td');
    var checkbox = document.createElement('input');
    checkbox.checked = true;
    checkbox.type = 'checkbox';
    checkbox.id = 'check' + i;
    col0.appendChild(checkbox);
    col1.innerText = visibleLinks[i];
    col1.style.whiteSpace = 'nowrap';
    col1.onclick = function() {
      checkbox.checked = !checkbox.checked;
    }
    row.appendChild(col0);
    row.appendChild(col1);
    linksTable.appendChild(row);
  }
 }



// Add links to allLinks and visibleLinks, sort and show them.  send_links.js is
// injected into all frames of the active tab, so this listener may be called
// multiple times.
chrome.extension.onMessage.addListener(function(links) {
  for (var index in links) {
    allLinks.push(links[index]);
  }
  allLinks.sort();
  visibleLinks = allLinks;
  //console.log(links);
  showLinks();
});

// Set up event handlers and inject send_links.js into all frames in the active
// tab.
window.onload = function() {


  chrome.windows.getCurrent(function (currentWindow) {
    chrome.tabs.query({active: true, windowId: currentWindow.id},
                      function(activeTabs) {
      chrome.tabs.executeScript(
        activeTabs[0].id, {file: 'send_links.js', allFrames: true});
    });
  });
};

<强> send_links.js

// Send back to the popup a sorted deduped list of valid link URLs on this page.
// The popup injects this script into all frames in the active tab.
console.log("Injected");

var links = [].slice.apply(document.getElementsByTagName('a'));
console.log(links);
links = links.map(function(element) {
  // Return an anchor's href attribute, stripping any URL fragment (hash '#').
  // If the html specifies a relative path, chrome converts it to an absolute
  // URL.
  var href = element.href;
  var hashIndex = href.indexOf('#');
  if (hashIndex >= 0) {
    href = href.substr(0, hashIndex);
  }
  return href;
});

links.sort();

// Remove duplicates and invalid URLs.
var kBadPrefix = 'javascript';
for (var i = 0; i < links.length;) {

  if ((((i > 0) && (links[i] == links[i - 1])) ||
      (links[i] == '') ||
      (kBadPrefix == links[i].toLowerCase().substr(0, kBadPrefix.length))) ) {
    links.splice(i, 1);
  } else {
    ++i;
  }
}
console.log(links);

chrome.extension.sendMessage(links);

答案 1 :(得分:1)

有点不确定你想要什么,但我的猜测是你想要一种方法在浏览器上点击你的测试代码点击没有你现在正在做的书签样式。
如果是这样,我就是这样做的。
manifest.json

{
  "name": "Testing from a PopUp",
  "version": "1.0",
  "permissions": [
    "tabs", "<all_urls>"
  ],
  "browser_action": {
      "default_title": "Inject Test Script",
      "default_icon": "icon.png",
      "default_popup": "popup.html"
  },
  "manifest_version":2
}

popup.html

<!doctype html>
<html>
  <head>
    <script src="popup.js"></script>
  </head>
  <body>
  </body>
</html>

popup.js

chrome.tabs.executeScript(null,{file:"injectedCode.js"});
window.close();

injectCode.js

log = function() {
    for(i = 0; i < document.links.length; i++) console.debug(document.links[i]);
}

highlight = function() {
    var links = document.querySelectorAll('div[id="search"] ol li h3 a');
    for(var i = 0,length=links.length>10 ? 10 : links.length; i < length; i++) links[i].style.backgroundColor = '#F00';
}

log();
highlight();

你应该查找的是executeScript
http://developer.chrome.com/extensions/tabs.html#method-executeScript
就个人而言,我喜欢这种方式来测试一点点,然后再将它们转换成内容脚本。