From c2eb2ad043f29f4d6883c9381e6a7e97cf19c2ec Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 10 Aug 2023 09:00:20 +0800 Subject: [PATCH 1/8] Fix intercom id preventing it from being double embedded --- .../web/public/static/scripts/intercom.js | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/packages/web/public/static/scripts/intercom.js b/packages/web/public/static/scripts/intercom.js index 29c6aa4ee..ff965895d 100644 --- a/packages/web/public/static/scripts/intercom.js +++ b/packages/web/public/static/scripts/intercom.js @@ -1 +1,31 @@ -(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic('reattach_activator');ic('update',w.intercomSettings);}else{var d=document;var i=function(){i.c(arguments);};i.q=[];i.c=function(args){i.q.push(args);};w.Intercom=i;var l=function(){var s=d.createElement('script');s.type='text/javascript';s.async=true;s.src='https://widget.intercom.io/widget/rmymbkjv';var x=d.getElementsByTagName('script')[0];x.parentNode.insertBefore(s,x);};if(w.attachEvent){w.attachEvent('onload',l);}else{w.addEventListener('load',l,false);}}})(); +;(function () { + var w = window + var ic = w.Intercom + if (typeof ic === 'function') { + ic('reattach_activator') + ic('update', w.intercomSettings) + } else { + var d = document + var i = function () { + i.c(arguments) + } + i.q = [] + i.c = function (args) { + i.q.push(args) + } + w.Intercom = i + var l = function () { + var s = d.createElement('script') + s.type = 'text/javascript' + s.async = true + s.src = 'https://widget.intercom.io/widget/lddb7880' + var x = d.getElementsByTagName('script')[0] + x.parentNode.insertBefore(s, x) + } + if (w.attachEvent) { + w.attachEvent('onload', l) + } else { + w.addEventListener('load', l, false) + } + } +})() From 1f9beca0d56dd9f028bfd22c279c00f662572cce Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 10 Aug 2023 09:15:52 +0800 Subject: [PATCH 2/8] Add some debugging --- packages/web/public/static/scripts/intercom.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/web/public/static/scripts/intercom.js b/packages/web/public/static/scripts/intercom.js index ff965895d..c3bca674b 100644 --- a/packages/web/public/static/scripts/intercom.js +++ b/packages/web/public/static/scripts/intercom.js @@ -1,10 +1,13 @@ ;(function () { var w = window var ic = w.Intercom + console.log('running the intercom script, checking if we are injected: ', ic) + if (typeof ic === 'function') { ic('reattach_activator') ic('update', w.intercomSettings) } else { + console.log('injecting the intercom widget') var d = document var i = function () { i.c(arguments) From 496d3bbb7cefb2e580b3ac3efa573d79979c118c Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 10 Aug 2023 09:22:14 +0800 Subject: [PATCH 3/8] Add more debugging --- packages/web/public/static/scripts/intercom.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/web/public/static/scripts/intercom.js b/packages/web/public/static/scripts/intercom.js index c3bca674b..a89ba39da 100644 --- a/packages/web/public/static/scripts/intercom.js +++ b/packages/web/public/static/scripts/intercom.js @@ -18,6 +18,8 @@ } w.Intercom = i var l = function () { + console.log(' running intercom onload script ') + var s = d.createElement('script') s.type = 'text/javascript' s.async = true From b2eb20ecd37a89b800e97eb400cebfb33ad94936 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 10 Aug 2023 09:54:19 +0800 Subject: [PATCH 4/8] Try debugging service worker --- packages/web/public/sw.js | 209 ++++++++++++++++++++------------------ 1 file changed, 110 insertions(+), 99 deletions(-) diff --git a/packages/web/public/sw.js b/packages/web/public/sw.js index ceed2950c..01242baa3 100644 --- a/packages/web/public/sw.js +++ b/packages/web/public/sw.js @@ -4,19 +4,19 @@ Response */ -'use strict'; +'use strict' +;(function () { + const globalApi = (typeof globalThis !== 'undefined' && globalThis) || self + const naviApi = globalApi.navigator -(function () { - const globalApi = (typeof globalThis !== 'undefined' && globalThis) || self; - const naviApi = globalApi.navigator; + const mainOrigin = 'https://omnivore.app' + const devOrigin = 'https://dev.omnivore.app' + const demoOrigin = 'https://demo.omnivore.app' + const webProdOrigin = 'https://web-prod.omnivore.app' + const webDemoOrigin = 'https://web-demo.omnivore.app' - const mainOrigin = 'https://omnivore.app'; - const devOrigin = 'https://dev.omnivore.app'; - const demoOrigin = 'https://demo.omnivore.app'; - const webProdOrigin = 'https://web-prod.omnivore.app'; - const webDemoOrigin = 'https://web-demo.omnivore.app'; - - const currentOrigin = (globalApi.location && globalApi.location.origin) || mainOrigin; + const currentOrigin = + (globalApi.location && globalApi.location.origin) || mainOrigin if ( currentOrigin !== mainOrigin && @@ -24,11 +24,12 @@ currentOrigin !== demoOrigin && currentOrigin !== webProdOrigin && currentOrigin !== webDemoOrigin - ) return; + ) + return - const cacheVersion = 'v1.0.0'; + const cacheVersion = 'v1.0.0' - const homeCache = '/?cid=' + cacheVersion; + const homeCache = '/?cid=' + cacheVersion const initialCacheItems = [ homeCache, @@ -41,135 +42,145 @@ '/pwa-192.png', '/pwa-256.png', '/pwa-384.png', - '/pwa-512.png' - ]; + '/pwa-512.png', + ] - function fetchWithCacheBackup (request) { + function fetchWithCacheBackup(request) { return globalApi.fetch(request).then((freshResult) => { - if (freshResult.status > 199 && freshResult.status < 400) return freshResult; + if (freshResult.status > 199 && freshResult.status < 400) + return freshResult - return globalApi.caches.match(request, { - ignoreSearch: true, - ignoreMethod: true, - ignoreVary: true - }).then((cachedResult) => { - return cachedResult || freshResult; - }); - }); + return globalApi.caches + .match(request, { + ignoreSearch: true, + ignoreMethod: true, + ignoreVary: true, + }) + .then((cachedResult) => { + return cachedResult || freshResult + }) + }) } - function findShareUrlInText (formData) { - const url = formData.get('url') || ''; - const text = formData.get('text') || ''; - const title = formData.get('title') || ''; + function findShareUrlInText(formData) { + const url = formData.get('url') || '' + const text = formData.get('text') || '' + const title = formData.get('title') || '' - const allParts = [url, text, title].join(' '); - const snippets = allParts.split(/\s+/); + const allParts = [url, text, title].join(' ') + const snippets = allParts.split(/\s+/) for (let i = 0; i < snippets.length; i++) { try { - const shareUrl = new URL(snippets[i]); - return shareUrl; + const shareUrl = new URL(snippets[i]) + return shareUrl } catch (e) {} } } - function saveArticleUrl (url) { + function saveArticleUrl(url) { if (!url) { - return Promise.reject(new Error('No URL')); + return Promise.reject(new Error('No URL')) } - const requestUrl = currentOrigin + '/api/article/save'; + const requestUrl = currentOrigin + '/api/article/save' - return globalApi.fetch(requestUrl, { - method: 'POST', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded' - }, - body: new URLSearchParams({ - url: url, - v: '0.2.18' + return globalApi + .fetch(requestUrl, { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + }, + body: new URLSearchParams({ + url: url, + v: '0.2.18', + }), }) - }).then(function (response) { - if (response.status === 200) { - return response.json().then((responseJson) => { - const url = encodeURIComponent(responseJson.url); - return currentOrigin + '/article?url=' + url; - }); - } + .then(function (response) { + if (response.status === 200) { + return response.json().then((responseJson) => { + const url = encodeURIComponent(responseJson.url) + return currentOrigin + '/article?url=' + url + }) + } - if (response.status === 400) { - return response.json().then((responseJson) => { - if (responseJson.errorCode === 'UNAUTHORIZED') { - return currentOrigin + '/login'; - } - }); - } - }); + if (response.status === 400) { + return response.json().then((responseJson) => { + if (responseJson.errorCode === 'UNAUTHORIZED') { + return currentOrigin + '/login' + } + }) + } + }) } - function handleShareTarget (request) { - return request.formData().then((formData) => { - const shareUrl = findShareUrlInText(formData); + function handleShareTarget(request) { + return request + .formData() + .then((formData) => { + const shareUrl = findShareUrlInText(formData) - return saveArticleUrl(shareUrl).catch(() => { - // generic error redirect - return currentOrigin + '/'; - }); - }).then((responseUrl) => { - return Response.redirect(responseUrl, 303); - }); + return saveArticleUrl(shareUrl).catch(() => { + // generic error redirect + return currentOrigin + '/' + }) + }) + .then((responseUrl) => { + return Response.redirect(responseUrl, 303) + }) } - function handleFetchRequest (ev) { - const request = ev.request; + function handleFetchRequest(ev) { + const request = ev.request if (request.method === 'POST') { - const requestUrl = new URL(request.url); + const requestUrl = new URL(request.url) if (requestUrl.pathname === '/share-target') { - const shareRequest = handleShareTarget(request); - ev.respondWith(shareRequest); + const shareRequest = handleShareTarget(request) + ev.respondWith(shareRequest) } } if (naviApi.onLine) { - return globalApi.fetch(request); + return globalApi.fetch(request) } - return fetchWithCacheBackup(request); + return fetchWithCacheBackup(request) } - function handleOutdatedCache () { + function handleOutdatedCache() { return globalApi.caches.keys().then((cacheNames) => { - return Promise.all(cacheNames.map((cacheName) => { - if (cacheName !== cacheVersion) { - return globalApi.caches.delete(cacheName); - } - })); - }); + return Promise.all( + cacheNames.map((cacheName) => { + if (cacheName !== cacheVersion) { + return globalApi.caches.delete(cacheName) + } + }) + ) + }) } - function initCache () { + function initCache() { return globalApi.caches.open(cacheVersion).then((cache) => { - if (!cache.addAll || !naviApi.onLine) return; - return cache.addAll(initialCacheItems); - }); + if (!cache.addAll || !naviApi.onLine) return + return cache.addAll(initialCacheItems) + }) } globalApi.addEventListener('install', (ev) => { - globalApi.skipWaiting(); + globalApi.skipWaiting() - const handler = initCache(); - ev.waitUntil(handler); - }); + const handler = initCache() + ev.waitUntil(handler) + }) globalApi.addEventListener('activate', (ev) => { - const handler = handleOutdatedCache(); - ev.waitUntil(handler); - }); + const handler = handleOutdatedCache() + ev.waitUntil(handler) + }) globalApi.addEventListener('fetch', (ev) => { - const handler = handleFetchRequest(ev); - ev.respondWith(handler); - }); -})(); + const handler = handleFetchRequest(ev) + ev.respondWith(handler) + }) +})() From 897c8343fbcab7a1e18ef5a93262d654b701ecbe Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 10 Aug 2023 10:08:47 +0800 Subject: [PATCH 5/8] Comment out fetch handler --- packages/web/public/sw.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/web/public/sw.js b/packages/web/public/sw.js index 01242baa3..b0fdeb219 100644 --- a/packages/web/public/sw.js +++ b/packages/web/public/sw.js @@ -179,8 +179,8 @@ ev.waitUntil(handler) }) - globalApi.addEventListener('fetch', (ev) => { - const handler = handleFetchRequest(ev) - ev.respondWith(handler) - }) + // globalApi.addEventListener('fetch', (ev) => { + // const handler = handleFetchRequest(ev) + // ev.respondWith(handler) + // }) })() From dc21b9f41212b57d5db321d7690916a027196744 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 10 Aug 2023 10:24:30 +0800 Subject: [PATCH 6/8] Add more debugging --- packages/web/public/sw.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/web/public/sw.js b/packages/web/public/sw.js index b0fdeb219..12d991b49 100644 --- a/packages/web/public/sw.js +++ b/packages/web/public/sw.js @@ -179,8 +179,15 @@ ev.waitUntil(handler) }) - // globalApi.addEventListener('fetch', (ev) => { - // const handler = handleFetchRequest(ev) - // ev.respondWith(handler) - // }) + globalApi.addEventListener('fetch', (ev) => { + try { + console.log('request type: ', ev.request) + // if (requestUrl.startsWith(currentOrigin) + } catch (err) { + console.log('fetch request error: ', err) + } + + const handler = handleFetchRequest(ev) + ev.respondWith(handler) + }) })() From 86993f93b4720815965cd1bac72d9e4ee9d6170e Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 10 Aug 2023 10:51:00 +0800 Subject: [PATCH 7/8] Remove scripts from service worker cache --- packages/web/public/sw.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/web/public/sw.js b/packages/web/public/sw.js index 12d991b49..8e195e663 100644 --- a/packages/web/public/sw.js +++ b/packages/web/public/sw.js @@ -180,11 +180,8 @@ }) globalApi.addEventListener('fetch', (ev) => { - try { - console.log('request type: ', ev.request) - // if (requestUrl.startsWith(currentOrigin) - } catch (err) { - console.log('fetch request error: ', err) + if (ev.request.destination === 'script') { + return } const handler = handleFetchRequest(ev) From 89631cc266a79ddedadd729149a947243330c0ea Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Thu, 10 Aug 2023 11:14:17 +0800 Subject: [PATCH 8/8] Remove debug from intercom script --- packages/web/next.config.js | 10 +++++----- packages/web/public/static/scripts/intercom.js | 4 ---- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/packages/web/next.config.js b/packages/web/next.config.js index 3a874e7c2..d4d2544c2 100644 --- a/packages/web/next.config.js +++ b/packages/web/next.config.js @@ -2,14 +2,14 @@ const ContentSecurityPolicy = ` default-src 'self'; base-uri 'self'; block-all-mixed-content; - connect-src 'self' ${process.env.NEXT_PUBLIC_SERVER_BASE_URL} proxy-prod.omnivore-image-cache.app accounts.google.com proxy-demo.omnivore-image-cache.app storage.googleapis.com api.segment.io cdn.segment.com widget.intercom.io api-iam.intercom.io wss://nexus-websocket-a.intercom.io wss://nexus-websocket-b.intercom.io wss://nexus-europe-websocket.intercom.io wss://nexus-australia-websocket.intercom.io platform.twitter.com; - font-src 'self' data: cdn.jsdelivr.net https://js.intercomcdn.com https://fonts.intercomcdn.com; + connect-src 'self' ${process.env.NEXT_PUBLIC_SERVER_BASE_URL} https://proxy-prod.omnivore-image-cache.app https://accounts.google.com https://proxy-demo.omnivore-image-cache.app https://storage.googleapis.com https://api.segment.io https://cdn.segment.com https://widget.intercom.io https://api-iam.intercom.io https://platform.twitter.com wss://nexus-websocket-a.intercom.io wss://nexus-websocket-b.intercom.io wss://nexus-europe-websocket.intercom.io wss://nexus-australia-websocket.intercom.io; + font-src 'self' data: https://cdn.jsdelivr.net https://js.intercomcdn.com https://fonts.intercomcdn.com; form-action 'self' ${process.env.NEXT_PUBLIC_SERVER_BASE_URL} https://getpocket.com/auth/authorize https://intercom.help https://api-iam.intercom.io https://api-iam.eu.intercom.io https://api-iam.au.intercom.io; frame-ancestors 'none'; - frame-src 'self' accounts.google.com platform.twitter.com www.youtube.com www.youtube-nocookie.com; + frame-src 'self' https://accounts.google.com https://platform.twitter.com https://www.youtube.com https://www.youtube-nocookie.com; manifest-src 'self'; - script-src 'self' 'unsafe-inline' 'unsafe-eval' accounts.google.com widget.intercom.io js.intercomcdn.com platform.twitter.com cdnjs.cloudflare.com cdn.jsdelivr.net cdn.segment.com; - style-src 'self' 'unsafe-inline' accounts.google.com cdnjs.cloudflare.com; + script-src 'self' 'unsafe-inline' 'unsafe-eval' accounts.google.com https://widget.intercom.io https://js.intercomcdn.com https://platform.twitter.com https://cdnjs.cloudflare.com https://cdn.jsdelivr.net https://cdn.segment.com; + style-src 'self' 'unsafe-inline' https://accounts.google.com https://cdnjs.cloudflare.com; img-src 'self' blob: data: https:; worker-src 'self' blob:; media-src https://js.intercomcdn.com; diff --git a/packages/web/public/static/scripts/intercom.js b/packages/web/public/static/scripts/intercom.js index a89ba39da..55fb1d20f 100644 --- a/packages/web/public/static/scripts/intercom.js +++ b/packages/web/public/static/scripts/intercom.js @@ -1,13 +1,11 @@ ;(function () { var w = window var ic = w.Intercom - console.log('running the intercom script, checking if we are injected: ', ic) if (typeof ic === 'function') { ic('reattach_activator') ic('update', w.intercomSettings) } else { - console.log('injecting the intercom widget') var d = document var i = function () { i.c(arguments) @@ -18,8 +16,6 @@ } w.Intercom = i var l = function () { - console.log(' running intercom onload script ') - var s = d.createElement('script') s.type = 'text/javascript' s.async = true