如何在html中获取页面源? [英] how to get a page source in html?

查看:125
本文介绍了如何在html中获取页面源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTML和javaScript ..我正在尝试构建chrome扩展程序,该扩展程序将在弹出窗口中显示网站上的一些信息

我需要获取 http://met.guc.edu.eg 的页面源在我的网页上下文中,并使用它来获取一些"li"标签并对其进行一些处理(RegEx)

例如,

在网页中显示学生所修的课程-通过从 http://met选修课程.guc.edu.eg ..并在弹出窗口中很好地显示它们

解决方案

由于尚无真正答案,因此我将发布我使用的方法-我不知道这是否是最好的方法-但这是可行的. /p>

XHR并不是最好的主意的原因是因为它并不总是能够为您提供特定标签的确切来源-这样可以.

content.js


chrome.extension.onRequest.addListener(function(request, sender, callback)
{
    if (request.action == 'getSource')
    {
        callback(document.documentElement.outerHTML);
    }
});

background.html


chrome.tabs.sendRequest(tab.id, {action : 'getSource'}, function(source) {
    console.log(source);
});

I'm using HTML and javaScript .. I'm trying to build a chrome extension , which will display some info from the website in the popup

I need to get the page source of http://met.guc.edu.eg in the context of my web page and use it to get some of the "li" tags and do some work on them ( RegEx )

for example display the courses taken by student in web page -- By taking them from the http://met.guc.edu.eg .. and display them in a nice way in a pop up

解决方案

Since there is no true answer to this yet, I will post the method I use - I do not know if it's the best way - but it works.

The reason XHR may not be the best idea is because it's not always going to give you the exact source of a certain tab - this way will.

content.js


chrome.extension.onRequest.addListener(function(request, sender, callback)
{
    if (request.action == 'getSource')
    {
        callback(document.documentElement.outerHTML);
    }
});

background.html


chrome.tabs.sendRequest(tab.id, {action : 'getSource'}, function(source) {
    console.log(source);
});

这篇关于如何在html中获取页面源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆