admin-assets
3 months ago
admin-pages
11 months ago
ai-assets
2 years ago
default-assets
4 years ago
fancybox
1 year ago
icons
1 year ago
kubio-iframe-loader.html
7 months ago
kubio-logo.svg
4 years ago
maintain-preview-url.js
2 years ago
questioning.jpg
2 years ago
maintain-preview-url.js
117 lines
| 1 | (function (location, document, wpURL) { |
| 2 | function makeRandomString(length) { |
| 3 | let result = ''; |
| 4 | const characters = |
| 5 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 6 | const charactersLength = characters.length; |
| 7 | for (let i = 0; i < length; i++) { |
| 8 | result += characters.charAt( |
| 9 | Math.floor(Math.random() * charactersLength) |
| 10 | ); |
| 11 | } |
| 12 | return result; |
| 13 | } |
| 14 | |
| 15 | const urlKey = 'kubio-preview'; |
| 16 | const kubioRandomKey = 'kubio-random'; |
| 17 | const queryParam = new URLSearchParams(location.search); |
| 18 | |
| 19 | const kubioPreviewUUID = queryParam.get(urlKey); |
| 20 | const kubioRandomValue = queryParam.get(kubioRandomKey); |
| 21 | |
| 22 | const protocolRegExp = /^(?:[a-z]+:|#|\?|\.|\/)/i; |
| 23 | |
| 24 | if (!(kubioPreviewUUID || kubioRandomValue)) { |
| 25 | return; |
| 26 | } |
| 27 | |
| 28 | const baseURL = (window.kubioMaintainPreviewURLBase || location.toString()) |
| 29 | .replace(location.search, '') |
| 30 | .replace('#' + location.hash, '') |
| 31 | .replace(protocolRegExp, '') |
| 32 | .replace(/\/$/, ''); |
| 33 | |
| 34 | const keepAliveCurrentUrl = function (root) { |
| 35 | const elements = Array.from(root.querySelectorAll('a')); |
| 36 | |
| 37 | if (root.nodeName.toLowerCase() === 'a') { |
| 38 | elements.push(root); |
| 39 | } |
| 40 | |
| 41 | elements.forEach(function (link) { |
| 42 | // use get attr instead of .href to get the actual attribute value instead of the computed one |
| 43 | let href = link.href; // link.getAttribute('href') || ''; |
| 44 | let hash = ''; |
| 45 | try { |
| 46 | const url = new URL(href); |
| 47 | hash = url.hash; |
| 48 | url.hash = ''; |
| 49 | href = url.toString(); |
| 50 | } catch (e) {} |
| 51 | |
| 52 | if (!href.trim()) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | const urlArgs = wpURL.getQueryArgs(href); |
| 57 | |
| 58 | if ( |
| 59 | urlArgs[urlKey] === kubioPreviewUUID && |
| 60 | urlArgs[kubioRandomKey] === kubioRandomValue |
| 61 | ) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | const hrefWithoutProtocol = href.replace(protocolRegExp, ''); |
| 66 | |
| 67 | if (hrefWithoutProtocol.indexOf(baseURL) === 0) { |
| 68 | const nextArgs = {}; |
| 69 | if (kubioPreviewUUID) { |
| 70 | nextArgs[urlKey] = kubioPreviewUUID; |
| 71 | } |
| 72 | nextArgs[kubioRandomKey] = kubioRandomValue |
| 73 | ? kubioRandomValue |
| 74 | : makeRandomString(10); |
| 75 | let nextURL = wpURL.addQueryArgs(href, nextArgs); |
| 76 | |
| 77 | if (hash) { |
| 78 | nextURL += hash; |
| 79 | } |
| 80 | |
| 81 | link.setAttribute('href', nextURL); |
| 82 | } |
| 83 | }); |
| 84 | }; |
| 85 | |
| 86 | keepAliveCurrentUrl(document.body); |
| 87 | |
| 88 | const mutationObserver = new window.MutationObserver(function ( |
| 89 | mutationList |
| 90 | ) { |
| 91 | mutationList.forEach(function (mutation) { |
| 92 | switch (mutation.type) { |
| 93 | case 'childList': |
| 94 | mutation.addedNodes.forEach(function (node) { |
| 95 | if (node.nodeName.toLowerCase() === 'a') { |
| 96 | keepAliveCurrentUrl(node); |
| 97 | } |
| 98 | }); |
| 99 | break; |
| 100 | case 'attributes': |
| 101 | if ( |
| 102 | mutation.target.nodeName.toLowerCase() === 'a' && |
| 103 | mutation.attributeName === 'href' |
| 104 | ) { |
| 105 | keepAliveCurrentUrl(mutation.target); |
| 106 | } |
| 107 | break; |
| 108 | } |
| 109 | }); |
| 110 | }); |
| 111 | mutationObserver.observe(document.body, { |
| 112 | attributes: true, |
| 113 | childList: true, |
| 114 | subtree: true, |
| 115 | }); |
| 116 | })(window.location, window.document, window.wp.url); |
| 117 |