Skip to content

Instantly share code, notes, and snippets.

@n1k0
Last active August 5, 2016 04:58

Revisions

  1. n1k0 renamed this gist Dec 28, 2012. 1 changed file with 1 addition and 1 deletion.
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # A CasperJS link checker daemon
    # A CasperJS link checker

    A simple [CasperJS](http://casperjs.org/) script to periodically check for a link and process accordingly.

  2. n1k0 revised this gist Dec 28, 2012. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions casperjs-link-checker-daemon.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # A CasperJS link checker daemon

    A simple [CasperJS](http://casperjs.org/) script to periodically check for a link and process accordingly.

    ```js
  3. n1k0 renamed this gist Dec 28, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. n1k0 revised this gist Dec 28, 2012. 1 changed file with 33 additions and 0 deletions.
    33 changes: 33 additions & 0 deletions usage.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,36 @@
    A simple [CasperJS](http://casperjs.org/) script to periodically check for a link and process accordingly.

    ```js
    /*jshint strict:false*/
    /*global CasperError console phantom require*/
    var casper = require('casper').create({
    logLevel: "debug",
    verbose: true
    }),
    interval = 1000 * 60, // 1 minute
    url = 'http://google.com/'; // URL to check

    casper.checkLink = function checkLink() {
    this.echo('Setting up check steps...');
    this.open(url).then(function() {
    if (this.exists('#selector-to-your-link')) {
    this.echo('link was found, clicking');
    this.thenClick('#selector-to-your-link').then(function() {
    // add stuff to be done
    // ... then exit
    this.echo('Done.').exit();
    });
    } else {
    this.warn('Link not found, scheduling another attempt');
    this.wait(interval);
    }
    });
    this.run(this.checkLink);
    };

    casper.start().checkLink();
    ```

    Sample output:

    ```
  5. n1k0 revised this gist Dec 28, 2012. 2 changed files with 61 additions and 10 deletions.
    53 changes: 53 additions & 0 deletions usage.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    Sample output:

    ```
    $ casperjs waiter.js
    [info] [phantom] Starting...
    Setting up check steps...
    [debug] [phantom] opening url: http://google.com/, HTTP GET
    [debug] [phantom] Navigation requested: url=http://google.com/, type=Other, lock=true, isMainFrame=true
    [info] [phantom] Running suite: 1 step
    [debug] [phantom] Navigation requested: url=http://www.google.com/, type=Other, lock=true, isMainFrame=true
    [debug] [phantom] Navigation requested: url=http://www.google.fr/, type=Other, lock=true, isMainFrame=true
    [debug] [phantom] url changed to "http://www.google.fr/"
    [debug] [phantom] Successfully injected Casper client-side utilities
    [info] [phantom] Step 1/1 http://www.google.fr/ (HTTP 301)
    [warning] [phantom] Link not found, scheduling another attempt
    ⚠  Link not found, scheduling another attempt
    [info] [phantom] Step 1/1: done in 1265ms.
    [info] [phantom] Step 2/2 http://www.google.fr/ (HTTP 301)
    [info] [phantom] Step 2/2: done in 1364ms.
    [info] [phantom] wait() finished waiting for 10000ms.
    [info] [phantom] Done 2 steps in 11387ms
    Setting up check steps...
    [debug] [phantom] opening url: http://google.com/, HTTP GET
    [debug] [phantom] Navigation requested: url=http://google.com/, type=Other, lock=true, isMainFrame=true
    [info] [phantom] Running suite: 3 steps
    [debug] [phantom] Navigation requested: url=http://www.google.com/, type=Other, lock=true, isMainFrame=true
    [debug] [phantom] Navigation requested: url=http://www.google.fr/, type=Other, lock=true, isMainFrame=true
    [debug] [phantom] url changed to "http://www.google.fr/"
    [debug] [phantom] Successfully injected Casper client-side utilities
    [info] [phantom] Step 3/3 http://www.google.fr/ (HTTP 301)
    [warning] [phantom] Link not found, scheduling another attempt
    ⚠  Link not found, scheduling another attempt
    [info] [phantom] Step 3/3: done in 11896ms.
    [info] [phantom] Step 4/4 http://www.google.fr/ (HTTP 301)
    [info] [phantom] Step 4/4: done in 11992ms.
    [info] [phantom] wait() finished waiting for 10000ms.
    [info] [phantom] Done 4 steps in 22015ms
    Setting up check steps...
    [debug] [phantom] opening url: http://google.com/, HTTP GET
    [debug] [phantom] Navigation requested: url=http://google.com/, type=Other, lock=true, isMainFrame=true
    [info] [phantom] Running suite: 5 steps
    [debug] [phantom] Navigation requested: url=http://www.google.com/, type=Other, lock=true, isMainFrame=true
    [debug] [phantom] Navigation requested: url=http://www.google.fr/, type=Other, lock=true, isMainFrame=true
    [debug] [phantom] url changed to "http://www.google.fr/"
    [debug] [phantom] Successfully injected Casper client-side utilities
    [info] [phantom] Step 5/5 http://www.google.fr/ (HTTP 301)
    [warning] [phantom] Link not found, scheduling another attempt
    ⚠  Link not found, scheduling another attempt
    [info] [phantom] Step 5/5: done in 22525ms.
    [info] [phantom] Step 6/6 http://www.google.fr/ (HTTP 301)
    [info] [phantom] Step 6/6: done in 22622ms.
    ^C
    ```
    18 changes: 8 additions & 10 deletions waiter.js
    Original file line number Diff line number Diff line change
    @@ -4,12 +4,12 @@ var casper = require('casper').create({
    logLevel: "debug",
    verbose: true
    }),
    interval = 1000 * 60, // 1 minute
    url = 'http://google.com/';
    interval = 1000 * 60, // 1 minute
    url = 'http://google.com/'; // URL to check

    function checkLink() {
    casper.checkLink = function checkLink() {
    this.echo('Setting up check steps...');
    this.thenOpen(url, function() {
    this.open(url).then(function() {
    if (this.exists('#selector-to-your-link')) {
    this.echo('link was found, clicking');
    this.thenClick('#selector-to-your-link').then(function() {
    @@ -18,13 +18,11 @@ function checkLink() {
    this.echo('Done.').exit();
    });
    } else {
    this.echo('Link not found, scheduling another attempt');
    this.warn('Link not found, scheduling another attempt');
    this.wait(interval);
    }
    });
    this.run(checkLink);
    }
    this.run(this.checkLink);
    };

    casper.start().then(function() {
    this.echo('Daemon started');
    }).run(checkLink);
    casper.start().checkLink();
  6. n1k0 revised this gist Dec 28, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion waiter.js
    Original file line number Diff line number Diff line change
    @@ -23,7 +23,7 @@ function checkLink() {
    }
    });
    this.run(checkLink);
    };
    }

    casper.start().then(function() {
    this.echo('Daemon started');
  7. n1k0 revised this gist Dec 28, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion waiter.js
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ var casper = require('casper').create({
    interval = 1000 * 60, // 1 minute
    url = 'http://google.com/';

    var checkLink = function checkLink() {
    function checkLink() {
    this.echo('Setting up check steps...');
    this.thenOpen(url, function() {
    if (this.exists('#selector-to-your-link')) {
  8. n1k0 revised this gist Dec 28, 2012. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions waiter.js
    Original file line number Diff line number Diff line change
    @@ -8,22 +8,20 @@ var casper = require('casper').create({
    url = 'http://google.com/';

    var checkLink = function checkLink() {
    this.echo('Setting up check...');
    this.echo('Setting up check steps...');
    this.thenOpen(url, function() {
    var found = this.exists('#selector-to-your-link');
    this.echo(found ? 'link was found' : 'link not found');
    if (found) {
    if (this.exists('#selector-to-your-link')) {
    this.echo('link was found, clicking');
    this.thenClick('#selector-to-your-link').then(function() {
    // add stuff to be done
    // ... then exit
    this.echo('Done.').exit();
    });
    } else {
    this.echo('Scheduling another attempt');
    this.echo('Link not found, scheduling another attempt');
    this.wait(interval);
    }
    });
    this.echo('Check steps added');
    this.run(checkLink);
    };

  9. n1k0 revised this gist Dec 28, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion waiter.js
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,6 @@ var casper = require('casper').create({
    var checkLink = function checkLink() {
    this.echo('Setting up check...');
    this.thenOpen(url, function() {
    this.echo(this.getCurrentUrl());
    var found = this.exists('#selector-to-your-link');
    this.echo(found ? 'link was found' : 'link not found');
    if (found) {
  10. n1k0 revised this gist Dec 28, 2012. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions waiter.js
    Original file line number Diff line number Diff line change
    @@ -5,24 +5,22 @@ var casper = require('casper').create({
    verbose: true
    }),
    interval = 1000 * 60, // 1 minute
    found = false,
    url = 'http://google.com/';

    var checkLink = function checkLink() {
    this.echo('Setting up check...');
    this.thenOpen(url, function() {
    this.echo(this.getCurrentUrl());
    found = this.exists('#selector-to-your-link');
    var found = this.exists('#selector-to-your-link');
    this.echo(found ? 'link was found' : 'link not found');
    });
    this.then(function() {
    if (found) {
    this.thenClick('#selector-to-your-link').then(function() {
    // add stuff to be done
    // ... then exit
    this.echo('Done.').exit();
    });
    } else {
    this.echo('Scheduling another attempt');
    this.wait(interval);
    }
    });
  11. n1k0 revised this gist Dec 27, 2012. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion waiter.js
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,6 @@ var checkLink = function checkLink() {
    }
    });
    this.echo('Check steps added');
    this.echo(this.steps.length);
    this.run(checkLink);
    };

  12. n1k0 revised this gist Dec 27, 2012. 1 changed file with 5 additions and 3 deletions.
    8 changes: 5 additions & 3 deletions waiter.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ var casper = require('casper').create({
    url = 'http://google.com/';

    var checkLink = function checkLink() {
    this.echo('checkLink() called');
    this.echo('Setting up check...');
    this.thenOpen(url, function() {
    this.echo(this.getCurrentUrl());
    found = this.exists('#selector-to-your-link');
    @@ -19,16 +19,18 @@ var checkLink = function checkLink() {
    if (found) {
    this.thenClick('#selector-to-your-link').then(function() {
    // add stuff to be done
    // ... then exit
    this.echo('Done.').exit();
    });
    } else {
    this.wait(interval);
    }
    });
    this.echo('checkLink() end');
    this.echo('Check steps added');
    this.echo(this.steps.length);
    this.run(checkLink);
    };

    casper.start().then(function() {
    this.echo('then');
    this.echo('Daemon started');
    }).run(checkLink);
  13. n1k0 revised this gist Dec 27, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions waiter.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ var casper = require('casper').create({
    logLevel: "debug",
    verbose: true
    }),
    interval = 1000 * 60 * 1,
    interval = 1000 * 60, // 1 minute
    found = false,
    url = 'http://google.com/';

    @@ -21,7 +21,7 @@ var checkLink = function checkLink() {
    // add stuff to be done
    });
    } else {
    this.wait(interval); // 5 more minutes
    this.wait(interval);
    }
    });
    this.echo('checkLink() end');
  14. n1k0 created this gist Dec 27, 2012.
    34 changes: 34 additions & 0 deletions waiter.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    /*jshint strict:false*/
    /*global CasperError console phantom require*/
    var casper = require('casper').create({
    logLevel: "debug",
    verbose: true
    }),
    interval = 1000 * 60 * 1,
    found = false,
    url = 'http://google.com/';

    var checkLink = function checkLink() {
    this.echo('checkLink() called');
    this.thenOpen(url, function() {
    this.echo(this.getCurrentUrl());
    found = this.exists('#selector-to-your-link');
    this.echo(found ? 'link was found' : 'link not found');
    });
    this.then(function() {
    if (found) {
    this.thenClick('#selector-to-your-link').then(function() {
    // add stuff to be done
    });
    } else {
    this.wait(interval); // 5 more minutes
    }
    });
    this.echo('checkLink() end');
    this.echo(this.steps.length);
    this.run(checkLink);
    };

    casper.start().then(function() {
    this.echo('then');
    }).run(checkLink);