-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
108 lines (87 loc) · 3.4 KB
/
content.js
File metadata and controls
108 lines (87 loc) · 3.4 KB
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
// Define the button HTML outside the function to avoid recreating it unnecessarily
const buttonHtml = `<div id="supercast-button" class="mt-3 hidden rounded-lg px-2 py-3 pt-1.5 bg-overlay-light mdlg:block"><div class="flex flex-col items-center pt-1"> <form class="w-full"><button type="submit" class="rounded-lg font-semibold disabled:opacity-50 border bg-action-muted border-default px-4 py-2 text-sm w-full">Open in Supercast</button></form> </div></div>`;
function getSupercastUrl() {
let url = window.location.pathname;
url = url.replace('/~/', '/');
return `https://supercast.xyz${url}`;
}
function injectButton() {
// Get the current URL path
let urlPath = window.location.pathname;
// Check if the URL matches the specified patterns
const pattern1 = /^(?!.*\/~\/)(?:\/[^\/]+(?:\/[a-zA-Z0-9]*)?)$/;
// const pattern2 = /^\/~\/channel(\/|$)/;
checkDomExists = document.getElementById('supercast-button');
if (!pattern1.test(urlPath)) {
console.log('URL does not match the required patterns.');
if (checkDomExists) {
checkDomExists.remove();
}
return;
}
const asideArr = document.querySelectorAll('aside');
const aside = asideArr[1];
if (!aside) {
console.error('Aside not found');
return;
}
// Check if the button already exists
let buttonDOM = aside.querySelector('#supercast-button');
if (!buttonDOM) {
// Create the button if it doesn't exist
buttonDOM = new DOMParser().parseFromString(buttonHtml, 'text/html').body.firstChild;
if (!buttonDOM) {
console.error('Button DOM not found');
return;
}
// Initially set the supercast url to the form action
buttonDOM.querySelector('form').setAttribute('action', getSupercastUrl());
// Insert the button into the aside
const hasInviteBox = Array.from(aside.children).find(div => div.textContent?.includes('Invite friends, earn warps'));
if (!hasInviteBox) {
aside.children[1].replaceWith(buttonDOM);
} else {
const children = Array.from(aside.children);
children.splice(1, 0, buttonDOM);
aside.innerHTML = '';
children.forEach(child => aside.appendChild(child));
}
} else {
// Update the supercast url if the button already exists
buttonDOM.querySelector('form').setAttribute('action', getSupercastUrl());
}
return true;
}
const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;
history.pushState = function () {
originalPushState.apply(history, arguments);
injectButton();
};
history.replaceState = function () {
originalReplaceState.apply(history, arguments);
injectButton();
};
window.addEventListener('popstate', () => {
injectButton();
});
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
injectButton();
}
});
});
observer.observe(document.body, { childList: true, subtree: true });
let attempts = 0;
const maxAttempts = 30;
const interval = 1000;
setTimeout(() => {
const intervalId = setInterval(() => {
const success = injectButton();
attempts++;
if (success || attempts >= maxAttempts) {
clearInterval(intervalId);
}
}, interval);
}, 3000);