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