-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmbeddedYoutubeVideoForFacebook.user.js
60 lines (55 loc) · 3.12 KB
/
EmbeddedYoutubeVideoForFacebook.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
// ==UserScript==
// @name Embedded Youtube Video For Facebook
// @namespace https://openuserjs.org/users/Energy0124
// @version 0.1.2
// @description Convert Youtube video link in Facebook to embedded video, works for post and comment.
// @author Energy0124
// @copyright 2020, Energy01214
// @license MIT
// @match https://www.facebook.com/*
// @grant none
// ==/UserScript==
// based on https://greasyfork.org/en/scripts/404309-facebook-hide-ads-a-k-a-sponsored-posts
(function () {
'use strict';
const qS = (el, scope) => {
scope = (typeof scope == 'object') ? scope : document;
return scope.querySelector(el) || false;
},
targetNode = qS('body'),
observerConfig = {
attributes: false,
childList: true,
subtree: true
},
callback = function (mutationsList, observer) {
mutationsList.forEach(function (mutation) {
const youtube_parser = function (url) {
const regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/;
const match = url.match(regExp);
if (match && match[2].length === 11) {
return match[2];
} else {
//error
return "";
}
};
// find all posts with youtube link and convert them to embedded player
Array.from(document.querySelectorAll('a[href*="youtu"] > div > div > div > div > img')).map(x => x.parentElement.parentElement.parentElement.parentElement)
.map(x => {
return [x.parentElement, youtube_parser(decodeURIComponent(x.parentElement.href.replace("https://l.facebook.com/l.php?u=", "")))];
}).map(x => {
x[0].parentElement.innerHTML = `<div style="position: relative; width: 100%; height: 0; padding-bottom: 56.25%;"><iframe style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" src="https://www.youtube.com/embed/${x[1]}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></div>`
});
// find all comments with youtube link and convert them to embedded player
Array.from(document.querySelectorAll('a[href*="youtu"] > span'))
.map(x => {
return [x.parentElement, youtube_parser(decodeURIComponent(x.parentElement.href.replace("https://l.facebook.com/l.php?u=", "")))];
}).map(x => {
x[0].parentElement.parentElement.parentElement.innerHTML = `<div style="position: relative; width: 100%; height: 0; padding-bottom: 56.25%;"><iframe style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" src="https://www.youtube.com/embed/${x[1]}" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></div>`
});
});
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, observerConfig);
})();