-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent-modify.js
More file actions
157 lines (141 loc) · 5.88 KB
/
content-modify.js
File metadata and controls
157 lines (141 loc) · 5.88 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
console.log = function() {};
// The Intervention mechanism -------
chrome.storage.sync.get('temporaryRedirectDisable', function(data) {
console.log(data);
if (!data.temporaryRedirectDisable) {
const interventionUrl = chrome.runtime.getURL("intervention.html");
console.log("InterventionURL is", interventionUrl);
window.location.href = interventionUrl;
} else {
initPostButtonModifier();
}
});
// End Intervention mechanism ------
// Post button modifier - shows "Post as @handle" ------
function getCurrentHandle() {
const switcher = document.querySelector('[data-testid="SideNav_AccountSwitcher_Button"]');
if (switcher) {
for (const span of switcher.querySelectorAll('span')) {
const text = span.textContent.trim();
if (text.startsWith('@')) return text;
}
}
const profileLink = document.querySelector('[data-testid="AppTabBar_Profile_Link"]');
if (profileLink) {
const href = profileLink.getAttribute('href');
if (href) return '@' + href.replace('/', '');
}
return null;
}
function modifyPostButtons(handle) {
const selectors = '[data-testid="tweetButton"], [data-testid="tweetButtonInline"]';
document.querySelectorAll(selectors).forEach(button => {
if (button.dataset.xcoachHandleApplied) return;
const innerSpan = button.querySelector('span > span');
if (innerSpan && innerSpan.textContent.trim() === 'Post') {
innerSpan.textContent = 'Post as ' + handle;
button.dataset.xcoachHandleApplied = 'true';
}
});
}
function initPostButtonModifier() {
let cachedHandle = null;
let debounceTimer = null;
let lastUrl = location.href;
function processDOM() {
if (location.href !== lastUrl) {
lastUrl = location.href;
cachedHandle = null;
}
if (!cachedHandle) cachedHandle = getCurrentHandle();
if (cachedHandle) modifyPostButtons(cachedHandle);
}
const observer = new MutationObserver(() => {
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(processDOM, 300);
});
if (document.body) {
observer.observe(document.body, { childList: true, subtree: true });
processDOM();
} else {
document.addEventListener('DOMContentLoaded', () => {
observer.observe(document.body, { childList: true, subtree: true });
processDOM();
});
}
}
// End Post button modifier ------
// Runtime message handler -----
handleOnMessageContent = (request, sender, sendResponse) => {
if (request.action === "warnClose") { // As in, warn that it's about to close
let warningBanner = document.createElement('div');
warningBanner.id = 'RedWarningBannerId';
// Banner styling
warningBanner.style.position = 'fixed';
warningBanner.style.top = '0';
warningBanner.style.left = '50%';
warningBanner.style.transform = 'translateX(-60%)';
warningBanner.style.backgroundColor = '#FF4500';
warningBanner.style.color = 'white';
warningBanner.style.textAlign = 'center';
warningBanner.style.paddingLeft = '20px';
warningBanner.style.paddingRight = '20px';
warningBanner.style.paddingTop = '10px';
warningBanner.style.paddingBottom = '10px';
warningBanner.style.borderRadius = '10px';
warningBanner.style.zIndex = '1000';
warningBanner.innerText = 'XCoach: X tabs will close in under a minute!';
// Dismiss button
let dismissBtn = document.createElement('button');
dismissBtn.innerText = 'Dismiss';
dismissBtn.style.marginLeft = '15px';
dismissBtn.style.backgroundColor = '#555555';
dismissBtn.onclick = function() {
warningBanner.remove();
};
warningBanner.appendChild(dismissBtn);
// Snooze button
let snoozeBtn = document.createElement('button');
snoozeBtn.innerText = 'Snooze';
snoozeBtn.style.backgroundColor = '#555555';
snoozeBtn.style.marginLeft = '15px';
snoozeBtn.onclick = function() {
let snoozeMinutes = parseInt(snoozeInput.value, 10);
if (!isNaN(snoozeMinutes) && snoozeMinutes >= 2 && snoozeMinutes <= 120) {
chrome.runtime.sendMessage({ action: "snooze", minutes: snoozeMinutes });
} else {
alert('Please enter a valid number of minutes between 2 and 120.');
}
};
warningBanner.appendChild(snoozeBtn);
// Input box for custom snooze time
let snoozeInput = document.createElement('input');
snoozeInput.type = 'number';
snoozeInput.value = '2';
snoozeInput.min = '2';
snoozeInput.max = '120';
snoozeInput.step = '1';
snoozeInput.style.marginLeft = '15px';
snoozeInput.style.padding = '5px';
snoozeInput.style.borderRadius = '5px';
snoozeInput.style.border = '1px solid #ccc';
snoozeInput.style.width = '40px';
warningBanner.appendChild(snoozeInput);
// Finish word minutes
let minutesText = document.createElement('span');
minutesText.innerText = 'minutes';
minutesText.style.marginLeft = '5px';
minutesText.style.color = 'white'; // Ensure text color matches the banner
warningBanner.appendChild(minutesText);
// Finished creating the warning banner
document.body.appendChild(warningBanner);
sendResponse();
} else if (request.action === "removeWarning") {
let warningBanner = document.getElementById('RedWarningBannerId');
warningBanner.remove();
console.log('Tried to remove warning banner');
sendResponse();
}
return true; // if I was to use sendResponse, I'd do so asyncronously. Keep channel open.
};
chrome.runtime.onMessage.addListener(handleOnMessageContent);