background.js 1.1 KB
Newer Older
B
baiy 已提交
1
let windowId = null;
2 3 4 5 6 7
// 打开独立窗口
const panel = {
    create() {
        chrome.windows.create({
            url: chrome.runtime.getURL("tool.html"),
            type: "popup",
B
baiy 已提交
8
            width: 810,
9 10
            left: 200,
            top: 200,
B
baiy 已提交
11
            height: 610,
12
        }, (w) => {
B
baiy 已提交
13
            windowId = w.id
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
        })
    },
    open() {
        if (windowId === null) {
            this.create()
        } else {
            chrome.windows.get(windowId, (w) => {
                if (!w) {
                    this.create()
                } else {
                    chrome.windows.update(windowId, {focused: true})
                }
            })
        }

    },
    onRemoved(id) {
B
baiy 已提交
31 32
        if (id === windowId) {
            windowId = null;
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
        }
    }
}

// 注册快捷键
chrome.commands.onCommand.addListener((command) => {
    switch (command) {
        case "panel":
            panel.open()

            break;
        default:
            return;
    }
})

// 窗口关闭事件
chrome.windows.onRemoved.addListener((id) => {
    panel.onRemoved(id);
B
fix  
baiy 已提交
52
})