Files
background.js
content.css
content.js
manifest.json
popup.css
popup.html
popup.js
Code
7 1,251
content.js
const DATA_POLL_INTERVAL = 500;
let latestData = {
pendingModerations: [],
pendingShifts: {}
};
let pollingInProgress = false;
let importInProgress = false;
let injectionTimer = null;
let pendingOverlay = null;
let toastTimer = null;
function sendMessage(message) {
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(message, (response) => {
if (chrome.runtime.lastError) {
reject(new Error(chrome.runtime.lastError.message));
return;
}
resolve(response);
});
});
}
function wait(milliseconds) {
return new Promise((resolve) => {
window.setTimeout(resolve, milliseconds);
});
}
function normalizedText(value) {
return String(value || '').trim().toLowerCase().replace(/[^a-z0-9]/g, '');
}
function showToast(message) {
const existingToast = document.querySelector('.trident-sync-toast');
if (existingToast) {
existingToast.remove();
}
const toast = document.createElement('div');
toast.className = 'trident-sync-toast';
toast.setAttribute('role', 'status');
toast.textContent = message;
document.body.appendChild(toast);
if (toastTimer) {
window.clearTimeout(toastTimer);
}
toastTimer = window.setTimeout(() => {
toast.remove();
toastTimer = null;
}, 3500);
}
async function pollData() {
if (pollingInProgress) {
return;
}
pollingInProgress = true;
try {
const response = await sendMessage({ type: 'READ_DATA' });
if (response?.ok) {
latestData = response.data;
} else if (response?.code !== 'FILE_NOT_CONNECTED') {
console.error('Trident data polling failed:', response?.error || 'Unknown error');
}
} catch (error) {
console.error('Trident data polling request failed:', error);
} finally {
pollingInProgress = false;
}
}
function getElementDescriptor(element) {
const label = element.labels?.[0]?.textContent || '';
return [
element.getAttribute('name'),
element.getAttribute('id'),
element.getAttribute('aria-label'),
element.getAttribute('placeholder'),
label
].join(' ').toLowerCase();
}
function findField(container, keywords, selector = 'input, textarea, select, [role="combobox"]') {
const fields = [...container.querySelectorAll(selector)];
return fields.find((field) => {
const descriptor = getElementDescriptor(field);
return keywords.some((keyword) => descriptor.includes(keyword));
}) || null;
}
function setInputValue(field, value) {
const stringValue = value == null ? '' : String(value);
const prototype = field instanceof HTMLTextAreaElement
? HTMLTextAreaElement.prototype
: HTMLInputElement.prototype;
const valueSetter = Object.getOwnPropertyDescriptor(prototype, 'value')?.set;
if (valueSetter) {
valueSetter.call(field, stringValue);
} else {
field.value = stringValue;
}
field.dispatchEvent(new Event('input', { bubbles: true }));
field.dispatchEvent(new Event('change', { bubbles: true }));
}
function findActionControl(container) {
const select = findField(container, ['action', 'moderation action', 'type'], 'select');
if (select) {
return select;
}
return findField(container, ['action', 'moderation action', 'type'], '[role="combobox"], button');
}
async function setAction(container, action) {
const actionControl = findActionControl(container);
if (!actionControl) {
throw new Error('Could not find the moderation action control.');
}
const wantedAction = normalizedText(action);
if (actionControl instanceof HTMLSelectElement) {
const matchingOption = [...actionControl.options].find((option) => {
return normalizedText(option.value) === wantedAction ||
normalizedText(option.textContent) === wantedAction;
});
if (!matchingOption) {
throw new Error(`The moderation action "${action}" is not available.`);
}
actionControl.value = matchingOption.value;
actionControl.dispatchEvent(new Event('input', { bubbles: true }));
actionControl.dispatchEvent(new Event('change', { bubbles: true }));
return;
}
actionControl.click();
await wait(80);
const options = [...document.querySelectorAll('[role="option"], option, li, [data-value]')];
const matchingOption = options.find((option) => {
return normalizedText(option.textContent) === wantedAction ||
normalizedText(option.getAttribute('data-value')) === wantedAction;
});
if (!matchingOption) {
throw new Error(`The moderation action "${action}" is not available.`);
}
matchingOption.click();
}
function findCreateModerationButton(container) {
const buttons = [...container.querySelectorAll('button, [role="button"]')];
return buttons.find((button) => {
const text = normalizedText(button.textContent);
return text.includes('createmoderation') ||
text === 'create' ||
text.includes('submitmoderation');
}) || null;
}
function findModerationContainer() {
const createButton = [...document.querySelectorAll('button, [role="button"]')].find((button) => {
return normalizedText(button.textContent).includes('createmoderation');
});
if (!createButton) {
return null;
}
return createButton.closest('form, [role="dialog"], section, main') ||
createButton.parentElement;
}
function applyButtonStyle(button, referenceButton) {
if (!referenceButton) {
return;
}
const styles = window.getComputedStyle(referenceButton);
button.style.setProperty('--trident-sync-border', styles.border);
button.style.setProperty('--trident-sync-radius', styles.borderRadius);
button.style.setProperty('--trident-sync-background', styles.backgroundColor);
button.style.setProperty('--trident-sync-color', styles.color);