-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoplay.js
117 lines (97 loc) · 2.86 KB
/
noplay.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
/*
* removeVideoAutoPlay
* - Deletes all the audio and video autoplay attribute
* This function is run after the DOM is rendered and loaded
*/
function removeVideoAutoPlay () {
var autoPlayItems = document.querySelectorAll('video[autoplay], audio[autoplay]')
for (var i = 0; i < autoPlayItems.length; i++) {
autoPlayItems[i].pause()
autoPlayItems[i].removeAttribute('autoplay')
}
}
/*
* removeYoutubeAutoplay
* - Removes the autoplay=1 query param on the Youtube Iframes src
*/
function removeYoutubeAutoplay () {
var youtubeIframes = document.querySelectorAll('iframe')
for (var i = 0; i < youtubeIframes.length; i++) {
var iframe = youtubeIframes[i]
var iframeSrc = iframe.src
if (iframeSrc.match(/autoplay=1/)) {
iframe.src = iframeSrc.replace('autoplay=1', '')
}
}
}
/*
* pauseAllPlayingItems
* - Pauses all the audio and videos in the document
*/
function pauseAllPlayingItems () {
var items = document.querySelectorAll('video, audio')
for (var i = 0; i < items.length; i++) {
items[i].pause()
}
}
/*
* registerObserver
* - Registers a MutationObserver (ie DOM Node observer)
* It will detect new video and audio nodes added to the DOM (lazyloading)
* And automatically remove their autoplay attribute
* It will also prevent adding the Youtube autoplay=1 attribute on the fly
* - Implements a fallback for older browsers (<IE11)
*/
function registerObserver () {
if (typeof window.MutationObserver !== 'function') {
return window.setInterval(function () {
removeVideoAutoPlay()
removeYoutubeAutoplay()
}, 200)
}
var observer = new window.MutationObserver(function (mutations) {
for (var i = 0; i < mutations.length; i++) {
var mutation = mutations[i]
// Prevent adding autoplay on the fly
if (mutation.attributeName === 'src' &&
mutation.target &&
mutation.target.tagName.match(/IFRAME/) &&
mutation.target.src.match(/autoplay=1/)) {
mutation.target.src = mutation.target.src.replace('autoplay=1', '')
}
if (!mutation.addedNodes) {
continue
}
for (var j = 0; j < mutation.addedNodes.length; j++) {
var addedNode = mutation.addedNodes[j]
if (!addedNode.tagName) {
continue
}
// Block lazyladed iframes
if (addedNode.tagName.match(/IFRAME/)) {
addedNode.src = addedNode.src.replace('autoplay=1', '')
}
// Block lazyladed video/audio
if (addedNode.tagName.match(/VIDEO|AUDIO/)) {
addedNode.removeAttribute('autoplay')
}
}
}
})
observer.observe(document.body, {
childList: true,
attributes: true,
characterData: false,
subtree: true
})
}
/*
* Initialize Noplay
*/
function init () {
removeVideoAutoPlay()
registerObserver()
pauseAllPlayingItems()
removeYoutubeAutoplay()
}
init()