Search code examples
javascriptphantomjscasperjsbookmarklet

PhantomJS or CasperJS open page and execute bookmarklet


Is it possible to open a web page with PhantomJS or CasperJS and then run a bookmarklet on that page?

I would like to get the DOM of this result to check if the bookmarklet was loaded correctly.

I've already searched on the web and tried many things but found no solution.

Thanks in advance.


Solution

  • As far as I'm aware, I do not believe that PhantomJS or CasperJS uses the javascript: protocol that bookmarklets use.

    Alternatively, you can use casper.evaluate() to execute the bookmarklet contents in the webpage:

    casper.evaluate(function () {
      // Insert your bookmarklet code here
    });
    

    You can get the DOM of the resulting webpage afterwards with casper.getHTML().