about summary refs log tree commit diff stats
path: root/src/assets/javascripts/reuters.js
blob: 78f5077beba7b710f82d0208760280ced5d0b234 (plain) (blame)
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
window.browser = window.browser || window.chrome

import utils from "./utils.js"

const targets = [/^https?:\/{2}(www\.|)reuters\.com.*/]

const frontends = new Array("neuters")
const protocols = new Array("normal", "tor", "i2p", "loki")

let redirects = {}

for (let i = 0; i < frontends.length; i++) {
	redirects[frontends[i]] = {}
	for (let x = 0; x < protocols.length; x++) {
		redirects[frontends[i]][protocols[x]] = []
	}
}

function setRedirects(val) {
	browser.storage.local.get(["cloudflareBlackList", "offlineBlackList"], r => {
		redirects.neuters = val
		neutersNormalRedirectsChecks = [...redirects.neuters.normal]
		for (const instance of [...r.cloudflareBlackList, ...r.offlineBlackList]) {
			const a = neutersNormalRedirectsChecks.indexOf(instance)
			if (a > -1) neutersNormalRedirectsChecks.splice(a, 1)
		}
		browser.storage.local.set({
			neutersRedirects: redirects,
			neutersNormalRedirectsChecks,
		})
	})
}

let disableReuters,
	protocol,
	protocolFallback,
	reutersRedirects,
	neutersNormalRedirectsChecks,
	neutersNormalCustomRedirects,
	neutersTorRedirectsChecks,
	neutersTorCustomRedirects,
	neutersI2pCustomRedirects,
	neutersLokiCustomRedirects

function init() {
	return new Promise(async resolve => {
		browser.storage.local.get(
			[
				"disableReuters",
				"protocol",
				"protocolFallback",
				"reutersRedirects",
				"neutersNormalRedirectsChecks",
				"neutersNormalCustomRedirects",
				"neutersTorRedirectsChecks",
				"neutersTorCustomRedirects",
				"neutersI2pCustomRedirects",
				"neutersLokiCustomRedirects",
			],
			r => {
				disableReuters = r.disableReuters
				protocol = r.protocol
				protocolFallback = r.protocolFallback
				reutersRedirects = r.reutersRedirects
				neutersNormalRedirectsChecks = r.neutersNormalRedirectsChecks
				neutersNormalCustomRedirects = r.neutersNormalCustomRedirects
				neutersTorRedirectsChecks = r.neutersTorRedirectsChecks
				neutersTorCustomRedirects = r.neutersTorCustomRedirects
				neutersI2pCustomRedirects = r.neutersI2pCustomRedirects
				neutersLokiCustomRedirects = r.neutersLokiCustomRedirects
				resolve()
			}
		)
	})
}

init()
browser.storage.onChanged.addListener(init)

function redirect(url, type, initiator, disableOverride) {
	if (disableReuters && !disableOverride) return
	if (type != "main_frame") return
	const all = [...reutersRedirects.neuters.normal, ...neutersNormalCustomRedirects]
	if (initiator && (all.includes(initiator.origin) || targets.includes(initiator.host))) return
	if (!targets.some(rx => rx.test(url.href))) return

	let instancesList = []
	if (protocol == "loki") instancesList = [...neutersLokiCustomRedirects]
	else if (protocol == "i2p") instancesList = [...neutersI2pCustomRedirects]
	else if (protocol == "tor") instancesList = [...neutersTorRedirectsChecks, ...neutersTorCustomRedirects]
	if ((instancesList.length === 0 && protocolFallback) || protocol == "normal") {
		instancesList = [...neutersNormalRedirectsChecks, ...neutersNormalCustomRedirects]
	}
	if (instancesList.length === 0) return

	const randomInstance = utils.getRandomInstance(instancesList)
	// stolen from https://addons.mozilla.org/en-US/firefox/addon/reuters-redirect/
	if (url.pathname.startsWith("/article/") || url.pathname.startsWith("/pf/") || url.pathname.startsWith("/arc/") || url.pathname.startsWith("/resizer/")) return null
	else if (url.pathname.endsWith("/")) return `${randomInstance}${url.pathname}`
	else return `${randomInstance}${url.pathname}/`
}

function initDefaults() {
	return new Promise(async resolve => {
		fetch("/instances/data.json")
			.then(response => response.text())
			.then(async data => {
				let dataJson = JSON.parse(data)
				for (let i = 0; i < frontends.length; i++) {
					redirects[frontends[i]] = dataJson[frontends[i]]
				}
	browser.storage.local.get(["cloudflareBlackList", "offlineBlackList"], async r => {
		neutersNormalRedirectsChecks = [...redirects.neuters.normal]
		for (const instance of [...r.cloudflareBlackList, ...r.offlineBlackList]) {
			const a = neutersNormalRedirectsChecks.indexOf(instance)
			if (a > -1) neutersNormalRedirectsChecks.splice(a, 1)
		}
				browser.storage.local.set(
					{
						disableReuters: true,

						reutersRedirects: redirects,

						neutersNormalRedirectsChecks,
						neutersNormalCustomRedirects: [],

						neutersTorRedirectsChecks: [...redirects.neuters.tor],
						neutersTorCustomRedirects: [],

						neutersI2pRedirectsChecks: [...redirects.neuters.i2p],
						neutersI2pCustomRedirects: [],

						neutersLokiRedirectsChecks: [...redirects.neuters.loki],
						neutersLokiCustomRedirects: [],
					},
					() => resolve()
				)
			})
			})
	})
}

export default {
	setRedirects,
	redirect,
	initDefaults,
}