var getLinks = function () {
"use strict";
var a = document.getElementsByTagName("a"),
b = a.length,
c = 0,
d = [],
e = "",
f = location.href;
f = f.substring(0, f.lastIndexOf("/"));
for (c = 0; c < b; c += 1) {
e = a[c].getAttribute("href");
if (typeof e === "string" && e.length > 4) {
if (e.charAt(0) === "/" || e.charAt(0) === "?") {
e = f + e;
}
d.push(e);
}
}
return d.join("\n") + "\n" + d.length + " total links";
},
myLinks = getLinks(); //myLinks variable will contain the desired output.
//To output to the console just replace the line with 'return' with this code:
//console.log(d.join("\n") + "\n" + d.length + " total links");
このコードを実行すると、指定されたページのすべてのハイパーリンクのリストが返され、各結果が 1 行に表示されます。
編集: 相対リンクを絶対 URI に変換するようになりました。