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');
|
console.log('globals.js loaded');
|
||||||
|
|
||||||
|
window.globalsLoaded = true;
|
||||||
|
|
||||||
let lastChecked = null;
|
let lastChecked = null;
|
||||||
|
|
||||||
let Selectors = {
|
let Selectors = {
|
||||||
@@ -14,3 +17,10 @@ let Selectors = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let CHECKBOX_CLASS = 'conversation-checkbox';
|
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', () => {
|
function loadGlobalsThenExecute(tabId, secondaryScript) {
|
||||||
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
|
|
||||||
chrome.scripting.executeScript({
|
chrome.scripting.executeScript({
|
||||||
target: { tabId: tab.id },
|
target: { tabId: tabId },
|
||||||
files: ['addCheckboxes.js']
|
files: ['globals.js']
|
||||||
});
|
}, () => {
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
document.getElementById('bulk-delete').addEventListener('click', () => {
|
|
||||||
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
|
|
||||||
chrome.scripting.executeScript({
|
chrome.scripting.executeScript({
|
||||||
target: { tabId: tab.id },
|
target: { tabId: tabId },
|
||||||
files: ['bulkDeleteConversations.js']
|
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.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
|
||||||
chrome.scripting.executeScript({
|
if (tab) {
|
||||||
target: { tabId: tab.id },
|
loadGlobalsThenExecute(tab.id, scriptName);
|
||||||
files: ['toggleCheckboxes.js']
|
}
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
}
|
||||||
|
|
||||||
document.getElementById('remove-checkboxes').addEventListener('click', () => {
|
function initializeButtons() {
|
||||||
chrome.tabs.query({ active: true, currentWindow: true }, ([tab]) => {
|
addButtonListener('add-checkboxes', 'addCheckboxes.js');
|
||||||
chrome.scripting.executeScript({
|
addButtonListener('bulk-delete', 'bulkDeleteConversations.js');
|
||||||
target: { tabId: tab.id },
|
addButtonListener('toggle-checkboxes', 'toggleCheckboxes.js');
|
||||||
files: ['removeCheckboxes.js']
|
addButtonListener('remove-checkboxes', 'removeCheckboxes.js');
|
||||||
});
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
|
function updateCopyrightYear() {
|
||||||
// Update copyright year
|
|
||||||
const currentYear = new Date().getFullYear();
|
const currentYear = new Date().getFullYear();
|
||||||
document.getElementById('copyright').innerHTML =
|
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