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:
20
globals.js
20
globals.js
@@ -1,8 +1,11 @@
|
||||
console.log('globals.js loaded');
|
||||
if (typeof window.globalsLoaded === 'undefined') {
|
||||
console.log('globals.js loaded');
|
||||
|
||||
let lastChecked = null;
|
||||
window.globalsLoaded = true;
|
||||
|
||||
let Selectors = {
|
||||
let lastChecked = null;
|
||||
|
||||
let Selectors = {
|
||||
// Plus 用户的选择器
|
||||
conversationsCheckbox: '.conversation-checkbox:checked',
|
||||
deleteButton: '[class*="text-red"]',
|
||||
@@ -11,6 +14,13 @@ let Selectors = {
|
||||
// 其他 Plus 用户选择器...
|
||||
CONVERSATION_SELECTOR: 'div > div > div > div > div > div > nav > div > div > div > div > ol > li > div > a',
|
||||
TITLE_SELECTOR: '.relative.grow.overflow-hidden.whitespace-nowrap',
|
||||
};
|
||||
};
|
||||
|
||||
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');
|
||||
}
|
||||
58
popup.js
58
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']
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
// Update copyright year
|
||||
const currentYear = new Date().getFullYear();
|
||||
document.getElementById('copyright').innerHTML =
|
||||
'© ' + currentYear + ' <a href="https://github.com/qcrao/bulk-delete-chatGPT" target="_blank">qcrao@GitHub</a>';
|
||||
function initializeButtons() {
|
||||
addButtonListener('add-checkboxes', 'addCheckboxes.js');
|
||||
addButtonListener('bulk-delete', 'bulkDeleteConversations.js');
|
||||
addButtonListener('toggle-checkboxes', 'toggleCheckboxes.js');
|
||||
addButtonListener('remove-checkboxes', 'removeCheckboxes.js');
|
||||
}
|
||||
|
||||
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>`;
|
||||
}
|
||||
|
||||
initializeButtons();
|
||||
updateCopyrightYear();
|
||||
|
||||
Reference in New Issue
Block a user