25 lines
950 B
JavaScript
25 lines
950 B
JavaScript
/**
|
|||
|
|
* background.js —— 工具栏图标与快捷键的入口
|
||
|
|
*
|
||
|
|
* 拾取器已由 manifest 的 content_scripts 自动注入到每个页面(默认不激活),
|
||
|
|
* 所以点图标或按 Alt+Shift+Z 只需要给页面里那份发一条「切换」消息。
|
||
|
|
* 若该页面没有拾取器(刚装上扩展时已开着的旧标签、或内部页面),再补注入一份。
|
||
|
|
*/
|
||
|
|
function toggle(tab) {
|
||
|
|
if (!tab || !tab.id) return;
|
||
|
|
|
||
|
|
chrome.tabs.sendMessage(tab.id, { type: 'ui-picker-toggle' }, () => {
|
||
|
|
if (chrome.runtime.lastError) {
|
||
|
|
// 页面里还没有拾取器 → 补注入(chrome:// 等内部页面注入会失败,忽略)
|
||
|
|
chrome.scripting
|
||
|
|
.executeScript({ target: { tabId: tab.id }, files: ['picker.js'] })
|
||
|
|
.catch(() => {});
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
chrome.action.onClicked.addListener(toggle);
|
||
|
|
|
||
|
|
chrome.commands.onCommand.addListener((command, tab) => {
|
||
|
|
if (command === 'toggle-picker') toggle(tab);
|
||
|
|
});
|