Skip to content

Instantly share code, notes, and snippets.

@Haugen
Last active February 15, 2019 10:39
Show Gist options
  • Save Haugen/11e039aa2d5c3976ad58eaddcea649f1 to your computer and use it in GitHub Desktop.
Save Haugen/11e039aa2d5c3976ad58eaddcea649f1 to your computer and use it in GitHub Desktop.
DN

Read DN

This is quick and dirty experimental code that lets you read articles on dn.se. Try it out! Not all articles send the premium content to the browser though. In those cases, there is nothing this script can do. Suggestions or improvements? Did it work for you? Please let me know.

To use this you need:

Once installed, att the custom script below and browse to an article on http://dn.se/ that would normally be behind a paywall. Wait until the page has loaded. Can you read the article?

// ==UserScript==
// @name Read DN
// @namespace http://tampermonkey.net/
// @version 0.1.0
// @description I just want to read the news.
// @author Tobias Haugen
// @match *://www.dn.se/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Collecting the content I need while the page is loading.
const premiumContent = document.querySelector('.article__premium-content');
const premiumContentText = document.querySelector(
'.article__premium-content .article__content'
);
const articleLead = document.querySelector('.article__lead');
const articleHeader = document.querySelector('article.article header');
// Reset the classes here on the premium content so that their js don't hide it.
premiumContent.className = '';
// When page is loaded, call my function to make and insert the article.
// Only do this is we actually caught some premium content. Otherwise we just
// leave the page as is.
premiumContentText
? window.addEventListener('load', newArticle, false)
: console.log('No premium content sent to browser');
function newArticle() {
// Getting the wrapper of the paywall covered article so we can remove it.
const oldArticle = document.querySelector('.article__content--locked');
oldArticle.remove();
// Resetting classes again. Seams to be nesseccary to make sure no classes are left after page load.
premiumContent.className = '';
// Inser the lead in the right place in the article body.
premiumContentText.insertAdjacentElement('afterBegin', articleLead);
// Create a new wrapper with the article and insert it under the article header.
const newArticleWrapper = document.createElement('div');
newArticleWrapper.setAttribute('id', 'new-article');
newArticleWrapper.appendChild(premiumContent);
articleHeader.insertAdjacentElement('afterEnd', newArticleWrapper);
// First attempt to start removing ads since they don't allow adblock. Not working too great atm.
const ads = document.getElementsByClassName('ad');
for (let ad of ads) {
ad.remove();
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment