-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmedium-unlocked.user.js
More file actions
415 lines (362 loc) · 14.5 KB
/
Copy pathmedium-unlocked.user.js
File metadata and controls
415 lines (362 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// ==UserScript==
// @name Medium Unlocked
// @namespace https://github.com/ShrekBytes
// @description Adds alternate reading links (RemovePaywalls, Freedium, Archive.today & ReadMedium) to Medium paywalled articles with improved reliability.
// @version 3.5.0
// @author ShrekBytes
// @license MIT
// @match https://medium.com/*
// @match https://*.medium.com/*
// @match https://infosecwriteups.com/*
// @match https://*.infosecwriteups.com/*
// @match https://betterprogramming.pub/*
// @match https://*.betterprogramming.pub/*
// @match https://betterhumans.pub/*
// @match https://*.betterhumans.pub/*
// @match https://uxplanet.org/*
// @match https://*.uxplanet.org/*
// @match https://writingcooperative.com/*
// @match https://*.writingcooperative.com/*
// @match https://entrepreneurshandbook.co/*
// @match https://*.entrepreneurshandbook.co/*
// @match https://medium.muz.li/*
// @match https://*.medium.muz.li/*
// @match https://blog.prototypr.io/*
// @match https://*.blog.prototypr.io/*
// @match https://bettermarketing.pub/*
// @match https://*.bettermarketing.pub/*
// @match https://byrslf.co/*
// @match https://*.byrslf.co/*
// @match https://levelup.gitconnected.com/*
// @match https://*.levelup.gitconnected.com/*
// @match https://javascript.plainenglish.io/*
// @match https://*.javascript.plainenglish.io/*
// @match https://thebelladonnacomedy.com/*
// @match https://*.thebelladonnacomedy.com/*
// @match https://medium.datadriveninvestor.com/*
// @match https://*.medium.datadriveninvestor.com/*
// @match https://itnext.io/*
// @match https://*.itnext.io/*
// @match https://proandroiddev.com/*
// @match https://*.proandroiddev.com/*
// @match https://code.likeagirl.io/*
// @match https://*.code.likeagirl.io/*
// @match https://blog.bitsrc.io/*
// @match https://*.blog.bitsrc.io/*
// @match https://uxdesign.cc/*
// @match https://*.uxdesign.cc/*
// @match https://thebolditalic.com/*
// @match https://*.thebolditalic.com/*
// @match https://towardsdatascience.com/*
// @match https://*.towardsdatascience.com/*
// @match https://medium.freecodecamp.org/*
// @match https://*.medium.freecodecamp.org/*
// @match https://hackernoon.com/*
// @match https://*.hackernoon.com/*
// @match https://codeburst.io/*
// @match https://*.codeburst.io/*
// @match https://blog.usejournal.com/*
// @match https://*.blog.usejournal.com/*
// @match https://chatbotslife.com/*
// @match https://*.chatbotslife.com/*
// @match https://plainenglish.io/*
// @match https://*.plainenglish.io/*
// @match https://blog.devgenius.io/*
// @match https://*.blog.devgenius.io/*
// @match https://aws.plainenglish.io/*
// @match https://*.aws.plainenglish.io/*
// @match https://python.plainenglish.io/*
// @match https://*.python.plainenglish.io/*
// @match https://entrepreneurship.com/*
// @match https://*.entrepreneurship.com/*
// @match https://pub.towardsai.net/*
// @match https://*.pub.towardsai.net/*
// @match https://medium.com/@*
// @match https://link.medium.com/*
// @match https://stories.medium.com/*
// @match https://generativeai.pub/*
// @match https://*.generativeai.pub/*
// @icon https://raw.githubusercontent.com/ShrekBytes/medium-unlocked/refs/heads/main/freedom.png
// @grant none
// @noframes
// @run-at document-start
// @homepageURL https://github.com/ShrekBytes/medium-unlocked
// @supportURL https://github.com/ShrekBytes/medium-unlocked/issues
// @downloadURL https://github.com/ShrekBytes/medium-unlocked/raw/main/medium-unlocked.user.js
// @updateURL https://github.com/ShrekBytes/medium-unlocked/raw/main/medium-unlocked.user.js
// ==/UserScript==
(function() {
'use strict';
// State management
const state = {
buttonsAdded: false,
currentUrl: window.location.href,
isChecking: false,
lastCheck: 0,
observer: null,
checkTimeout: null,
abortController: new AbortController()
};
// Performance optimized selectors - ordered by likelihood and specificity
const PAYWALL_SELECTORS = Object.freeze([
// Primary paywall indicators (most common)
'[data-testid="paywall"]',
'[data-testid="meter-stats"]',
'[data-testid="subscribe-paywall"]',
'.paywall',
// Secondary indicators
'[data-testid="paywall-upsell"]',
'[data-testid="meter-card"]',
'.js-paywall',
'.meteredContent',
'.u-showForMembers',
'.memberPreview',
'.js-memberPreview',
// Content limitation indicators
'.js-truncatedPostBody',
'[data-source="paywall"]',
'[data-post-id][data-source="meter"]',
'.u-lineHeightTighter.u-fontSize18:last-child',
// Subscribe/upgrade prompts
'[data-testid="subscribe-button"]',
'[data-testid="upgrade-button"]',
'.js-upgradeButton',
'.js-subscribeButton'
]);
// Optimized text patterns - case insensitive, ordered by frequency
const PAYWALL_PATTERNS = Object.freeze([
'member-only story',
'subscribe to read',
'become a member',
'sign up to read',
'continue reading with',
'read the full story',
'unlock unlimited',
'upgrade to continue',
'this story is published in',
'get unlimited access'
]);
// Efficient paywall detection with early returns
function isPaywalled() {
// Quick DOM-based detection first (fastest)
for (const selector of PAYWALL_SELECTORS) {
if (document.querySelector(selector)) {
return true;
}
}
// Text-based detection (slower, but thorough)
const bodyText = document.body?.textContent?.toLowerCase();
if (!bodyText) return false;
return PAYWALL_PATTERNS.some(pattern => bodyText.includes(pattern));
}
// Optimized button creation with minimal DOM operations
function createButton(text, url, top) {
const button = document.createElement('a');
// Set properties in batch for better performance
Object.assign(button, {
innerHTML: text,
href: url,
target: '_blank',
rel: 'noopener noreferrer',
className: 'medium-unlock-btn'
});
// Add ARIA label for accessibility
button.setAttribute('aria-label', `Read this article on ${text}`);
button.setAttribute('role', 'link');
// Optimized styles as single string with fixed font syntax
button.style.cssText = `
position:fixed !important;top:${top}px !important;right:64px !important;z-index:9999 !important;
background:rgba(64, 64, 128,.33) !important;backdrop-filter:blur(2px) !important;
color:#000 !important;border:1px solid #000 !important;border-radius:2px !important;
font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif !important;
font-size:14px !important;font-weight:400 !important;line-height:1.4 !important;
cursor:pointer !important;width:128px !important;height:36px !important;
display:flex !important;align-items:center !important;justify-content:center !important;
text-decoration:none !important;box-sizing:border-box !important;
`;
return button;
}
// Efficient button management
function addButtons() {
if (state.buttonsAdded || !document.body) return;
// Double-check to prevent race conditions
const existingButtons = document.querySelectorAll('.medium-unlock-btn');
if (existingButtons.length > 0) {
state.buttonsAdded = true;
return;
}
const url = encodeURIComponent(window.location.href);
const rawUrl = window.location.href;
const fragment = document.createDocumentFragment();
// Create buttons in memory first (ordered by preference)
fragment.appendChild(createButton('RemovePaywalls', `https://removepaywalls.com/${rawUrl}`, 400));
fragment.appendChild(createButton('Freedium', `https://freedium-mirror.cfd/${url}`, 440));
fragment.appendChild(createButton('Archive.today', `https://archive.today/latest/${rawUrl}`, 480));
fragment.appendChild(createButton('ReadMedium', `https://readmedium.com/en/${url}`, 520));
// Single DOM append operation
document.body.appendChild(fragment);
state.buttonsAdded = true;
}
function removeButtons() {
if (!state.buttonsAdded) return;
// Use more specific selector to avoid conflicts
const buttons = document.querySelectorAll('.medium-unlock-btn');
if (buttons.length > 0) {
buttons.forEach(btn => btn.remove());
state.buttonsAdded = false;
}
}
// Throttled paywall check to prevent excessive calls
function checkPaywall() {
const now = Date.now();
// Prevent rapid successive checks
if (state.isChecking || (now - state.lastCheck) < 100) {
return;
}
state.isChecking = true;
state.lastCheck = now;
try {
const isPaywalledNow = isPaywalled();
if (isPaywalledNow && !state.buttonsAdded) {
addButtons();
} else if (!isPaywalledNow && state.buttonsAdded) {
removeButtons();
}
} catch (error) {
// Silent error handling - don't break the page
} finally {
state.isChecking = false;
}
}
// Debounced check for performance
function scheduleCheck(delay = 150) {
if (state.checkTimeout) {
clearTimeout(state.checkTimeout);
}
state.checkTimeout = setTimeout(() => {
checkPaywall();
state.checkTimeout = null;
}, delay);
}
// Optimized mutation observer with smart filtering
function createObserver() {
return new MutationObserver((mutations) => {
let shouldCheck = false;
// Efficient mutation analysis
for (const mutation of mutations) {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
// Check only Element nodes for relevant changes
for (const node of mutation.addedNodes) {
if (node.nodeType === Node.ELEMENT_NODE) {
const element = node;
// Check if added node or its children contain paywall indicators
if (element.matches?.(PAYWALL_SELECTORS.join(',')) ||
element.querySelector?.(PAYWALL_SELECTORS.join(','))) {
shouldCheck = true;
break;
}
}
}
if (shouldCheck) break;
}
}
if (shouldCheck) {
scheduleCheck();
}
});
}
// Handle URL changes efficiently
function handleUrlChange() {
const newUrl = window.location.href;
if (newUrl !== state.currentUrl) {
state.currentUrl = newUrl;
removeButtons();
scheduleCheck(300); // Slight delay for page transition
}
}
// Optimized history API interception
function interceptHistory() {
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
history.pushState = function(...args) {
const result = originalPushState.apply(this, args);
setTimeout(handleUrlChange, 0);
return result;
};
history.replaceState = function(...args) {
const result = originalReplaceState.apply(this, args);
setTimeout(handleUrlChange, 0);
return result;
};
}
// Initialize with optimal timing
function initialize() {
// Immediate check if DOM is ready
if (document.readyState !== 'loading') {
scheduleCheck(0);
}
// Setup observers and listeners
if (!state.observer) {
state.observer = createObserver();
// Wait for body to be available
const startObserving = () => {
if (document.body) {
state.observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ['data-testid', 'class', 'data-source']
});
} else {
setTimeout(startObserving, 50);
}
};
startObserving();
}
// Event listeners with passive option for performance and AbortController
window.addEventListener('popstate', handleUrlChange, {
passive: true,
signal: state.abortController.signal
});
// Handle visibility changes
document.addEventListener('visibilitychange', () => {
if (!document.hidden) {
scheduleCheck(50);
}
}, {
passive: true,
signal: state.abortController.signal
});
// DOM ready fallback
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', () => scheduleCheck(0), {
once: true,
signal: state.abortController.signal
});
}
}
// Cleanup function
function cleanup() {
if (state.observer) {
state.observer.disconnect();
state.observer = null;
}
if (state.checkTimeout) {
clearTimeout(state.checkTimeout);
state.checkTimeout = null;
}
if (state.abortController) {
state.abortController.abort();
}
removeButtons();
state.isChecking = false;
}
// Handle page unload
window.addEventListener('beforeunload', cleanup, {
passive: true,
signal: state.abortController.signal
});
// Start everything
interceptHistory();
initialize();
})();