-
Notifications
You must be signed in to change notification settings - Fork 2
/
newspaper-paywall-bypasser.user.js
519 lines (495 loc) · 15.2 KB
/
newspaper-paywall-bypasser.user.js
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// ==UserScript==
// @name Newspaper Paywall Bypasser
// @namespace https://greasyfork.org/users/649
// @version 1.2.6
// @description Bypass the paywall on online newspapers
// @author Adrien Pyke
// @match *://www.thenation.com/article/*
// @match *://www.wsj.com/articles/*
// @match *://blogs.wsj.com/*
// @match *://www.bostonglobe.com/*
// @match *://www.nytimes.com/*
// @match *://myaccount.nytimes.com/mobile/wall/smart/*
// @match *://mobile.nytimes.com/*
// @match *://www.latimes.com/*
// @match *://www.washingtonpost.com/*
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @grant unsafeWindow
// @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
// @noframes
// ==/UserScript==
(() => {
'use strict';
// short reference to unsafeWindow (or window if unsafeWindow is unavailable e.g. bookmarklet)
const W = typeof unsafeWindow === 'undefined' ? window : unsafeWindow;
const SCRIPT_NAME = 'Newspaper Paywall Bypasser';
const Util = {
log(...args) {
args.unshift(`%c${SCRIPT_NAME}:`, 'font-weight: bold;color: #233c7b;');
console.log(...args);
},
q(query, context = document) {
return context.querySelector(query);
},
qq(query, context = document) {
return Array.from(context.querySelectorAll(query));
},
getQueryParameter(name, url = W.location.href) {
name = name.replace(/[[\]]/gu, '\\$&');
const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`, 'u');
const results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/gu, ' '));
},
appendStyle(css) {
let out = '';
for (const selector in css) {
out += `${selector}{`;
for (const rule in css[selector]) {
out += `${rule}:${css[selector][rule]}!important;`;
}
out += '}';
}
const style = document.createElement('style');
style.type = 'text/css';
style.appendChild(document.createTextNode(out));
document.head.appendChild(style);
},
clearAllIntervals() {
const interval_id = window.setInterval(null, 9999);
for (let i = 1; i <= interval_id; i++) {
window.clearInterval(i);
}
},
hijackScrollEvent(cb) {
document.onscroll = e => {
if (cb) {
cb(e);
}
e.preventDefault();
e.stopImmediatePropagation();
return false;
};
},
addScript(src, onload) {
const s = document.createElement('script');
s.onload = onload;
s.src = src;
document.body.appendChild(s);
},
prepend(parent, child) {
parent.insertBefore(child, parent.firstChild);
}
};
// GM_xmlhttpRequest polyfill
if (typeof GM_xmlhttpRequest === 'undefined') {
Util.log('Adding GM_xmlhttpRequest polyfill');
W.GM_xmlhttpRequest = function (config) {
const xhr = new XMLHttpRequest();
xhr.open(config.method || 'GET', config.url);
if (config.headers) {
for (const header in config.headers) {
xhr.setRequestHeader(header, config.headers[header]);
}
}
if (config.anonymous) {
xhr.setRequestHeader('Authorization', '');
}
if (config.onload) {
xhr.onload = function () {
config.onload(xhr);
};
}
if (config.onerror) {
xhr.onerror = function () {
config.onerror(xhr.status);
};
}
xhr.send();
};
}
/**
* Sample Implementation:
{
name: 'something', // name of the implementation
match: '^https?://domain.com/.*', // the url to react to
remove: '#element', // css selector to get elements to remove
wait: 3000, // how many ms to wait before running (to wait for elements to load), or a css selector to keep trying until it returns an elem
referer: 'something', // load content in with an xhr using this referrer
replace: '#element', // css selector to get element to replace with xhr
replaceUsing: 'url', // url to use for the replace xhr. If null, it'll use the curren url.
replaceWith: '#element', // css selector to get element to replace the element with. if null, it will use the same seletor as replace.
css: {}, // object, keyed by css selector of css rules
bmmode: function() { }, // function to call before doing anything else if in BM_MODE
fn: function() { }, // a function to run before doing anything else for more complicated logic
afterReplace: function() { } // a function that runs after the replace is done
}
* Any of the CSS selectors can be functions instead that return the desired value.
*/
const implementations = [
{
name: 'The Nation',
match: '^https?://www\\.thenation\\.com/article/.*',
remove: '#paywall',
wait: '#paywall',
bmmode() {
W.Paywall.hide();
}
},
{
name: 'Wall Street Journal',
match: '^https?://.*\\.wsj\\.com/.*',
wait: '.wsj-snippet-login',
referer: 'https://t.co/T1323aaaa',
afterReplace() {
W.loadCSS('//asset.wsj.net/public/extra.production-2a7a40d6.css');
const scripts = Util.qq('script');
const add = function (regex, onload) {
const matching = scripts.filter(script => script.src.match(regex));
if (matching.length > 0) {
Util.addScript(matching[0].src, onload);
} else {
onload();
}
};
add(/\/common\\.js$/iu, () => {
add(/\/article\\.js$/iu, () => {
add(/\/snippet\\.js$/iu);
});
});
}
},
{
name: 'Boston Globe',
match: '^https?://www\\.bostonglobe\\.com/.*',
css: {
'html, body, #contain': {
overflow: 'visible'
},
'.mfp-wrap, .mfp-ready': {
display: 'none'
}
}
},
{
name: 'NY Times',
match: '^https?://www\\.nytimes\\.com/.*',
css: {
'html, body': {
overflow: 'visible'
},
'#Gateway_optly, #overlay': {
display: 'none'
},
'.media .image': {
'margin-bottom': '7px'
},
'.new-story-body-text': {
'font-size': '1.0625rem',
'line-height': '1.625rem'
}
},
cleanupStory(story) {
if (story) {
// prevent payywall from finding the elements to remove
Util.qq('figure', story).forEach(figure => {
figure.outerHTML = figure.outerHTML
.replace(/<figure/u, '<div')
.replace(/<\/figure/u, '</div');
});
Util.qq('.story-body-text', story).forEach(paragraph => {
paragraph.classList.remove('story-body-text');
paragraph.classList.add('new-story-body-text');
});
}
return story;
},
bmmode() {
const self = this;
Util.clearAllIntervals();
GM_xmlhttpRequest({
url: W.location.href,
method: 'GET',
onload(response) {
const tempDiv = document.createElement('div');
tempDiv.innerHTML = response.responseText;
const story = self.cleanupStory(Util.q('#story', tempDiv));
if (story) {
Util.q('#story').innerHTML = story.innerHTML;
}
}
});
},
fn() {
// clear intervals once the paywall comes up to prevent changes afterward
waitForElems({
sel: '#Gateway_optly',
stop: true,
onmatch: Util.clearAllIntervals
});
this.cleanupStory(Util.q('#story'));
setTimeout(() => {
W.require(['jquery/nyt'], $ => {
W.require(['vhs'], vhs => {
Util.qq('.video').forEach(video => {
video.setAttribute('style', 'position: relative');
const bind = document.createElement('div');
bind.classList.add('video-bind');
const div = document.createElement('div');
div.setAttribute(
'style',
'padding-bottom: 56.25%; position: relative; overflow: hidden;'
);
bind.appendChild(div);
Util.prepend(video, bind);
vhs.player({
id: video.dataset.videoid,
container: $(div),
width: '100%',
height: '100%',
mode: 'html5',
controlsOverlay: {
mode: 'article'
},
cover: {
mode: 'article'
},
newControls: true
});
});
});
});
}, 0);
}
},
{
name: 'NY Times Mobile Redirect',
match: '^https?://myaccount\\.nytimes\\.com/mobile/wall/smart/.*',
fn() {
const article = Util.getQueryParameter('EXIT_URI');
if (article) {
W.location.replace(
`http://mobile.nytimes.com?LOAD_ARTICLE=${encodeURIComponent(
article
)}`
);
}
}
},
{
name: 'NY Times Mobile Loader',
match: '^https?://mobile\\.nytimes\\.com',
css: {
'.full-art': {
'font-family': 'Georgia,serif',
color: '#333'
},
'.full-art .article-body': {
'margin-bottom': '26px',
'font-size': '1.6em',
'line-height': '1.4em'
}
},
replaceUsing: Util.getQueryParameter('LOAD_ARTICLE'),
replace() {
if (this.repalceUsing) {
return '.sect';
}
return null;
},
replaceWith() {
if (this.repalceUsing) {
return 'article';
}
return null;
}
},
{
name: 'LA Times',
match: '^https?://www\\.latimes\\.com/.*',
css: {
'div#reg-overlay': {
display: 'none'
},
'html, body': {
overflow: 'visible'
}
},
fn: Util.hijackScrollEvent
},
{
name: 'Washington Post',
match: '^https?://www\\.washingtonpost\\.com/.*',
css: {
'.wp_signin, #wp_Signin': {
display: 'none'
},
'html, body': {
overflow: 'visible'
}
},
fn() {
const handler = e => {
e.stopImmediatePropagation();
};
document.addEventListener('keydown', handler, true);
document.addEventListener('mousewheel', handler, true);
}
}
];
// END OF IMPLEMENTATIONS
const Config = {
load() {
const defaults = {
blacklist: {}
};
let cfg = GM_getValue('cfg');
if (!cfg) return defaults;
cfg = JSON.parse(cfg);
Object.entries(defaults).forEach(([key, value]) => {
if (typeof cfg[key] === 'undefined') {
cfg[key] = value;
}
});
return cfg;
},
save(cfg) {
GM_setValue('cfg', JSON.stringify(cfg));
},
toggleBlacklist(imp) {
const cfg = Config.load();
if (cfg.blacklist[imp]) {
cfg.blacklist[imp] = false;
} else {
cfg.blacklist[imp] = true;
}
Config.save(cfg);
}
};
const App = {
currentImpName: null,
bypass(imp) {
if (W.BM_MODE && imp.bmmode) {
Util.log('Running bookmarkelet specific function');
imp.bmmode();
}
if (imp.fn) {
Util.log('Running site specific function');
imp.fn();
}
if (imp.css) {
Util.log('Adding style');
const cssObj = typeof imp.css === 'function' ? imp.css() : imp.css;
Util.appendStyle(cssObj);
}
if (imp.remove) {
Util.log('Removing elements');
const elemsToRemove =
typeof imp.remove === 'function' ? imp.remove() : Util.qq(imp.remove);
elemsToRemove.forEach(elem => {
elem.remove();
});
}
const replaceSelector =
typeof imp.replace === 'function' ? imp.replace() : imp.replace;
let replaceUsing =
typeof imp.replaceUsing === 'function'
? imp.replaceUsing()
: imp.replaceUsing;
const theReferer =
typeof imp.referer === 'function' ? imp.referer() : imp.referer;
if (replaceSelector || replaceUsing || theReferer) {
replaceUsing = replaceUsing || W.location.href;
Util.log(
`Loading xhr for "${replaceUsing}" with referer: ${theReferer}`
);
GM_xmlhttpRequest({
method: 'GET',
url: replaceUsing,
headers: {
referer: theReferer
},
anonymous: true,
onload(response) {
if (replaceSelector) {
let replaceWithSelector =
typeof imp.replaceWith === 'function'
? imp.replaceWith()
: imp.replaceWith;
replaceWithSelector = replaceWithSelector || replaceSelector;
const tempDiv = document.createElement('div');
tempDiv.innerHTML = response.responseText;
Util.q(replaceSelector).innerHTML = Util.q(
replaceWithSelector,
tempDiv
).innerHTML;
} else {
document.body.innerHTML = response.responseText;
}
if (imp.afterReplace) {
Util.log('Performing after replace logic');
imp.afterReplace();
}
},
onerror() {
Util.log('error occured when loading xhr');
}
});
}
Util.log('Paywall Bypassed.');
},
waitAndBypass(imp) {
if (imp.wait) {
const waitType = typeof imp.wait;
if (waitType === 'number') {
setTimeout(App.bypass(imp), imp.wait || 0);
} else {
const wait = waitType === 'function' ? imp.wait() : imp.wait;
waitForElems({
sel: wait,
stop: true,
onmatch() {
Util.log('Condition fulfilled, bypassing');
App.bypass(imp);
}
});
}
} else {
App.bypass(imp);
}
},
start(imps) {
Util.log('starting...');
const success = imps.some(imp => {
if (imp.match && new RegExp(imp.match, 'iu').test(W.location.href)) {
App.currentImpName = imp.name;
if (W.BM_MODE) {
App.waitAndBypass(imp);
} else {
let menuCommandText;
if (!Config.load().blacklist[imp.name]) {
menuCommandText = `Disable ${SCRIPT_NAME} for ${imp.name}`;
App.waitAndBypass(imp);
} else {
menuCommandText = `Enable ${SCRIPT_NAME} for ${imp.name}`;
Util.log(`${imp.name} blacklisted`);
}
GM_registerMenuCommand(menuCommandText, () => {
Config.toggleBlacklist(imp.name);
location.reload();
});
}
return true;
}
});
if (!success) {
Util.log(`no implementation for ${W.location.href}`, 'error');
}
}
};
App.start(implementations);
})();