如何在javascript中将页面的完整内容作为字符串获取?

时间:2021-03-26 21:12:50

I'm writing a bookmarklet, i.e. a bookmark that contains javascript instead of a URL, and I have some trouble. In fact, I cannot remember how I can get the content of the page as a string, so I can apply a regular expression to find what I want. Can you please help me on this?

我正在写一个书签,即包含javascript而不是URL的书签,我遇到了一些麻烦。事实上,我不记得如何将页面内容作为字符串获取,因此我可以应用正则表达式来查找我想要的内容。你能帮帮我吗?

Before anyone suggests it, I cannot use getElementBy(Id/Name/Tag), because the data I'm looking for is HTML-commented and inside markups, so I don't think that would work.

在任何人建议之前,我不能使用getElementBy(Id / Name / Tag),因为我正在寻找的数据是HTML注释和内部标记,所以我认为这不会起作用。

Thanks.

2 个解决方案

#1


You can access it through:

您可以通过以下方式访问它

document.body.innerHTML

#2


so I can apply a regular expression to find what I want

所以我可以应用正则表达式来找到我想要的东西

Do. Not. Use. Regex. To. Parse. HTML.

做。不。使用。正则表达式。至。解析。 HTML。

Especially when the browser has already parsed it for you! Come ON!

特别是当浏览器已经为你解析它!来吧!

the data I'm looking for is HTML-commented

我正在寻找的数据是HTML评论

You can perfectly well grab comment content out of the DOM. eg.

你可以很好地从DOM中获取评论内容。例如。

<div id="mything"><!-- la la la I'm a big comment --></div>

alert(document.getElementById('mything').firstChild.data);

And if you need to search the DOM for comment elements:

如果您需要在DOM中搜索注释元素:

// Get comment descendents
//
function dom_getComments(parent, recurse) {
    var results= [];
    for (var childi= 0; childi<parent.childNodes.length; childi++) {
        var child= parent.childNodes[childi];
        if (child.nodeType==8) // Node.COMMENT_NODE
            results.push(child);
        else if (recurse && child.nodeType==1) // Node.ELEMENT_NODE
            results= results.concat(dom_getComments(child));
    }
    return results;
}

#1


You can access it through:

您可以通过以下方式访问它

document.body.innerHTML

#2


so I can apply a regular expression to find what I want

所以我可以应用正则表达式来找到我想要的东西

Do. Not. Use. Regex. To. Parse. HTML.

做。不。使用。正则表达式。至。解析。 HTML。

Especially when the browser has already parsed it for you! Come ON!

特别是当浏览器已经为你解析它!来吧!

the data I'm looking for is HTML-commented

我正在寻找的数据是HTML评论

You can perfectly well grab comment content out of the DOM. eg.

你可以很好地从DOM中获取评论内容。例如。

<div id="mything"><!-- la la la I'm a big comment --></div>

alert(document.getElementById('mything').firstChild.data);

And if you need to search the DOM for comment elements:

如果您需要在DOM中搜索注释元素:

// Get comment descendents
//
function dom_getComments(parent, recurse) {
    var results= [];
    for (var childi= 0; childi<parent.childNodes.length; childi++) {
        var child= parent.childNodes[childi];
        if (child.nodeType==8) // Node.COMMENT_NODE
            results.push(child);
        else if (recurse && child.nodeType==1) // Node.ELEMENT_NODE
            results= results.concat(dom_getComments(child));
    }
    return results;
}