|
1 | 1 | import "./src/styles/global.css"; |
2 | 2 | /* global window, document */ |
| 3 | +const instaEmbedClasses = [".instagram-media"].join(","); |
| 4 | +const twitterEmbedClasses = [ |
| 5 | + ".twitter-tweet", |
| 6 | + ".twitter-timeline", |
| 7 | + ".twitter-follow-button", |
| 8 | + ".twitter-share-button", |
| 9 | +].join(","); |
3 | 10 | const scrollTo = (id) => () => { |
4 | 11 | const el = document.querySelector(id); |
5 | 12 | if (el) return window.scrollTo(0, el.offsetTop - 20); |
6 | 13 | return false; |
7 | 14 | }; |
8 | 15 |
|
| 16 | +const injectScript = function injectScript() { |
| 17 | + var js = document.createElement("script"); |
| 18 | + var firstScript = document.getElementsByTagName("script")[0]; |
| 19 | + js.id = "gatsby-plugin-instagram"; |
| 20 | + js.src = "https://instagram.com/embed.js"; |
| 21 | + firstScript.parentNode.insertBefore(js, firstScript); |
| 22 | + injected = true; |
| 23 | + if ( |
| 24 | + typeof instgrm !== "undefined" && |
| 25 | + window.instgrm.Embeds && |
| 26 | + typeof window.instgrm.Embeds.process === "function" |
| 27 | + ) { |
| 28 | + // manual process |
| 29 | + window.instgrm.Embeds.process(); |
| 30 | + } |
| 31 | + return true; |
| 32 | +}; |
| 33 | + |
| 34 | +var injectTwitterScript = function injectTwitterScript() { |
| 35 | + function addJS(jsCode) { |
| 36 | + var s = document.createElement("script"); |
| 37 | + s.type = "text/javascript"; |
| 38 | + s.innerText = jsCode; |
| 39 | + document.getElementsByTagName("head")[0].appendChild(s); |
| 40 | + injectedTwitterScript = true; |
| 41 | + |
| 42 | + if ( |
| 43 | + typeof twttr !== "undefined" && |
| 44 | + window.twttr.widgets && |
| 45 | + typeof window.twttr.widgets.load === "function" |
| 46 | + ) { |
| 47 | + window.twttr.widgets.load(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + addJS( |
| 52 | + '\n window.twttr = (function(d, s, id) {\n var js,\n fjs = d.getElementsByTagName(s)[0],\n t = window.twttr || {};\n if (d.getElementById(id)) return t;\n js = d.createElement(s);\n js.id = id;\n js.src = "https://platform.twitter.com/widgets.js";\n fjs.parentNode.insertBefore(js, fjs);\n t._e = [];\n t.ready = function(f) {\n t._e.push(f);\n };\n return t;\n })(document, "script", "twitter-wjs");\n ' |
| 53 | + ); |
| 54 | +}; |
| 55 | +let injected = false; |
| 56 | +let injectedTwitterScript = false; |
| 57 | + |
9 | 58 | export const onRouteUpdate = ({ location: { hash } }) => { |
10 | 59 | if (hash) { |
11 | 60 | window.setTimeout(scrollTo(hash), 10); |
12 | 61 | } |
| 62 | + |
| 63 | + if (document.querySelector(instaEmbedClasses) !== null) { |
| 64 | + setTimeout(() => { |
| 65 | + if (!injected) { |
| 66 | + window.addEventListener( |
| 67 | + "scroll", |
| 68 | + function () { |
| 69 | + injectScript(); |
| 70 | + }, |
| 71 | + { once: true } |
| 72 | + ); |
| 73 | + } |
| 74 | + }, 2000); |
| 75 | + } |
| 76 | + |
| 77 | + if (document.querySelector(twitterEmbedClasses) !== null) { |
| 78 | + setTimeout(() => { |
| 79 | + if (!injectedTwitterScript) { |
| 80 | + window.addEventListener( |
| 81 | + "scroll", |
| 82 | + function () { |
| 83 | + injectTwitterScript(); |
| 84 | + }, |
| 85 | + { once: true } |
| 86 | + ); |
| 87 | + } |
| 88 | + }, 2000); |
| 89 | + } |
13 | 90 | }; |
| 91 | + |
| 92 | +export const registerServiceWorker = () => true; |
0 commit comments