-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadblock-twitch-capture-requests.js
173 lines (157 loc) · 6.55 KB
/
adblock-twitch-capture-requests.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
/*
* This file is part of AdBlock <https://getadblock.com/>,
* Copyright (C) 2013-present Adblock, Inc.
*
* AdBlock is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* AdBlock is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AdBlock. If not, see <http://www.gnu.org/licenses/>.
*/
/* For ESLint: List any global identifiers used in this file below */
/* global DOMPurify */
/*
The code in this file is injected into the world context when a user navigates to Twitch.tv
It performs the following actions:
1.a) listen for event messages from content script (update URL with channel name)
1.b) send event messages (channel name) to content script
1.c) wrap fetch to capture the channel name in the JSON request or response
1.d) when the channel name is found in (1.c) above,
1.d.1) send a event message to the content script (1.b above)
1.d.2) parse the channel name, and update the pages URL
*/
const parseChannelName = function (channelNameToParse) {
// used to decode all encoded HTML (convert '&' to &)
const parseElem = document.createElement('textarea');
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16)}`);
}
parseElem.innerHTML = DOMPurify.sanitize(channelNameToParse);
const channelName = parseElem.innerText;
// Remove whitespace, and encode
return fixedEncodeURIComponent(channelName.replace(/\s/g, ''));
};
const updateURLWrapped = function (channelName) {
if (window.location.pathname !== '/') {
const parsedChannelName = parseChannelName(channelName);
const currentLocation = new URL(window.location.href);
let updatedUrl;
let [baseUrl] = window.location.href.split('&ab_channel');
[baseUrl] = baseUrl.split('?ab_channel');
if (currentLocation.search) {
updatedUrl = `${baseUrl}&ab_channel=${parsedChannelName}`;
} else {
updatedUrl = `${baseUrl}?&ab_channel=${parsedChannelName}`;
}
// Add the name of the channel to the end of URL
window.history.replaceState(null, null, updatedUrl);
}
};
const sendMessageToCS = function (toContentScriptEventName, channelName) {
window.postMessage({
eventName: toContentScriptEventName,
channelName: String(channelName),
}, '*');
};
const preProcessCheck = function (input, params, toContentScriptEventName) {
if (params.length >= 2
&& typeof input === 'string'
&& input.includes('https://gql.twitch.tv/gql')
) {
let body = {};
try {
body = JSON.parse(params[1].body);
} catch (ex) {
// eslint-disable-next-line no-console
console.log('ex', ex);
}
// the following is invoked from a /team/ page or the home page
if (body
&& Array.isArray(body)
&& body.length > 0
&& body[0].variables
&& body[0].variables.channel
&& (window.location.pathname.startsWith('/team/')
|| window.location.pathname.startsWith('/'))
) {
updateURLWrapped(body[0].variables.channel);
sendMessageToCS(toContentScriptEventName, body[0].variables.channel);
}
}
};
const postRequestCheck = function (response, toContentScriptEventName) {
if (response && response.url === 'https://gql.twitch.tv/gql') {
response.clone().json().then((respObj) => {
if (Array.isArray(respObj) && respObj.length > 0) {
let nameFound = false;
for (let inx = 0; (inx < respObj.length && nameFound === false); inx++) {
const entry = respObj[inx];
// capture channel name when loading a video with the URL https://www.twitch.tv/videos/...
if (entry && entry.data && entry.data.video && entry.data.video.owner && entry.data.video.owner.displayName && window.location.pathname.startsWith('/videos/')) {
nameFound = true;
updateURLWrapped(entry.data.video.owner.displayName);
sendMessageToCS(toContentScriptEventName, entry.data.video.owner.displayName);
}
// capture channel name when loading a video with the URL https://www.twitch.tv/clips/...
if (entry && entry.data && entry.data.clip && entry.data.clip.broadcaster && entry.data.clip.broadcaster.displayName && window.location.pathname.indexOf('/clip/')) {
nameFound = true;
updateURLWrapped(entry.data.clip.broadcaster.displayName);
sendMessageToCS(toContentScriptEventName, entry.data.clip.broadcaster.displayName);
}
if (entry && entry.data && entry.data.user && entry.data.user.displayName && (entry.data.user.channel || entry.data.user.stream) && !window.location.pathname.startsWith('/directory/')) {
nameFound = true;
updateURLWrapped(entry.data.user.displayName);
sendMessageToCS(toContentScriptEventName, entry.data.user.displayName);
}
// capture channel name when clicking on the same channels multiple times
// in the 'followed channels' panel on the left on the home page
if (!nameFound && entry && entry.data && entry.data.community && entry.data.community.displayName && !window.location.pathname.startsWith('/directory/')) {
nameFound = true;
updateURLWrapped(entry.data.community.displayName);
sendMessageToCS(toContentScriptEventName, entry.data.community.displayName);
}
}
}
});
}
};
const wrapFetch = function ({ toContentScriptEventName }) {
const myFetch = window.fetch;
window.fetch = function theFetch(...args) {
const params = args;
let input = '';
if (params.length >= 1) {
[input] = params;
}
preProcessCheck(input, params, toContentScriptEventName);
return new Promise((resolve, reject) => {
myFetch.apply(this, args)
.then((response) => {
postRequestCheck(response, toContentScriptEventName);
resolve(response);
})
.catch((error) => {
reject(error);
});
});
};
};
const initWithParams = function () {
try {
const { params } = document.querySelector('script[data-name="capture-requests"]').dataset;
wrapFetch(JSON.parse(params));
} catch (err) {
/* eslint-disable-next-line no-console */
console.error(err);
}
};
const start = function () {
initWithParams();
};
start();