Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Twitch JS API widget replacement placeholder #3050

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/data/surrogates.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ const hostnames = {
],
widgetName: "Google reCAPTCHA"
},
'embed.twitch.tv': {
match: MATCH_PREFIX,
tokens: [
'/embed/v1.js'
],
widgetName: "Twitch Player"
},
'platform.twitter.com': {
match: MATCH_PREFIX,
tokens: [
Expand Down Expand Up @@ -207,6 +214,8 @@ const surrogates = {

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

'/embed/v1.js': 'twitch.js',

'noopjs': 'noop.js'
};

Expand Down
20 changes: 20 additions & 0 deletions src/data/web_accessible_resources/twitch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function () {
// https://dev.twitch.tv/docs/embed/everything/
class Embed {
constructor(id, conf) {
let detail = {
type: "widgetFromSurrogate",
name: "Twitch Player",
widgetData: {
domId: id,
videoId: conf.channel
}
};
document.dispatchEvent(new CustomEvent("pbSurrogateMessage", { detail }));
}
}

window.Twitch = {
Embed
};
}());
29 changes: 29 additions & 0 deletions src/js/webrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,35 @@ function getSurrogateWidget(name, data, frame_url) {
return widget;
}

if (name == "Twitch Player") {
if (!data || !data.domId || !data.videoId) {
return false;
}

let video_id = data.videoId,
dom_id = data.domId;

if (!OK.test(video_id) || !OK.test(dom_id)) {
return false;
}

let widget = {
name,
buttonSelectors: ["#" + dom_id],
scriptSelectors: [
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need scriptSelectors if we're going to reloadOnActivation?

`script[src^='${CSS.escape("https://embed.twitch.tv/embed/v1.js")}']`
],
replacementButton: {
"unblockDomains": ["twitch.tv", "*.twitch.tv"],
"type": 4
},
directLinkUrl: `https://www.twitch.tv/${video_id}`,
reloadOnActivation: true
};

return widget;
}

return false;
}

Expand Down