可以刷新网站,直到用 Javascript 进行更改吗?

发布于 2024-10-17 15:50:59 字数 632 浏览 1 评论 0原文

我是一个相对较新的编码员,在询问之前已经在此处和谷歌上进行了一些搜索,但我却一无所获。

我想知道是否有一种方法可以用 Javascript 创建脚本,并让它刷新页面,直到通过将页面输入到字符串中来更改页面。

类似的东西(请原谅那些糟糕的伪代码,但我意识到需要围绕此编写一些函数):

Start

currentPage = webclient.getPage("www.somesite.com")
boolean diff = false
string pageText = currentPage.astext()

do {
   currentPage.refresh()
} until (currentPage.astext() != pageText)

string alert = "Change Found"
string address = "[email protected]"
e-mail(address,alert)

END

感谢任何人提供的任何帮助,任何人都可以为此提供新的编码器:)

I am a relatively new coder and have been searching around a bit on here and on google before asking but I have come up empty.

I was wondering if there was a way to create a script in say Javascript, and have it refresh a page until there is a change on the page by inputting the page into a string.

Something along the lines of (excuse the soddy psuedo-code but I realise that some functions would need to be written around this):

Start

currentPage = webclient.getPage("www.somesite.com")
boolean diff = false
string pageText = currentPage.astext()

do {
   currentPage.refresh()
} until (currentPage.astext() != pageText)

string alert = "Change Found"
string address = "[email protected]"
e-mail(address,alert)

END

Thanks for any help anyone can offer a new coder on this :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盗琴音 2024-10-24 15:50:59

PHP 似乎更适合这种操作。这是我要做的:

  • 使用 cURL 获取页面内容
  • 稍等一下(例如 1 分钟) *
  • 再次获取页面内容
  • 比较两个页面内容
  • 电子邮件(如果有更改)并重新开始

* 您不想像伪代码一样刷新页面,因为您的脚本会消耗大量带宽,并且很可能使您的目标网站饱和。

您需要代码示例吗?

编辑
这是我正在运行的 PHP 脚本:

<?php
////////////////
// Parameters //
////////////////
$url = 'http://www.example.com';
$sleepyTime = 60; // seconds
$recipient = '[email protected]';
$subject = 'Change found';
$message = 'Change found in ' . $url;

////////////////
// Functions  //
////////////////
function fetchWebsiteContent($url) {
    // init curl handle
    $ch = curl_init($url);

    // Tells curl to return the content
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // fetch content
    $res = curl_exec($ch);

    // close handle and return content
    curl_close($ch);
    return $res;
}

////////////////////
// The comparison //
////////////////////
$firstContent = fetchWebsiteContent($url);

// This is an endless checking scope
while (1) {
    // sleep a bit and fetch website content again
    sleep($sleepytime);
    $secondContent = fetchWebsiteContent($url);

    // check if change occured
    if ($firstContent == $secondContent) {
        mail($recipient, $subject, $message);
    }

    $firstContent = $secondContent;
}
?>

有用的资源:
cURL 手册
邮件手册

希望你喜欢它;)

PHP seems better suited for this kind of operation. Here is what I would do:

  • Fetch the page content with cURL
  • Wait a bit (e.g. 1 min) *
  • Fetch the page content again
  • Compare both page contents
  • e-mail if there is a change and start over anyway

* You don't want to refresh the page as much as your pseudo-code does, as your script would eat a lot of bandwidth and would be most likely to saturate your targeted website.

Do you need code samples ?

EDIT
Here's my working PHP script:

<?php
////////////////
// Parameters //
////////////////
$url = 'http://www.example.com';
$sleepyTime = 60; // seconds
$recipient = '[email protected]';
$subject = 'Change found';
$message = 'Change found in ' . $url;

////////////////
// Functions  //
////////////////
function fetchWebsiteContent($url) {
    // init curl handle
    $ch = curl_init($url);

    // Tells curl to return the content
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // fetch content
    $res = curl_exec($ch);

    // close handle and return content
    curl_close($ch);
    return $res;
}

////////////////////
// The comparison //
////////////////////
$firstContent = fetchWebsiteContent($url);

// This is an endless checking scope
while (1) {
    // sleep a bit and fetch website content again
    sleep($sleepytime);
    $secondContent = fetchWebsiteContent($url);

    // check if change occured
    if ($firstContent == $secondContent) {
        mail($recipient, $subject, $message);
    }

    $firstContent = $secondContent;
}
?>

Helpful ressources:
cURL manual
mail manual

Hope you like it ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文