-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
184 lines (164 loc) · 5.76 KB
/
background.js
File metadata and controls
184 lines (164 loc) · 5.76 KB
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
const app_match_patterns = [
"*://*.netflix.com/*",
"*://*.amazon.com/Amazon-Video/*",
"*://*.amazon.com/gp/video/detail/*",
"*://*.pandora.com/*",
"*://*.youtube.com/*",
"*://*.funimation.com/*",
"*://*.hulu.com/*",
"*://*.crunchyroll.com/*",
"*://*.plex.tv/*",
"*://*.vudu.com/*",
"*://*.hbomax.com/*",
"*://*.vrv.co/*",
"*://*.disneyplus.com/*",
"*://*.twitch.tv/*",
"*://*.peacocktv.com/*"
];
let tab_info = new Map();
function extract(url, tab_id) {
if (tab_id) {
let cached = tab_info.get(tab_id);
if (cached) return cached;
}
for (let extractor of extractors) {
const result = extractor.url_regexp.exec(url);
if (result) {
let content_id, media_type;
let pair = extractor.url_extractor?.(result);
if (pair) [content_id, media_type] = pair;
return {
name: extractor.name,
channel_id: extractor.channel_id,
content_id: content_id,
media_type: media_type
}
}
}
}
function get_title(url, tab_id) {
const info = extract(url, tab_id);
return `${info.content_id ? `Play this ${info.media_type ? info.media_type + " " : ""}on your Roku's ${info.name} app` : `Open ${info.name} on your Roku`}`;
}
function set_title(url, tab_id) {
browser.pageAction.setTitle({
tabId: tab_id,
title: get_title(url, tab_id)
});
}
async function open_on_roku(url, tab_id) {
if (typeof (url) != "string") throw `URL is ${url}`;
const roku_ip = (await browser.storage.local.get("roku_ip")).roku_ip;
if (!await browser.permissions.contains({ origins: [`*://${roku_ip}/*`] })) {
console.error(`We don't have permission for ${roku_ip}`);
browser.runtime.openOptionsPage();
return;
}
const info = extract(url, tab_id);
let qs = [];
if (info.content_id) qs.push(`contentId=${info.content_id}`);
if (info.media_type) qs.push(`mediaType=${info.media_type}`);
qs = qs.join("&");
const roku_url = `http://${roku_ip}:8060/launch/${info.channel_id}${qs ? "?" + qs : ""}`;
const response = await fetch(roku_url, fetch_init);
// Sometimes when the Roku is off, it'll just open to the home screen instead of the app, and respond with 503.
// However, now that it's on, we can send it again.
if (response.status == 503) await fetch(roku_url, fetch_init);
}
async function redo_menus() {
let options = await browser.storage.local.get(["context_menus", "bookmark_menus"]);
browser.menus.removeAll();
browser.menus.create({
contexts: ["page_action"],
id: "open_options",
title: "Options"
});
if (options.context_menus) {
let contexts = ["link"];
if (options.bookmark_menus) contexts.push("bookmark");
browser.menus.create({
contexts,
id: "app_link",
title: "Open on your Roku",
targetUrlPatterns: app_match_patterns
});
}
}
async function setup() {
const relevant = await browser.tabs.query({ url: app_match_patterns });
for (const tab of relevant) {
set_title(tab.url, tab.id);
}
redo_menus();
browser.menus.onClicked.addListener(async (info, _tab) => {
if (info.menuItemId == "open_options") {
browser.runtime.openOptionsPage();
} else if (info.menuItemId == "app_link") {
if (info.linkUrl) {
open_on_roku(info.linkUrl);
} else if (info.bookmarkId) {
let url = (await browser.bookmarks.get(info.bookmarkId))?.[0]?.url;
if (url) {
open_on_roku(url);
}
}
}
});
}
browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status === "loading") {
tab_info.delete(tabId);
set_title(tab.url, tabId);
} else if (changeInfo.status == "complete" && !extract(tab.url, tabId)) {
browser.pageAction.hide(tabId);
}
}, { properties: ["status"] });
browser.tabs.onRemoved.addListener((tabId) => tab_info.delete(tabId));
const amazon_url_rx = /.*?\/(\w+)\/?(?:\?.*|$)/i;
browser.webRequest.onBeforeRequest.addListener((details) => {
tab_info.set(details.tabId, {
name: "Prime Video",
channel_id: 13,
content_id: amazon_url_rx.exec(details.documentUrl)[1],
media_type: "season"
});
set_title(details.documentUrl, details.tabId);
browser.pageAction.show(details.tabId);
},
{ urls: ["*://js-assets.aiv-cdn.net/playback/web_player/WebLoader.js"] }
);
const utf8decoder = new TextDecoder();
const title_id_rx = /"asin":"(\w+)"/i;
browser.webRequest.onBeforeRequest.addListener((details) => {
const raw = details.requestBody?.raw?.[0]?.bytes;
if (raw) {
const info = utf8decoder.decode(raw);
if (info) {
const title_id = title_id_rx.exec(info)?.[1];
if (title_id) {
tab_info.set(details.tabId, {
name: "Prime Video",
channel_id: 13,
content_id: amazon_url_rx.exec(details.documentUrl)[1],
media_type: "season"
});
set_title(details.documentUrl, details.tabId);
}
}
}
},
{ urls: ["*://fls-na.amazon.com/1/aiv-web-player/1/OE"] },
["requestBody"]
);
const fetch_init = {
headers: { "cache-control": "no-cache" },
method: "POST",
};
browser.pageAction.onClicked.addListener((tab, on_click_data) => {
open_on_roku(tab.url, tab.id);
});
browser.runtime.onInstalled.addListener(setup);
browser.runtime.onStartup.addListener(setup);
browser.runtime.onMessage.addListener(async (message) => {
if (message == "redo_menus") redo_menus();
});