-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReddit_-_Fetch_YouTube_titles.user.js
123 lines (112 loc) · 3.69 KB
/
Reddit_-_Fetch_YouTube_titles.user.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
// ==UserScript==
// @name Reddit - Fetch YouTube titles
// @author Tailszefox
// @namespace localhost
// @description Display the title of YouTube videos
// @icon https://i.imgur.com/onrFu0I.png
// @include https://*.reddit.com/r/*/comments/*
// @version 1.1
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @grant GM_registerMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
function onLoad() {
var links = document.querySelectorAll(".commentarea")[0].querySelectorAll("a[href*='youtube.com'],a[href*='youtu.be']");
var i = 0;
for(i = 0; i < links.length; i++)
{
l = links[i];
var url = l.href;
var p = parse(url);
if(p)
{
request(l, p.vid);
}
}
}
function parse(url)
{
return /^https?:\/\/((www\.|m\.)?youtu(\.be|be\.(googleapis\.)?com).+v[=\/]|#p\/[a-z]\/.+\/|youtu\.be\/)([a-z0-9_-]{8,})(.*[#&\?]t=([0-9hms]+))?/i.exec(url) ? {vid:RegExp.$5, t:RegExp.$7} : null;
}
function request(l, vid)
{
var apiKey = GM_getValue("apiKey", null);
if(apiKey === null)
{
alert("Can't fetch YouTube titles; use userscript menu to enter API key.");
}
net.json('https://www.googleapis.com/youtube/v3/videos?id=' + vid + '&part=snippet&fields=items(snippet/title)&key=' + apiKey, function(code, obj, txt) {
var item = obj.items[0];
var title = item.snippet.title;
decorate(l, title);
});
}
function decorate(link, title)
{
link.innerHTML += " <span style=\"font-size: smaller;\">("+title+")</span>";
}
var net = {
info: function(li, f) {
var id = li.sid + li.vid;
if(typeof net.pending[id] != 'object') {
net.pending[id] = [f];
sites[li.sid].request(li.vid, function(vi) {
if(vi) {
cache.set(li.sid, li.vid, vi);
for(var i = net.pending[id].length; i--;) {
window.setTimeout(net.pending[id][i], 0, vi);
}
}
delete net.pending[id];
});
} else {
net.pending[id].push(f);
}
},
pending: {},
json: function(url, f) {
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function(req) {
var obj;
try {
obj = JSON.parse(req.responseText);
} catch(ex) {
log('JSON data from ' + url + ' could not be parsed: ' + ex + '\nHTTP: ' + req.status + '\nResponse: ' + req.responseText);
}
window.setTimeout(f, 0, req.status, obj, req.responseText);
},
onerror: function(req) {
log('Request to ' + url + ' failed.');
window.setTimeout(f, 0, req.status, null, req.responseText);
}
});
},
text: function(url, re, f) {
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function(req) {
var m = [], txt = req.responseText;
for(var i = 0, len = re.length; i < len; i++) {
m.push(re[i].exec(txt));
}
window.setTimeout(f, 0, req.status, m);
},
onerror: function(req) {
log('Request to ' + url + ' failed.');
window.setTimeout(f, 0, req.status, []);
}
});
}
};
function registerApiKey()
{
var apiKey = prompt("Please enter your YouTube API key");
GM_setValue("apiKey", apiKey);
}
GM_registerMenuCommand("Set API key", registerApiKey)
window.setTimeout(onLoad, 100);