在URL中传递页面抓取的数据

passing page scraped data in the URL

本文关键字:抓取 数据 URL      更新时间:2023-09-26

在我的Chrome扩展中,我试图从当前选项卡(content.js中)中抓取信息,并将其作为参数发送到提供的URL(background.js)。似乎我可以从选项卡中抓取所有内容,并将它附加到URL中,除了输入标记的值。这是我的代码:

content.js:

var elements = new Array("form","h1","input","td","textarea","time","title","var");
//declare an array for found elements
var foundElements = new Array();
//declare an array for found ids
var foundIds = new Array();
//this counter is used to hold positions in the element array.
var elementCounter = 0;
//this counter is used to hold positions in the foundIds array
var idsCounter = 0;
//this counter is used to hold positions in the classCounter array.
var classCounter = 0;
//and we're going to output everything in a giantic string.
var output = "URL=" + document.URL;
//scrape the page for all elements
for (var i = 0; i < elements.length; i++)
{
    var current = document.getElementsByTagName(elements[i]);
    if(current.length>0)
    {
        for (var z=0; z<current.length; z++)
        {
            var inTxt = current[z].innerText;
            output += "&" + elements[i] + "=" + inTxt;
        }
        elementCounter++;
        //now that we have an array of a tag, check it for IDs and classes.
        for (var y = 0; y<current.length; y++)
        {
            //check to see if the element has an id
            if(current[y].id)
            {
                //these should be unique
            var hit = false;
            for (var x = 0; x<foundIds.length; x++)
            {
                if(foundIds[x]==current[y].id)
                {
                    hit=true;
                }
            }
            //if there was no hit...
            if(!hit)
            {
                foundIds[idsCounter]=current[y].id;
                idsCounter++;
                var currVal = current[y].value;
                output+="&" + current[y].id + "=" + currVal;
            }
                    }
                    //now we pull the classes
            var classes = current[y].classList;
            if(classes.length>0)
            {
                for (var x = 0; x<classes.Length; x++)
                {
                    var hit = false;
                    for (var z = 0; z<foundClasses.length; z++)
                    {
                        if(foundClasses[z]==classes[x])
                        {
                            hit=true;
                        }
                    }
                    //if there was not a hit
                    if(!hit)
                    {
                        foundClasses[classCounter]=classes[x];
                        classCounter++;
                        output+="&" + classes[x] + "=";
                    }
                }
            }
        }
    }
}
chrome.runtime.sendMessage({data: output});

background.js:

var output2;
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
    output2 = "text_input1=";
    output2 += request.data;
});
chrome.browserAction.onClicked.addListener(function() {
    chrome.tabs.create({url: "http://www.google.com?" + output2}, function(tab) {
        chrome.tabs.executeScript(tab.id, {file: "content.js"}, function() {
            sendMessage();
        });
    });
});

有人知道为什么输入标签的值是空的吗?

因为您正试图使用current[z].innerText获取输入文本。但是,输入必须使用current[z].value