Skip to content

Commit df8f5f9

Browse files
committed
Add Twitch JS API widget replacement placeholder
1 parent 85e88e0 commit df8f5f9

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

src/data/surrogates.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ const hostnames = {
131131
],
132132
widgetName: "Google reCAPTCHA"
133133
},
134+
'embed.twitch.tv': {
135+
match: MATCH_PREFIX,
136+
tokens: [
137+
'/embed/v1.js'
138+
],
139+
widgetName: "Twitch Player"
140+
},
134141
'platform.twitter.com': {
135142
match: MATCH_PREFIX,
136143
tokens: [
@@ -207,6 +214,8 @@ const surrogates = {
207214

208215
'/omweb-v1.js': 'noop.js',
209216

217+
'/embed/v1.js': 'twitch.js',
218+
210219
'noopjs': 'noop.js'
211220
};
212221

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
(function () {
2+
// https://dev.twitch.tv/docs/embed/everything/
3+
class Embed {
4+
constructor(id, conf) {
5+
let detail = {
6+
type: "widgetFromSurrogate",
7+
name: "Twitch Player",
8+
widgetData: {
9+
domId: id,
10+
videoId: conf.channel
11+
}
12+
};
13+
document.dispatchEvent(new CustomEvent("pbSurrogateMessage", { detail }));
14+
}
15+
}
16+
17+
window.Twitch = {
18+
Embed
19+
};
20+
}());

src/js/webrequest.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,35 @@ function getSurrogateWidget(name, data, frame_url) {
11161116
return widget;
11171117
}
11181118

1119+
if (name == "Twitch Player") {
1120+
if (!data || !data.domId || !data.videoId) {
1121+
return false;
1122+
}
1123+
1124+
let video_id = data.videoId,
1125+
dom_id = data.domId;
1126+
1127+
if (!OK.test(video_id) || !OK.test(dom_id)) {
1128+
return false;
1129+
}
1130+
1131+
let widget = {
1132+
name,
1133+
buttonSelectors: ["#" + dom_id],
1134+
scriptSelectors: [
1135+
`script[src^='${CSS.escape("https://embed.twitch.tv/embed/v1.js")}']`
1136+
],
1137+
replacementButton: {
1138+
"unblockDomains": ["twitch.tv", "*.twitch.tv"],
1139+
"type": 4
1140+
},
1141+
directLinkUrl: `https://www.twitch.tv/${video_id}`,
1142+
reloadOnActivation: true
1143+
};
1144+
1145+
return widget;
1146+
}
1147+
11191148
return false;
11201149
}
11211150

0 commit comments

Comments
 (0)