Skip to content

Instantly share code, notes, and snippets.

@sugoidesune
Last active April 2, 2024 17:12
Show Gist options
  • Save sugoidesune/884bfdf8a975920e98e7307e981e8daf to your computer and use it in GitHub Desktop.
Save sugoidesune/884bfdf8a975920e98e7307e981e8daf to your computer and use it in GitHub Desktop.
//looking for a string to match name of page with.
// usually url would be enough, but medium script has to run on privately hosted domains too.
var currentsite = document.querySelector("meta[property='al:android:app_name']") ? document.querySelector("meta[property='al:android:app_name']").content : window.location.href
function isPage(pagename) {
return currentsite.includes(pagename)
}
function preprocess(html) {
// perform necessary preprocessing based on newssite
var processedHTML = html;
if (isPage('NYTimes')) {
document.querySelector('html').innerHTML = processedHTML
}
if (isPage('Medium')) {
processedHTML = html.replace(/<\/?noscript>/g, "") // load pictures and gifs
}
if (isPage('Bloomberg')) {
var htmlElement = document.createElement('html')
htmlElement.innerHTML = html
htmlElement.querySelectorAll('script').forEach(script => script.outerHTML = '')
//replace page content
processedHTML = htmlElement.outerHTML
document.open();
document.write(processedHTML);
document.close();
}
if (isPage('businessinsider')) {
var htmlElement = document.createElement('html')
htmlElement.innerHTML = html
//load images, remove scripts and other ways to load them
htmlElement.querySelectorAll('script').forEach(script => script.outerHTML = '')
htmlElement.querySelectorAll('iframe').forEach(iframe => iframe.outerHTML = '')
htmlElement.querySelectorAll('figure').forEach(figure => {
figure.innerHTML = figure.querySelector('noscript').innerHTML
})
processedHTML = htmlElement.outerHTML
document.open();
document.write(processedHTML);
document.close();
}
}
//fetch the full html article
fetch(window.location.href, {
credentials: 'omit', //dont send cookies
redirect: 'follow',
mode: 'no-cors'
})
.then(res => res.text()).then(html => {
preprocess(html) // start preprocessing
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment