我一直在尝试编写我的第一个chrome扩展程序。我希望此扩展程序能够在网页中获取具有特定格式的所有链接,并从主网页中找到的每个链接的网页中检索某个链接。
我认为问题可能出在消息传递中,您是否会收到一条消息,该消息将启动一组命令,触发您想要接收的另一条消息?
manifest.json:
{
"manifest_version": 2,
"name": "TAU Down",
"description": "This extension downloads whole courses from TAU Moodle website",
"version": "1.0",
"permissions": [
"http://moodle.tau.ac.il/", "downloads", "<all_urls>"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
popup.html:
<!DOCTYPE html>
<head>
<script src='popup.js'></script>
</head>
<body>
<button id=download0>Download All!</button>
<table id=links>
<tr>
<th><input type=checkbox checked id=toggle_all></th>
<th align=left>URL</th>
</tr>
</table>
<button id=download1>Download All!</button>
</body>
</html>
popup.js:
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This extension demonstrates using chrome.downloads.download() to
// download URLs.
var allLinks = [];
var visibleLinks = [];
var count = 0;
// 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);
}
}
// Toggle the checked state of all visible links.
function toggleAll() {
var checked = document.getElementById('toggle_all').checked;
for (var i = 0; i < visibleLinks.length; ++i) {
document.getElementById('check' + i).checked = checked;
}
}
// Download all visible checked links.
function downloadCheckedLinks() {
for (var i = 0; i < visibleLinks.length; ++i) {
if (document.getElementById('check' + i).checked) {
chrome.downloads.download({url: visibleLinks[i]},
function(id) {
});
}
}
window.close();
}
// Re-filter allLinks into visibleLinks and reshow visibleLinks.
function filterLinks() {
if (count == 0){
var filterValue = "moodle tau view resource";}
else{
var filterValue = "mod_resource/content";}
var terms = filterValue.split(' ');
visibleLinks = allLinks.filter(function(link) {
for (var termI = 0; termI < terms.length; ++termI) {
var term = terms[termI];
if (term.length != 0) {
var expected = (term[0] != '-');
if (!expected) {
term = term.substr(1);
if (term.length == 0) {
continue;
}
}
var found = (-1 !== link.indexOf(term));
if (found != expected) {
return false;
}
}
}
return true;
});
}
function ExLinks(){
tempLinks = visibleLinks;
chrome.tabs.create({url: "chrome://newtab", active: false, index: 0}, function(tab){
index = 0;
//for (var index in tempLinks){
chrome.tabs.update(tab.id, {url: tempLinks[index], highlighted: false});
chrome.tabs.executeScript(tab.id, {file: 'send_links.js', allFrames: true});
//tempLinks[index] = visibleLinks[0];
//}
//visibleLinks = tempLinks;
showLinks();
});
}
// 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.onRequest.addListener(function(links) {
chrome.runtime.onConnect.addListener(function(port) {
console.assert(port.name == "links_port");
port.onMessage.addListener(function(links) {
for (var index in links) {
allLinks.push(links[index]);
}
allLinks.sort();
visibleLinks = allLinks;
if(count == 0){
filterLinks();
count++;
ExLinks();
}
else{
//filterLinks();
}
showLinks();
});
});
// Set up event handlers and inject send_links.js into all frames in the active
// tab.
window.onload = function() {
document.getElementById('toggle_all').onchange = toggleAll;
document.getElementById('download0').onclick = downloadCheckedLinks;
document.getElementById('download1').onclick = downloadCheckedLinks;
chrome.runtime.connect();
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:
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// 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.
var links = [].slice.apply(document.getElementsByTagName('a'));
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;
}
}
//chrome.extension.sendRequest(links);
var port = chrome.runtime.connect({name: "links_port"});
port.postMessage(links);