postmessage.js 932 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. simple postmessage plugin
  3. Useful when a reveal slideshow is inside an iframe.
  4. It allows to call reveal methods from outside.
  5. Example:
  6. var reveal = window.frames[0];
  7. // Reveal.prev();
  8. reveal.postMessage(JSON.stringify({method: 'prev', args: []}), '*');
  9. // Reveal.next();
  10. reveal.postMessage(JSON.stringify({method: 'next', args: []}), '*');
  11. // Reveal.slide(2, 2);
  12. reveal.postMessage(JSON.stringify({method: 'slide', args: [2,2]}), '*');
  13. Add to the slideshow:
  14. dependencies: [
  15. ...
  16. { src: 'plugin/postmessage/postmessage.js', async: true, condition: function() { return !!document.body.classList; } }
  17. ]
  18. */
  19. (function (){
  20. window.addEventListener( "message", function ( event ) {
  21. var data = JSON.parse( event.data ),
  22. method = data.method,
  23. args = data.args;
  24. if( typeof Reveal[method] === 'function' ) {
  25. Reveal[method].apply( Reveal, data.args );
  26. }
  27. }, false);
  28. }());