Guest User

Untitled

a guest
Nov 18th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. // Needs URLSearchParams polyfilled for IE
  2.  
  3. (function () {
  4. // Selector for the iframe element embedding Readium
  5. var readiumIframe = document.querySelector('iframe');
  6.  
  7. // It's a good idea to focus on the Readium frame.
  8. // This bring it in keyboard focus right away.
  9. readiumIframe.focus();
  10.  
  11. // Used to hook into the iframe hosting Readium
  12. // after it 'unloads' its initial empty document
  13. // and begins to load the actual document.
  14. readiumIframe.contentWindow.addEventListener('unload', function () {
  15. setTimeout(function () {
  16. bindToIframe(readiumIframe);
  17. }, 0);
  18. });
  19.  
  20. function bindToIframe(iframe) {
  21. // Bring the appropriate search (query) params and fragment
  22. // from the top window URL to the iframe window URL
  23. setIframeWindowSearchParams(window, iframe.contentWindow);
  24.  
  25. getReadium(iframe.contentWindow, function (ReadiumSDK, reader) {
  26. reader.on(ReadiumSDK.Events.PAGINATION_CHANGED, function () {
  27. // Execute after all pagination changed event handlers
  28. setTimeout(function () {
  29.  
  30. // Bring the appropriate search (query) params and fragment
  31. // from the iframe window URL to the top window URL
  32. setTopWindowSearchParams(window, iframe.contentWindow);
  33. }, 0);
  34. });
  35. });
  36. }
  37.  
  38. function getReadium(iframeWindow, callback) {
  39. var readiumSdkVar;
  40.  
  41. // Intercept the global ReadiumSDK object assignment using a property.
  42. // This works because this will run before the Readium script is executed.
  43. Object.defineProperty(iframeWindow, 'ReadiumSDK', {
  44. get: function () {
  45. return readiumSdkVar;
  46. },
  47. set: function (ReadiumSDK) {
  48. if (!readiumSdkVar) {
  49. ReadiumSDK.once(ReadiumSDK.Events.READER_INITIALIZED, function (reader) {
  50. callback(ReadiumSDK, reader);
  51. });
  52. }
  53. readiumSdkVar = ReadiumSDK;
  54. }
  55. });
  56. }
  57.  
  58. function setIframeWindowSearchParams(topWindow, iframeWindow) {
  59. iframeWindow.location.hash = topWindow.location.hash;
  60. var iframeUrlSearchParams = new URLSearchParams(iframeWindow.location.search);
  61. var topWindowSearchParams = new URLSearchParams(topWindow.location.search);
  62. if (topWindowSearchParams.has('goto')) {
  63. iframeUrlSearchParams.set('goto', topWindowSearchParams.get('goto'));
  64. }
  65. var newUrl = iframeWindow.location.pathname + '?' + iframeUrlSearchParams.toString() + topWindow.location.hash;
  66. iframeWindow.history.replaceState({}, '', newUrl);
  67. }
  68.  
  69. function setTopWindowSearchParams(topWindow, iframeWindow) {
  70. var iframeUrlSearchParams = new URLSearchParams(iframeWindow.location.search);
  71. if (iframeUrlSearchParams.has('goto')) {
  72. var topWindowSearchParams = new URLSearchParams(topWindow.location.search);
  73. topWindowSearchParams.delete('goto');
  74. var topWindowSearchParamsString = topWindowSearchParams.toString();
  75. if (topWindowSearchParamsString.length) {
  76. topWindowSearchParamsString = topWindowSearchParamsString + '&';
  77. }
  78. topWindowSearchParamsString = topWindowSearchParamsString + 'goto=' + encodeURI(iframeUrlSearchParams.get('goto'));
  79. var newUrl = topWindow.location.pathname + '?' + topWindowSearchParamsString;
  80. topWindow.history.replaceState({}, '', newUrl);
  81. }
  82. }
  83. })();
Add Comment
Please, Sign In to add comment