Merge pull request #2631 from omnivore-app/fix/web-intercom-id

Fix intercom id preventing it from being double embedded
This commit is contained in:
Jackson Harper
2023-08-10 11:31:41 +08:00
committed by GitHub
3 changed files with 151 additions and 105 deletions

View File

@ -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;

View File

@ -1 +1,32 @@
(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)
}
}
})()

View File

@ -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,149 @@
'/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);
});
})();
if (ev.request.destination === 'script') {
return
}
const handler = handleFetchRequest(ev)
ev.respondWith(handler)
})
})()