Ensure globals.js is loaded prior to execution of dependent scripts
This commit prevents a race condition that causes global variables defined in globals.js to be inaccessible to the other content scripts
This commit is contained in:
10
globals.js
10
globals.js
@@ -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');
|
||||
}
|
||||
54
popup.js
54
popup.js
@@ -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 =
|
||||
'© ' + currentYear + ' <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>';
|
||||
|
||||
`© ${currentYear} <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>`;
|
||||
}
|
||||
|
||||
initializeButtons();
|
||||
updateCopyrightYear();
|
||||
|
||||
Reference in New Issue
Block a user