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.
5 function validateLinks(links, callback) {
7 var pendingRequests = 0;
9 function fetchLink(link) {
10 if (!/^http(s?):\/\//.test(link.href))
12 var xhr = new XMLHttpRequest();
13 xhr.open("HEAD", link.href, true);
14 xhr.onreadystatechange = function() {
15 if (xhr.readyState < xhr.HEADERS_RECEIVED || xhr.processed)
17 if (!xhr.status || xhr.status >= 400) {
21 status: xhr.statusText
26 if (!--pendingRequests) {
27 callback({ total: links.length, badlinks: results });
34 console.error("XHR failed for " + link.href + ", " + e);
38 for (var i = 0; i < links.length; ++i)
42 chrome.extension.onRequest.addListener(function(request, sender, callback) {
43 var tabId = request.tabId;
44 chrome.tabs.executeScript(tabId, { file: "content.js" }, function() {
45 chrome.tabs.sendRequest(tabId, {}, function(results) {
46 validateLinks(results, callback);