1

我是编写 Google Chrome 扩展程序的新手。我(特别是)打算做一个browser action扩展,点击它会打开页面上的所有链接(URL),当我们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()
{for(i=0;i<document.links.length;i++)alert(document.links[i]);})();";


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});
});


现在,我正在编写一个简单的inlinejavascript 代码,将它们存储在不同的变量中,如图所示,然后将它们传递给 Chrome 对象方法
-chrome.tabs.update()以执行highlightandalert操作。
我需要找出一种方法来通过编写一个绝对的 javascript 函数而不是传递一个变量来执行相同的操作。
建议?

4

2 回答 2

3

示例骨架将突出显示 Google 搜索扩展中的所有链接,您可以根据需要对其进行自定义。如果您需要更多信息,请与我们联系

在此处输入图像描述

清单.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);
于 2012-11-29T14:09:58.327 回答
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();

注入代码.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
就 我个人而言,我喜欢这种在将其转换为内容脚本之前测试小位的方法。

于 2012-11-29T14:22:32.597 回答