-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
69 lines (59 loc) · 2.38 KB
/
content.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
let observer = new MutationObserver((mutations) => { // observe changes on the page, comments are loaded separately so we need this to wait for them
mutations.forEach((mutation) => {
if (!mutation.addedNodes) return
for (let i = 0; i < mutation.addedNodes.length; i++) {
let node = mutation.addedNodes[i];
if (node.id == "reply-dialog") { // we find the reply-dialog place holder
for (let j = 0; j < node.parentElement.childElementCount; j++) {
let possibleToolbar = node.parentElement.children[j];
if (possibleToolbar.id == "toolbar") { // let's find the heath/reply toolbar
var spanTag = createDeleteAllCommentsCTA();
possibleToolbar.appendChild(spanTag);
}
}
}
}
})
});
function createDeleteAllCommentsCTA() {
var spanTag = document.createElement("span");
spanTag.setAttribute( 'class', 'custom-youtube-button-class' );
spanTag.innerHTML = "DELETE All COMMENTS"
spanTag.style.font = "12px arial,serif";
spanTag.addEventListener("click", deleteAllButtonClicked);
return spanTag
}
function deleteAllButtonClicked(pointerEvent) {
searchingCommentParent = findMainParentOfReplyCTA(pointerEvent)
var referenceUrl = findUrlToUser(searchingCommentParent).href;
chrome.runtime.sendMessage({videoUrl: location.href}); // save video URL
chrome.runtime.sendMessage({toDeleteUserId: referenceUrl}); // save user URL
// get authentication token
window.open("https://accounts.google.com/o/oauth2/v2/auth?client_id="+google_login_client_id+"&redirect_uri=https://youtube.com/deleteallcomments&response_type=token&scope=https://www.googleapis.com/auth/youtube.force-ssl")
}
function findMainParentOfReplyCTA(pointerEvent) {
var searchingCommentParent = pointerEvent.currentTarget;
while(searchingCommentParent.id != "main") {
searchingCommentParent = searchingCommentParent.parentElement;
}
return searchingCommentParent;
}
function findUrlToUser(mainElement) {
for (var i = 0; i < mainElement.childElementCount; i++) {
if (mainElement.id == "author-text") {
return mainElement;
} else {
var childResult = findUrlToUser(mainElement.children[i]);
if (childResult != null) {
return childResult;
}
}
}
return null;
}
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});