Merge pull request #17 from Joenahm1937/globalsRaceFix

Ensure globals.js is loaded prior to execution of dependent scripts
This commit is contained in:
qcrao
2024-04-09 14:11:48 +08:00
committed by GitHub
2 changed files with 51 additions and 47 deletions

View File

@@ -1,5 +1,8 @@
if (typeof window.globalsLoaded === 'undefined') {
console.log('globals.js loaded');
window.globalsLoaded = true;
let lastChecked = null;
let Selectors = {
@@ -14,3 +17,10 @@ let Selectors = {
};
let CHECKBOX_CLASS = 'conversation-checkbox';
window.lastChecked = lastChecked;
window.Selectors = Selectors;
window.CHECKBOX_CLASS = CHECKBOX_CLASS;
} else {
console.log('globals.js already loaded, skipping re-initialization');
}

View File

@@ -1,43 +1,37 @@
document.getElementById('add-checkboxes').addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
function loadGlobalsThenExecute(tabId, secondaryScript) {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['addCheckboxes.js']
});
});
});
document.getElementById('bulk-delete').addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
target: { tabId: tabId },
files: ['globals.js']
}, () => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['bulkDeleteConversations.js']
});
target: { tabId: tabId },
files: [secondaryScript]
});
});
}
document.getElementById('toggle-checkboxes').addEventListener('click', () => {
function addButtonListener(buttonId, scriptName) {
document.getElementById(buttonId).addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['toggleCheckboxes.js']
});
if (tab) {
loadGlobalsThenExecute(tab.id, scriptName);
}
});
});
}
document.getElementById('remove-checkboxes').addEventListener('click', () => {
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['removeCheckboxes.js']
});
});
});
function initializeButtons() {
addButtonListener('add-checkboxes', 'addCheckboxes.js');
addButtonListener('bulk-delete', 'bulkDeleteConversations.js');
addButtonListener('toggle-checkboxes', 'toggleCheckboxes.js');
addButtonListener('remove-checkboxes', 'removeCheckboxes.js');
}
// Update copyright year
function updateCopyrightYear() {
const currentYear = new Date().getFullYear();
document.getElementById('copyright').innerHTML =
'&copy; ' + currentYear + ' <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>';
`&copy; ${currentYear} <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>`;
}
initializeButtons();
updateCopyrightYear();