Skip to content
This repository was archived by the owner on Nov 27, 2024. It is now read-only.

Commit 84f85d1

Browse files
committed
Replace mutation observers with interval timers
1 parent 36200d7 commit 84f85d1

File tree

2 files changed

+40
-39
lines changed

2 files changed

+40
-39
lines changed

manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 2,
33
"name": "Seeking Alpha Paywall Remover",
4-
"version": "1.7",
4+
"version": "1.8",
55
"description": "Removes the paywall from news and article pages on the seekingalpha.com website",
66
"icons": {
77
"48": "icons/48x48.png",

paywall-remover.js

+39-38
Original file line numberDiff line numberDiff line change
@@ -9,54 +9,49 @@ const payWallSelectors = [
99
];
1010

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

1614
let matchingPayWallSelector = "";
1715
let content = ""; // Stores the original full non-pay-walled content
1816

19-
// Code executed once the loader has finished loading the full content
20-
new window.MutationObserver(function (mutations) {
21-
for (const mutation of mutations) {
22-
if (mutation.target.matches(contentSelector)) {
23-
storeContent();
24-
preparePayWallRemover();
25-
this.disconnect();
26-
}
17+
// Code checking if the page has finished loading the full content
18+
let fullArticleLoadedCheck = setInterval(() => {
19+
// Here we assume that the page contains at least one paragraph
20+
const hasFullArticleLoaded = document.querySelector("p.paywall-full-content") != null;
21+
22+
if (hasFullArticleLoaded) {
23+
removePayWallFlags();
24+
storeContent();
25+
preparePayWallRemover();
26+
clearInterval(fullArticleLoadedCheck);
2727
}
28-
}).observe(document, { subtree: true, childList: true });
28+
}, 10);
2929

30+
// Code checking if the paywall has been displayed
3031
function preparePayWallRemover() {
31-
// Code executed once the paywall is shown
32-
new window.MutationObserver(function (mutations) {
33-
for (const mutation of mutations) {
34-
payWallSelectors.forEach((selector) => {
35-
if (mutation.target.matches(selector)) {
36-
matchingPayWallSelector = selector;
37-
}
38-
});
32+
let payWallLoadedCheck = setInterval(() => {
33+
payWallSelectors.forEach((selector) => {
34+
const payWall = document.querySelector(selector);
3935

40-
if (matchingPayWallSelector) {
41-
removeBodyScrollLock();
42-
hidePayWall();
43-
restoreContent();
44-
this.disconnect();
36+
if (payWall && payWall.innerHTML != "") {
37+
matchingPayWallSelector = selector;
4538
}
46-
}
47-
}).observe(document, { subtree: true, childList: true });
39+
});
4840

49-
// Code that removes the interactivity lock if it gets added by some script
50-
new window.MutationObserver(function (mutations) {
51-
for (const mutation of mutations) {
52-
if (mutation.target.matches(contentWrapperSelector)) {
53-
removeInteractivityLock();
54-
this.disconnect();
55-
}
41+
if (matchingPayWallSelector) {
42+
removeBodyScrollLock();
43+
hidePayWall();
44+
restoreContent();
45+
clearInterval(payWallLoadedCheck);
5646
}
57-
}).observe(document, { subtree: true, attributes: true });
47+
}, 10);
5848
}
5949

50+
// Code that keeps removing interactivity locks from the page
51+
setInterval(() => {
52+
removeInteractivityLock();
53+
}, 1000);
54+
6055
function hidePayWall() {
6156
const payWallDialog = document.querySelector(matchingPayWallSelector);
6257
if (payWallDialog) {
@@ -71,7 +66,7 @@ function hidePayWall() {
7166

7267
function removeBodyScrollLock() {
7368
const body = document.body;
74-
body.classList.remove("scrollLock");
69+
body.classList.remove("lockScroll");
7570
body.removeAttribute("style"); // there is an additional "overflow:hidden" to remove
7671
}
7772

@@ -82,10 +77,16 @@ function removeInteractivityLock() {
8277
});
8378
}
8479

80+
function removePayWallFlags() {
81+
document.querySelectorAll(".paywall-full-content").forEach((item) => {
82+
item.classList.remove("paywall-full-content");
83+
});
84+
}
85+
8586
function storeContent() {
86-
content = document.querySelector(contentSelector).innerHTML;
87+
content = document.querySelector(articleSelector).innerHTML;
8788
}
8889

8990
function restoreContent() {
90-
document.querySelector(contentSelector).innerHTML = content;
91+
document.querySelector(articleSelector).innerHTML = content;
9192
}

0 commit comments

Comments
 (0)