开发者

document is undefined in Google URL harvester

I have code taken from Google to harvest URLs from Google. I saved it as filename.js. When I run the file it showed "'document' is undefined". The part of the code which is showing problem is

// ==UserScript==
// @name           Google URL Harvester
// @namespace      http://userscripts.org/scripts/show/42998
// @description    Harvests URLs from a Google Search
// @include        http://www.google.co.uk/
// @include        http://www.google.com/
// ==/UserScript==

var btn_container;
var inputs = document.getElementsByTagName("input");
for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].name == "btnG")
        btn_container = inputs[i].parentNode;
}

function find_next_link(html) {
    var url = html.match(/(<a href="[^"]+">)\s*<span[^>]+style="[^"]*background-position:\s?-76px\s/);
    if (url == null)
        return false;

    var div = document.createElement("div");
    div.innerHTML = url[1];
    return div.firstChild.href;
}

function harvest(query_url, callback) {
    ajax(query_url, function(e){
        var als = e.match(/<a[^>]+class=l[^>]*>/g);
        for (var i = 0; i < als.length; i++) {
            urls.push(als[i].match(/href="([^"]+)"/)[1]);
        }
        var next_url = find_next_link(e);
        if (next_url)
            harvest(next_url, callback);
        else
            callback();
    });
}

function ajax(url, callback) {
    var req = new XMLHttpRequest();
    req.onreadystatechange = function() {
        if (req.readyState == 4 && req.status == 200) {
            callback(req.responseText);
        }
    }
  开发者_StackOverflow  req.open("GET", url, true);
    req.send("");
}

var new_button = document.createElement("input");
new_button.type = "button";
new_button.value = "Harvest URLs";
new_button.setAttribute("onsubmit", "return false;");
btn_container.appendChild(new_button);
var urls = [];
new_button.addEventListener("click", function(){
    var query_url = unsafeWindow.document.forms[0].action + "?num=100&q="+escape(unsafeWindow.document.forms[0].q.value);
    document.body.innerHTML = "<img src='http://oneworldwebsites.com/images/wheel%20throbber.gif' />";
    harvest(query_url, function() {
        document.body.innerHTML = urls.join("<br/>");
    });
},false);

Here I have not defined document(if it is necessary). Can anybody please rectify the error in this code. Operating system is Windows 7.

I am saving this document to my desktop as harv.js and running it. Am I doing anything wrong?


Google like most websites updates their structure over time.

for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].name == "btnG")
        btn_container = inputs[i].parentNode;
}

Needs to become

for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].name == "btnK") //<<------G to K
        btn_container = inputs[i].parentNode;
}


I can just guess that the error is thrown because you try to access the "input" element, before creating it later. Moreover you shouldn't use a html element as a identifier in JavaScript. You could also use Firebug to pinpoint the error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜