寻找一种使用 javascript 搜索 html 页面的方法
我想做的是在 html 页面中获取特定字符串,并在其后面读取一定数量的字符,并将这些字符呈现在锚标记中。
我遇到的问题是弄清楚如何在页面中搜索我发现的所有与标签或 ID 相关的字符串。也希望将其做成一个greatmonkey脚本供我个人使用。
function createlinks(srchstart,srchend){
var page = document.getElementsByTagName('html')[0].innerHTML;
page = page.substring(srchstart,srchend);
if (page.search("file','http:") != -1)
{
var begin = page.search("file','http:") + 7;
var end = begin + 79;
var link = page.substring(begin,end);
document.body.innerHTML += '<a href="'+link+'">LINK</a> | ';
createlinks(end+1,page.length);
}
};
不幸的是,在找到链接后我想到了它再次循环遍历文档
what I would like to do is to the html page for a specific string and read in a certain amount of characters after it and present those characters in an anchor tag.
the problem I'm having is figuring out how to search the page for a string everything I've found relates to by tag or id. Also hoping to make it a greasemonkey script for my personal use.
function createlinks(srchstart,srchend){
var page = document.getElementsByTagName('html')[0].innerHTML;
page = page.substring(srchstart,srchend);
if (page.search("file','http:") != -1)
{
var begin = page.search("file','http:") + 7;
var end = begin + 79;
var link = page.substring(begin,end);
document.body.innerHTML += '<a href="'+link+'">LINK</a> | ';
createlinks(end+1,page.length);
}
};
what I came up with unfortunately after finding the links it loops over the document again
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
辅助方向
不同的正则表达式函数做不同的事情。您可以按照建议在文档中搜索该字符串,但您必须递归地执行此操作,因为您正在搜索的字符串可能会在多个位置列出。
获取页面中的文本
document.getElementsByTagName('html')[0].innerHTML
$('html').html()
注意:
\n
,因为可能位于您要查找的字符串之间。Assisted Direction
Different regex functions do different things. You could search the document for the string, as suggested, but you'd have to do it recursively, since the string you're searching for may be listed in multiple places.
To Get the Text in the Page
document.getElementsByTagName('html')[0].innerHTML
$('html').html()
Note:
\n
that might want to take out, since one could be between the string you're looking for.好的,在 javascript 中,您已经在 DOM 树中获得了整个文档。您可以通过在 DOM 中递归搜索所需的字符串来搜索字符串。这很简单;我将输入伪代码,因为您想考虑正在使用哪些库(如果有)。
Okay, so in javascript you've got the whole document in the DOM tree. You an search for your string by recursively searching the DOM for the string you want. This is striaghtforward; I'll put in pseudocode because you want to think about what libraries (if any) you're using.