0

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

内容.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});

背景.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();
        });
    });
});

有谁知道为什么输入标签值作为空白传递?

4

1 回答 1

1

因为您正在尝试使用current[z].innerText. 但是,您必须使用current[z].value输入。

于 2013-06-26T06:30:40.377 回答