1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Send back to the popup a sorted deduped list of valid link URLs on this page.
6// The popup injects this script into all frames in the active tab.
7
8var links = [].slice.apply(document.getElementsByTagName('a'));
9links = links.map(function(element) {
10  // Return an anchor's href attribute, stripping any URL fragment (hash '#').
11  // If the html specifies a relative path, chrome converts it to an absolute
12  // URL.
13  var href = element.href;
14  var hashIndex = href.indexOf('#');
15  if (hashIndex >= 0) {
16    href = href.substr(0, hashIndex);
17  }
18  return href;
19});
20
21links.sort();
22
23// Remove duplicates and invalid URLs.
24var kBadPrefix = 'javascript';
25for (var i = 0; i < links.length;) {
26  if (((i > 0) && (links[i] == links[i - 1])) ||
27      (links[i] == '') ||
28      (kBadPrefix == links[i].toLowerCase().substr(0, kBadPrefix.length))) {
29    links.splice(i, 1);
30  } else {
31    ++i;
32  }
33}
34
35chrome.extension.sendRequest(links);
36