forked from viperrnya/TwitVx-Extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
139 lines (131 loc) · 7.11 KB
/
main.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
var inter; //interval
function specparent(a, b) { //returns parent element with specific tag, a is child and b is parent's tag
for (b = b.toLowerCase(); a && a.parentNode;)
if (a = a.parentNode, a.tagName && a.tagName.toLowerCase() == b) return a;
return null
}
function styleAppend() {
const css = `
.vxtwitterclass {
background-color: unset;
}
body[style*="background-color: rgb(21, 32, 43)"] .vxtwitterclass:hover,
body[style*="background-color: #15202B"] .vxtwitterclass:hover {
background-color: rgba(255, 255, 255, 0.03);
}
body[style*="background-color: rgb(255, 255, 255)"] .vxtwitterclass:hover,
body[style*="background-color: #FFFFFF"] .vxtwitterclass:hover {
background-color: rgba(0, 0, 0, 0.03);
}`; //stylesheet for the new rclick items, it is best to keep this here so twitter doesnt overwrite it
const style = document.createElement('style'); //creates style el
style.styleSheet ? style.styleSheet.cssText = css : style.appendChild(document.createTextNode(css));
style.id = "TwitVx";
document.getElementsByTagName('head')[0].appendChild(style); //appends to head
}
document.addEventListener('contextmenu', (e) => { //event for right click
const varray = Array.from(document.querySelectorAll("div[data-testid*='videoPlayer']")).filter(i => i.contains(e.target)); //this is array of targets
const videoPlayer = varray[0];
const article = specparent(videoPlayer, "article");
if (varray.length > 0 && Array.from(article.querySelectorAll("a")).filter(a => a.href.includes("broadcasts")).length < 1 && videoPlayer != article.querySelector("div[role=\"link\"] [data-testid=\"videoPlayer\"]")) { //checks if rclick was over video player
const atags = Array.from(article.querySelectorAll("a"));
const url = atags.filter(a => a.href.includes("status"))[0].href; //url is pulled from a A tag inside the tweet (article)
if (!document.getElementById("TwitVx"))
styleAppend();
function change() { //function for simplicity
const ogitem = videoPlayer.querySelector("div[role*='menuitem']"); //original item <> Copy Video Adress
const flex = ogitem.parentElement; //container/parent
if (!flex.contains(flex.querySelector(".vxtwitterclass"))) { //checks if vxtwitter items are already there
const opt1 = ogitem.cloneNode(true); //clones a copy of ogitem
opt1.querySelector("span").textContent = "Copy TwitVx Link"; //item text
opt1.classList.add("vxtwitterclass"); //class
opt1.addEventListener("click",
() => {
navigator.clipboard.writeText(url.replace("https://", "https://vx")) //adds click event and using navigator api, copies the url to clipboard
document.body.click();
}
);
const opt2 = ogitem.cloneNode(true); //same as above
opt2.querySelector("span").textContent = "Copy MP4 Link";
opt2.classList.add("vxtwitterclass");
opt2.addEventListener("click",
() => {
navigator.clipboard.writeText(url.replace("https://", "https://vx").concat(".mp4"));
document.body.click();
}
);
flex.append(opt1); //appends to container/parent
flex.append(opt2);
}
}
//Interval that checks every 4ms after rclick, waits until twitter's context menu appears then applies change and finally stops
//Todo: Find a better way to handle this, current imp. can cause potential bugs
var step = 0; //added so it stops after a while
if (!inter) {
inter = setInterval(() => {
step += 1;
if (videoPlayer.querySelector("div[role*='menuitem']") != null) { //checks if twitter's context menu exists
change();
step = 0;
clearInterval(inter);
inter = "";
} else if (step > 10000) {
step = 0;
clearInterval(inter);
inter = "";
}
}, 4);
}
}
}, false);
window.onload = () => {
styleAppend();
setInterval(() => {
if (document.querySelector("div:not(.vxTwittered)[class][role*='button'][aria-label*='Share Tweet']") != null) {
shareAppend();
}
}, 4);
};
function shareAppend() {
document.querySelectorAll("div:not(.vxTwittered)[class][role*='button'][aria-label*='Share Tweet']").forEach(shrbtn => {
shrbtn.classList.add("vxTwittered");
shrbtn.addEventListener("click", () => {
if (!document.getElementById("TwitVx"))
styleAppend();
function shareChange() {
const article = specparent(shrbtn, "article");
const atags = Array.from(article.querySelectorAll("a"));
const url = atags.filter(a => a.href.includes("status") && !a.href.includes("photo") && !a.href.includes("video"))[0].href; //url is pulled from a A tag inside the tweet (article)
const ogitem = document.querySelectorAll("div[role*='menuitem']")[2] ? document.querySelectorAll("div[role*='menuitem']")[2] : document.querySelector("div[role*='menuitem']");
const flex = ogitem.parentElement; //not actual menu but container of menuitems
const menu = document.querySelector("div[role*='menu']");
if (!flex.contains(flex.querySelector(".vxtwitterclass"))) { //checks if vxtwitter items are already there
const opt1 = ogitem.cloneNode(true); //clones a copy of ogitem
opt1.querySelector("span").textContent = "Copy TwitVx link"; //item text
opt1.classList.add("vxtwitterclass"); //class
opt1.addEventListener("click", () => {
navigator.clipboard.writeText(url.replace("https://", "https://vx"));
menu.remove();
}); //adds click event and using navigator api, copies the url to clipboard);
flex.insertBefore(opt1, ogitem.nextSibling); //appends to container/parent
}
}
//Todo: Find a better way to handle this, current imp. can cause potential bugs
var step = 0; //added so it stops after a while
if (!inter) {
inter = setInterval(() => {
step++;
if (document.querySelector("div[role*='menuitem']") != null) {
shareChange();
step = 0;
clearInterval(inter);
inter = "";
} else if (step > 10000) {
step = 0;
clearInterval(inter);
inter = "";
}
}, 4);
}
}, false);
});
}