-
Notifications
You must be signed in to change notification settings - Fork 1
/
pf_video_embed.user.js
162 lines (127 loc) · 4.88 KB
/
pf_video_embed.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
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
// ==UserScript==
// @name ProgrammersForumVideoEmbed
// @namespace http://programmersforum.ru/
// @version 0.52
// @description replaces youtube and coub links with embedded video player frames
// @author Alex P
// @include *programmersforum.ru/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
// @grant none
// @downloadURL https://github.com/AlexP11223/ProgForumRuUserscripts/raw/master/pf_video_embed.user.js
// ==/UserScript==
(function() {
'use strict';
if (window.videoEmbedInitialized)
return;
window.videoEmbedInitialized = true;
var YOUTUBE_EMBED_TEMPLATE = '<iframe src="https://www.youtube.com/embed/_ID_" width="560" height="315" frameborder="0" allowfullscreen></iframe>';
var COUB_EMBED_TEMPLATE = '<iframe src="//coub.com/embed/_ID_?muted=false&autostart=false&originalSize=false&startWithHD=true" width="640" height="270" frameborder="0" allowfullscreen></iframe>';
function insertEmbed(origLinkNode, embedTemplate, id) {
var html = '<br/>' + embedTemplate.replace('_ID_', id) + '<br/>';
$(html).insertAfter(origLinkNode);
origLinkNode.addClass('_embedded');
}
function parseURL(url) {
var parser = document.createElement('a');
var searchDict = {};
parser.href = url;
var queries = parser.search.replace(/^\?/, '').split('&');
var i;
for (i = 0; i < queries.length; i++) {
var parts = queries[i].split('=');
searchDict[parts[0]] = parts[1];
}
return {
protocol: parser.protocol,
host: parser.host,
hostname: parser.hostname,
port: parser.port,
pathname: parser.pathname,
search: parser.search,
hash: parser.hash,
searchDict: searchDict,
pathParts: parser.pathname.substring(1).split('/')
};
}
function containsAny(str, substrings) {
for (var i = 0; i != substrings.length; i++) {
var substring = substrings[i];
if (str.indexOf(substring) != -1) {
return true;
}
}
return false;
}
function parseYoutube(href) {
function parseId(url) {
if (url.hostname.indexOf('youtu.be') != -1)
return url.pathParts[0];
if (url.searchDict['v'])
return url.searchDict['v'];
if (['watch', 'embed', 'v'].indexOf(url.pathParts[0]) != -1)
return url.pathParts[1];
return false;
}
function parseTime(s) {
try {
var parts = s.replace(/[^0-9]+/g, ' ').split(' ');
parts = $.grep(parts, function(el) { return el !== '';});
if (parts.length === 1) {
return parseInt(parts[0], 10);
} else if (parts.length === 2) {
return parseInt(parts[0], 10) * 60 + parseInt(parts[1], 10);
} else if (parts.length === 3) {
return parseInt(parts[0], 10) * 3600 + parseInt(parts[1], 10) * 60 + parseInt(parts[2], 10);
}
} catch (e) {
}
return 0;
}
var url = parseURL(href);
var domains = ['youtube.com', 'youtu.be'];
if (!containsAny(url.hostname, domains))
return false;
var id = parseId(url);
if (id) {
var ret = { id: id, params: { } };
if (url.searchDict['t']) {
var t = parseTime(url.searchDict['t']);
if (t) {
ret.params.start = t;
}
}
return ret;
}
return false;
}
function parseCoubId(href) {
var url = parseURL(href);
var domains = ['coub.com'];
if (!containsAny(url.hostname, domains))
return false;
if (['view', 'embed'].indexOf(url.pathParts[0]) != -1)
return url.pathParts[1];
return false;
}
var postBlocks = $('#posts, #post, td.alt1:has(hr)');
$.each(postBlocks.find('a[href*="youtu"], a[href*="coub"]'), function (i, link) {
if (!$(link).is(':visible') || $(link).is('._embedded'))
return;
// skip signature
if ($(link).parents('div').eq(0).is(':contains("__________________")'))
return;
var url = $(link).attr('href');
var youtube = parseYoutube(url);
if (youtube) {
var paramsStr = '';
if (!$.isEmptyObject(youtube.params)) {
paramsStr = '?' + $.param(youtube.params);
}
insertEmbed($(link), YOUTUBE_EMBED_TEMPLATE, youtube.id + paramsStr);
}
var coubId = parseCoubId(url);
if (coubId) {
insertEmbed($(link), COUB_EMBED_TEMPLATE, coubId);
}
});
})();