Skip to content

Commit

Permalink
Adjust code because page content is now loaded dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
krisztianb committed Sep 18, 2024
1 parent 1ce0785 commit 36200d7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 34 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "Seeking Alpha Paywall Remover",
"version": "1.6",
"version": "1.7",
"description": "Removes the paywall from news and article pages on the seekingalpha.com website",
"icons": {
"48": "icons/48x48.png",
Expand Down
72 changes: 39 additions & 33 deletions paywall-remover.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
let matchingPayWallSelector = "";

/**
* The structure of the HTML (and the pay wall) is not always the same.
* So we need to check different possible pay wall locations on the page.
Expand All @@ -12,41 +10,52 @@ const payWallSelectors = [

const overlaySelector = "div.bg-black\\/30"; // this is the element creating the gray semi-transparent overlay effect
const contentWrapperSelector = ".contents";
const articleSelector = "main article section";
const contentSelector = "[data-test-id='content-container']";
const adSelector = "main + div"; // might contain "special offer" ads

// Store the original non-pay-walled content
const content = document.querySelector(articleSelector).innerHTML;

removeAds();
let matchingPayWallSelector = "";
let content = ""; // Stores the original full non-pay-walled content

// Code executed once the paywall is shown
// Code executed once the loader has finished loading the full content
new window.MutationObserver(function (mutations) {
for (const mutation of mutations) {
payWallSelectors.forEach((selector) => {
if (mutation.target.matches(selector)) {
matchingPayWallSelector = selector;
}
});

if (matchingPayWallSelector) {
removeBodyScrollLock();
hidePayWall();
restoreContent();
if (mutation.target.matches(contentSelector)) {
storeContent();
preparePayWallRemover();
this.disconnect();
}
}
}).observe(document, { subtree: true, childList: true });

// Code that removes the interactivity lock if it gets added by some script
new window.MutationObserver(function (mutations) {
for (const mutation of mutations) {
if (mutation.target.matches(contentWrapperSelector)) {
removeInteractivityLock();
this.disconnect();
function preparePayWallRemover() {
// Code executed once the paywall is shown
new window.MutationObserver(function (mutations) {
for (const mutation of mutations) {
payWallSelectors.forEach((selector) => {
if (mutation.target.matches(selector)) {
matchingPayWallSelector = selector;
}
});

if (matchingPayWallSelector) {
removeBodyScrollLock();
hidePayWall();
restoreContent();
this.disconnect();
}
}
}
}).observe(document, { subtree: true, attributes: true });
}).observe(document, { subtree: true, childList: true });

// Code that removes the interactivity lock if it gets added by some script
new window.MutationObserver(function (mutations) {
for (const mutation of mutations) {
if (mutation.target.matches(contentWrapperSelector)) {
removeInteractivityLock();
this.disconnect();
}
}
}).observe(document, { subtree: true, attributes: true });
}

function hidePayWall() {
const payWallDialog = document.querySelector(matchingPayWallSelector);
Expand All @@ -73,13 +82,10 @@ function removeInteractivityLock() {
});
}

function restoreContent() {
document.querySelector(articleSelector).innerHTML = content;
function storeContent() {
content = document.querySelector(contentSelector).innerHTML;
}

function removeAds() {
const ad = document.querySelector(adSelector);
if (ad) {
ad.remove();
}
function restoreContent() {
document.querySelector(contentSelector).innerHTML = content;
}

0 comments on commit 36200d7

Please sign in to comment.