Skip to content

Instantly share code, notes, and snippets.

@papoms
Created August 24, 2012 16:10
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save papoms/3452369 to your computer and use it in GitHub Desktop.
Save papoms/3452369 to your computer and use it in GitHub Desktop.
Fake Referrer with CasperJS
var fakeReferrer = "http://porzky.com"
var targetUrl = "http://keyworddomains.com"
var casper = require('casper').create();
casper.start(fakeReferrer, function() {
this.echo(this.getCurrentUrl());
});
casper.then(function(){
this.evaluate(function(){
var link = document.createElement('a');
link.setAttribute('href', 'http://keyworddomains.com');
link.setAttribute('id', "myTargetUrl")
document.body.appendChild(link);
});
});
casper.then(function() {
this.click('a#myTargetUrl');
});
casper.run(function() {
this.echo(this.getCurrentUrl());
});
@LeCoupa
Copy link

LeCoupa commented Mar 30, 2014

Thank you! This works for me, I have convert your code to coffeescript and pass the targetUrl variable to the evaluate function. https://gist.github.com/LeCoupa/9873864

@tamimibrahim
Copy link

Hi, how can i use 'targetUrl' variable inside this.evaluate function ?? like this way -->

var fakeReferrer = "http://porzky.com"
var targetUrl = "http://keyworddomains.com"

var casper = require('casper').create();

casper.start(fakeReferrer, function() {
    this.echo(this.getCurrentUrl());
});

casper.then(function(){
    this.evaluate(function(){
        var link = document.createElement('a');
        link.setAttribute('href', targetUrl);
        link.setAttribute('id', "myTargetUrl")
        document.body.appendChild(link);
    });
});

casper.then(function() {
        this.click('a#myTargetUrl');
});


casper.run(function() {
        this.echo(this.getCurrentUrl());
});

@davidgarsan
Copy link

davidgarsan commented Dec 7, 2016

Yo have to pass it:

...

casper.thenEvaluate(function(targetUrl){
    var link = document.createElement('a');
    link.setAttribute('href', targetUrl);
    link.setAttribute('id', "myTargetUrl")
    document.body.appendChild(link);
}, targetUrl);

...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment