Javascript 如何让我的代码在网站上查看';s编码并选择某个项目?
我想有一个代码,可以搜索通过一个网站的html,找到一个特定的项目,然后打印该项目。在我的代码中,我使用一个名为getPageSource.js的文件来获取html,如果我愿意,我可以打印出整个html。让我在这里发布:Javascript 如何让我的代码在网站上查看';s编码并选择某个项目?,javascript,html,google-chrome-extension,Javascript,Html,Google Chrome Extension,我想有一个代码,可以搜索通过一个网站的html,找到一个特定的项目,然后打印该项目。在我的代码中,我使用一个名为getPageSource.js的文件来获取html,如果我愿意,我可以打印出整个html。让我在这里发布: function DOMtoString(document_root) { var html = '', node = document_root.firstChild; while (node) { switch (node.n
function DOMtoString(document_root) {
var html = '',
node = document_root.firstChild;
while (node) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
html += node.outerHTML;
break;
case Node.TEXT_NODE:
html += node.nodeValue;
break;
case Node.CDATA_SECTION_NODE:
html += '<![CDATA[' + node.nodeValue + ']]>';
break;
case Node.COMMENT_NODE:
html += '<!--' + node.nodeValue + '-->';
break;
case Node.DOCUMENT_TYPE_NODE:
// (X)HTML documents are identified by public identifiers
html += "<!DOCTYPE " + node.name + (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '') + (!node.publicId && node.systemId ? ' SYSTEM' : '') + (node.systemId ? ' "' + node.systemId + '"' : '') + '>\n';
break;
}
node = node.nextSibling;
}
return html;
}
chrome.runtime.sendMessage({
action: "getSource",
source: DOMtoString(document)
});
函数DOMtoString(文档根){
var html='',
node=document_root.firstChild;
while(节点){
开关(node.nodeType){
案例节点。元素节点:
html+=node.outerHTML;
打破
案例节点.TEXT\u节点:
html+=node.nodeValue;
打破
案例节点.CDATA\u节\u节点:
html+='';
打破
案例节点。注释节点:
html+='';
打破
案例节点.文档类型节点:
//(X)HTML文档由公共标识符标识
html+=“简单的html dom可能就是你想要的。当你可以简单地使用document.documentElement.innerHTML时,我不明白DOMtoString
函数的用途。至于你的问题,我也不明白为什么你不能简单地使用document.querySelector(“#email”).outerHTML
。我不明白为什么我不明白。谢谢,我会试试看。:)
<input id= "email" class= "inputtext" type="email" tabindex="1" value= "" name="email" ></input>