refactor
This commit is contained in:
@@ -1,42 +1,34 @@
|
||||
/**
|
||||
* 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
|
||||
* Chrome 扩展后台脚本
|
||||
* 负责将内容脚本注入 Kintone 页面
|
||||
*/
|
||||
|
||||
import { SCRIPT_FILES } from './utils/constants.js';
|
||||
|
||||
/**
|
||||
* 将 Kintone Helper 脚本注入指定标签页
|
||||
* @param {Object} tab - Chrome 标签页对象
|
||||
* @returns {Promise<void>} 当脚本注入成功时解决
|
||||
*/
|
||||
const injectKintoneHelperScripts = async (tab) => {
|
||||
const scriptFiles = ['fields.js', 'dom.js', 'main.js'];
|
||||
|
||||
try {
|
||||
// Inject all scripts in parallel for better performance
|
||||
// 将所有脚本并行注入
|
||||
await Promise.all(
|
||||
scriptFiles.map(file =>
|
||||
SCRIPT_FILES.map(scriptFile =>
|
||||
chrome.scripting.executeScript({
|
||||
target: { tabId: tab.id },
|
||||
files: [file],
|
||||
files: [scriptFile],
|
||||
world: 'MAIN',
|
||||
})
|
||||
)
|
||||
);
|
||||
|
||||
console.log('Kintone helper scripts injected successfully');
|
||||
} catch (error) {
|
||||
console.error('Failed to inject Kintone helper scripts:', error);
|
||||
throw error; // Re-throw to allow caller to handle if needed
|
||||
throw new Error(`Kintone Helper script injection failed: ${error.message}`);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 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);
|
||||
// 当点击扩展,开始执行脚本注入
|
||||
chrome.action.onClicked.addListener(async (tab) => {
|
||||
await injectKintoneHelperScripts(tab);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user