This commit is contained in:
hsueh chiahao
2025-10-10 09:39:55 +08:00
parent dfedbf2ef6
commit a5dead4259
5 changed files with 580 additions and 306 deletions

View File

@@ -1,22 +1,42 @@
chrome.action.onClicked.addListener(async (tab) => {
/**
* Injects the content scripts into the active tab to enable Kintone helper functionality
* @param {Object} tab - The active tab object from Chrome API
* @returns {Promise<void>} Resolves when all scripts are injected successfully
*/
const injectKintoneHelperScripts = async (tab) => {
const scriptFiles = ['fields.js', 'dom.js', 'main.js'];
try {
// Inject the modules and then execute the main function
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["fields.js"],
world: "MAIN"
});
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["dom.js"],
world: "MAIN"
});
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ["main.js"],
world: "MAIN"
});
// Inject all scripts in parallel for better performance
await Promise.all(
scriptFiles.map(file =>
chrome.scripting.executeScript({
target: { tabId: tab.id },
files: [file],
world: 'MAIN',
})
)
);
console.log('Kintone helper scripts injected successfully');
} catch (error) {
console.error("Error injecting scripts:", error);
console.error('Failed to inject Kintone helper scripts:', error);
throw error; // Re-throw to allow caller to handle if needed
}
});
};
/**
* Handles the extension action click event
* @param {Object} tab - The active tab that triggered the action
*/
const handleActionClick = async (tab) => {
try {
await injectKintoneHelperScripts(tab);
} catch (error) {
// Error already logged in injectKintoneHelperScripts
// Could add user notification here if needed
}
};
// Register the click handler for the browser action
chrome.action.onClicked.addListener(handleActionClick);