-
Notifications
You must be signed in to change notification settings - Fork 1
/
youvlc.js
81 lines (70 loc) · 2.44 KB
/
youvlc.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
function getParameterByName(name, url) {
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function extractUrl(url) {
var urlRegex = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
var match = url.match(urlRegex);
if (match)
url = match[0];
// If it's a google url, match 'url' param
if (url.indexOf('google.') > -1)
url = getParameterByName('url', url);
// Matches youtube 'v' param. Found at: http://stackoverflow.com/a/27728417
var youtubeRegex = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;
match = url.match(youtubeRegex);
return match && url || null;
}
function entrypoint(queue, e) {
var rawUrl = e.linkUrl || e.selectedText || e.pageUrl;
console.log('[youvlc]', 'Raw URL:', rawUrl);
var url = extractUrl(rawUrl);
if (url) {
var data = { queue: queue, url: url };
chrome.runtime.sendNativeMessage('youvlc', data);
console.log('[youvlc]', 'Forwarding to host script');
console.log(data);
} else {
console.log('[youvlc]', "Couldn't find youtube link");
}
}
var defaultOptions = {
'contextMenuPlayVisible': true,
'contextMenuEnqueueVisible': false,
};
function updateMenus() {
chrome.contextMenus.removeAll(function () {
chrome.storage.sync.get(defaultOptions, function (values) {
console.log(values);
if (values['contextMenuPlayVisible'] !== false) {
chrome.contextMenus.create({
title: "Play in VLC",
contexts: ["link", "page", "selection"],
onclick: entrypoint.bind(null, false),
});
}
if (values['contextMenuEnqueueVisible'] !== false) {
chrome.contextMenus.create({
title: "Enqueue to VLC",
contexts: ["link", "page", "selection"],
onclick: entrypoint.bind(null, true),
});
}
});
});
}
// Installation Helper
// Echoing allowed_origin to add into app_manifest on first run to make it work
chrome.runtime.onInstalled.addListener(function (details) {
if (details.reason == "install") {
prompt('YouVLC extension ID (copy/paste in install script/app_manifest.json)', chrome.runtime.id);
}
});
chrome.storage.onChanged.addListener(function (changes) {
updateMenus();
});
updateMenus();