Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Needs URLSearchParams polyfilled for IE
- (function () {
- // Selector for the iframe element embedding Readium
- var readiumIframe = document.querySelector('iframe');
- // It's a good idea to focus on the Readium frame.
- // This bring it in keyboard focus right away.
- readiumIframe.focus();
- // Used to hook into the iframe hosting Readium
- // after it 'unloads' its initial empty document
- // and begins to load the actual document.
- readiumIframe.contentWindow.addEventListener('unload', function () {
- setTimeout(function () {
- bindToIframe(readiumIframe);
- }, 0);
- });
- function bindToIframe(iframe) {
- // Bring the appropriate search (query) params and fragment
- // from the top window URL to the iframe window URL
- setIframeWindowSearchParams(window, iframe.contentWindow);
- getReadium(iframe.contentWindow, function (ReadiumSDK, reader) {
- reader.on(ReadiumSDK.Events.PAGINATION_CHANGED, function () {
- // Execute after all pagination changed event handlers
- setTimeout(function () {
- // Bring the appropriate search (query) params and fragment
- // from the iframe window URL to the top window URL
- setTopWindowSearchParams(window, iframe.contentWindow);
- }, 0);
- });
- });
- }
- function getReadium(iframeWindow, callback) {
- var readiumSdkVar;
- // Intercept the global ReadiumSDK object assignment using a property.
- // This works because this will run before the Readium script is executed.
- Object.defineProperty(iframeWindow, 'ReadiumSDK', {
- get: function () {
- return readiumSdkVar;
- },
- set: function (ReadiumSDK) {
- if (!readiumSdkVar) {
- ReadiumSDK.once(ReadiumSDK.Events.READER_INITIALIZED, function (reader) {
- callback(ReadiumSDK, reader);
- });
- }
- readiumSdkVar = ReadiumSDK;
- }
- });
- }
- function setIframeWindowSearchParams(topWindow, iframeWindow) {
- iframeWindow.location.hash = topWindow.location.hash;
- var iframeUrlSearchParams = new URLSearchParams(iframeWindow.location.search);
- var topWindowSearchParams = new URLSearchParams(topWindow.location.search);
- if (topWindowSearchParams.has('goto')) {
- iframeUrlSearchParams.set('goto', topWindowSearchParams.get('goto'));
- }
- var newUrl = iframeWindow.location.pathname + '?' + iframeUrlSearchParams.toString() + topWindow.location.hash;
- iframeWindow.history.replaceState({}, '', newUrl);
- }
- function setTopWindowSearchParams(topWindow, iframeWindow) {
- var iframeUrlSearchParams = new URLSearchParams(iframeWindow.location.search);
- if (iframeUrlSearchParams.has('goto')) {
- var topWindowSearchParams = new URLSearchParams(topWindow.location.search);
- topWindowSearchParams.delete('goto');
- var topWindowSearchParamsString = topWindowSearchParams.toString();
- if (topWindowSearchParamsString.length) {
- topWindowSearchParamsString = topWindowSearchParamsString + '&';
- }
- topWindowSearchParamsString = topWindowSearchParamsString + 'goto=' + encodeURI(iframeUrlSearchParams.get('goto'));
- var newUrl = topWindow.location.pathname + '?' + topWindowSearchParamsString;
- topWindow.history.replaceState({}, '', newUrl);
- }
- }
- })();
Add Comment
Please, Sign In to add comment