Skip to content

Instantly share code, notes, and snippets.

@Stanback
Last active August 29, 2015 14:00
Show Gist options
  • Save Stanback/11068255 to your computer and use it in GitHub Desktop.
Save Stanback/11068255 to your computer and use it in GitHub Desktop.
PhantomJS test of the XContentReady event
/*
* Simple test of PhantomJS and the XContentReady event.
*
* This is the basic pattern used for the ember-prerender project: https://github.com/zipfworks/ember-prerender
* More info on the XContentReady event: https://github.com/n-fuse/the-XContentReady-Event/
*
* Usage: phantomjs phantom_test.js
*/
var page = require('webpage').create();
page.onInitialized = function() {
console.log("Initialized the page");
page.evaluate(function() {
document.addEventListener('XContentReady', function() {
window.callPhantom("Received XContentReady event!");
}, false);
});
};
page.onCallback = function(msg) {
console.log("Phantom:", msg);
phantom.exit(0);
};
page.onConsoleMessage = function(msg) {
console.log("Page:", msg);
};
page.content = "<html>\n" +
" <body>\n" +
" <script>\n" +
" var e;\n" +
" //e = new CustomEvent('XContentReady', { bubbles: true, cancelable: false });\n" +
" e = document.createEvent('Event');\n" +
" e.initEvent('XContentReady', true, false);\n" +
" console.log(\"Dispatching XContentReady event...\");\n" +
" document.dispatchEvent(e);\n" +
" </script>\n" +
" </body>\n" +
"</html>";
@Stanback
Copy link
Author

PhantomJS (as of version 1.9.7) doesn't seem to know about CustomEvent.

Using document.createEvent(...) and event.initEvent(...) works as expected.

@vanthome
Copy link

Very helpful!

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