Ville vakoilukoodi

21. syyskuuta 2022 | 2 näyttökertaa

var handleTimer, insertWidgetTabs = [],
    resolutionList = [],
    presentationStore = [],
    isRecording = !1,
    contextMenuInsert = !1,
    startRecordTabId = -1,
    currentSource = {
        videoIndex: 0,
        videoDeviceId: "",
        micIndex: 0,
        micDeviceId: "",
        width: 0,
        height: 0,
        micVolume: 1
    },
    canelRecordWithUpdate = !1;
chrome.runtime.onInstalled.addListener((function() {
    chrome.tabs.query({
        currentWindow: !0
    }, (function(e) {
        for (let t = 0; t < e.length; t++) e[t].url.includes("chrome://") || e[t].url.includes("chrome.com") || "complete" == e[t].status && chrome.scripting.executeScript({
            target: {
                tabId: e[t].id
            },
            files: ["./js/detect.js"]
        }, (function(e) {
            !chrome.runtime.lastError && e && e.length || console.log("Permission error, tab closed, etc.")
        }))
    }))
})), console.log("Service Worker init"), chrome.storage.local.get(["onboarddone"], (function(e) {
    checkPermissionGrant().then((e => {
        e ? createInsertWidgetMenu() : removeInsertWidgetMenu()
    }))
})), chrome.storage.local.get(["contextMenuInsert"], (function(e) {
    e && (contextMenuInsert = e.contextMenuInsert)
}));
var tabList = [],
    tmpWidgetTabs = [],
    index = 0;

function scanAndUpdateWidgetTabList() {
    index = 0, insertWidgetTabs = [], chrome.tabs.query({
        currentWindow: !0
    }, (function(e) {
        tabList = e
    })), readTabListWithWidget().then((e => {
        e && (tmpWidgetTabs = e)
    })).then((() => {
        scanTabWithWidget(tmpWidgetTabs, tabList)
    }))
}

function scanTabWithWidget(e, t) {
    if (index < e.length) {
        for (var n = !1, r = 0; r < t.length; r++)
            if (t[r].id == e[index].tabId) {
                n = !0;
                break
            } n && chrome.tabs.sendMessage(e[index].tabId, {
            type: "check-script-inject"
        }, (function(n) {
            n && n.isInject && insertWidgetTabs.push(e[index]), index += 1, scanTabWithWidget(e, t)
        }))
    }
    if (index >= e.length && (chrome.storage.local.set({
            insertWidgetTabs: insertWidgetTabs
        }), 0 == insertWidgetTabs.length)) {
        console.log("Reset the video state parameter");
        chrome.storage.local.set({
            isRecording: isRecording
        }), chrome.storage.local.set({
            startRecordTabId: startRecordTabId
        }), chrome.storage.local.set({
            isFreeze: !1
        }), chrome.storage.local.set({
            isPauseRecord: !1
        })
    }
}

function createInsertWidgetMenu() {
    contextMenuInsert || chrome.contextMenus.create({
        type: "normal",
        title: "Insert Sphere Lite Widget",
        id: "sphereLiteWidgetItem",
        contexts: ["all"]
    }, (function(e) {
        if (chrome.runtime.lastError || !e || !e.length) return contextMenuInsert = !0, void chrome.storage.local.set({
            contextMenuInsert: contextMenuInsert
        });
        contextMenuInsert = !0, chrome.storage.local.set({
            contextMenuInsert: contextMenuInsert
        }), console.log("contextMenus are create.")
    }))
}

function removeInsertWidgetMenu() {
    contextMenuInsert && chrome.contextMenus.remove("sphereLiteWidgetItem", (function() {
        contextMenuInsert = !1, chrome.storage.local.set({
            contextMenuInsert: contextMenuInsert
        }), console.log("contextMenus was removed.")
    }))
}

function openFileExplor() {
    console.log("openFileExplor()"), window_options = {
        url: chrome.runtime.getURL("main.html?requset=openExplor"),
        active: !0,
        selected: !0
    }, chrome.tabs.create(window_options, (function(e) {
        console.log("New tab. " + e.id)
    }))
}

function rightClickToInsert() {
    chrome.tabs.query({
        active: !0,
        currentWindow: !0
    }, (function(e) {
        checkUrlIsSildeEdit(e[0].url) || e[0].url.includes("chrome://") || e[0].url.includes("chrome.com") || e[0].url.includes("chrome-extension") || (checkPermissionGrant().then((t => {
            t || chrome.tabs.sendMessage(e[0].id, {
                type: "source-request"
            })
        })), getRequestTabIndex(e[0].id).then((e => {
            -1 == e.index && prepareWidgetInsert()
        })))
    }))
}

function prepareWidgetInsert(e) {
    chrome.tabs.query({
        active: !0,
        currentWindow: !0
    }, (function(t) {
        if (t[0].url.includes("chrome://") || t[0].url.includes("chrome.com") || t[0].url.includes("chrome-extension")) console.log("Chrome page, can not insert widget");
        else if (t[0].url.includes("google.com/presentation") && t[0].url.includes("edit#") && !t[0].url.includes("slide=id")) console.log("In slide page, but not in edit mode");
        else {
            var n = t[0].id;
            console.log("insertTabId : " + e), e && (n = e), chrome.tabs.sendMessage(n, {
                type: "check-script-inject"
            }, (function(e) {
                if (e)
                    if (e.isInject) t[0].url.includes("google.com/presentation") ? t[0].url.includes("edit#slide") ? (console.log("google slide page"), getRequestTabIndex(n).then((e => {
                        if (-1 != e.index) {
                            var r = parsePresentationId(t[0].url),
                                i = getSlidePageId(t[0].url);
                            chrome.tabs.sendMessage(n, {
                                type: "slideinsert-request",
                                tabId: n,
                                gpId: r,
                                pageId: i
                            })
                        }
                    }))) : reinsertWidgetToTab(n, t[0].url) : (console.log("the tab with inset script"), console.log(t[0].url), reinsertWidgetToTab(n, t[0].url));
                    else if (console.log("new tab widthout insert"), addTabWithWidget(n, t[0].url), chrome.scripting.executeScript({
                        target: {
                            tabId: n
                        },
                        files: ["./js/lib/jquery-3.5.1.min.js"]
                    }), chrome.scripting.executeScript({
                        target: {
                            tabId: n
                        },
                        files: ["./js/lib/jquery-ui.js"]
                    }), chrome.scripting.executeScript({
                        target: {
                            tabId: n
                        },
                        files: ["./js/widget/content.js"]
                    }), canelRecordWithUpdate && (chrome.scripting.executeScript({
                        target: {
                            tabId: n
                        },
                        files: ["./js/widget/alertMessage.js"]
                    }), canelRecordWithUpdate = !1), chrome.scripting.insertCSS({
                        target: {
                            tabId: n
                        },
                        files: ["./css/content.css"]
                    }), chrome.scripting.insertCSS({
                        target: {
                            tabId: n
                        },
                        files: ["./css/jquery-ui.css"]
                    }), chrome.tabs.sendMessage(n, {
                        type: "insert-widget-changed",
                        isInsert: !0
                    }), chrome.tabs.sendMessage(n, {
                        type: "notify-inject-content",
                        isInject: !0
                    }), t[0].url.includes("google.com/presentation") && t[0].url.includes("edit#slide")) {
                    var r = parsePresentationId(t[0].url),
                        i = getSlidePageId(t[0].url);
                    console.log("gpId :" + r + ", pageId:" + i), chrome.tabs.sendMessage(n, {
                        type: "slideinsert-request",
                        tabId: n,
                        gpId: r,
                        pageId: i
                    })
                }
            }))
        }
    }))
}

function restoreSlideWidgetConfig(e, t) {
    e.includes("chrome://") || e.includes("chrome.com") || e.includes("chrome-extension") ? console.log("Chrome page, can not insert widget") : (tabId = t.id, chrome.tabs.sendMessage(tabId, {
        type: "check-script-inject"
    }, (function(e) {
        e && (e.isInject || (console.log("new tab without insert"), addTabWithWidget(tabId, t.url), chrome.scripting.executeScript({
            target: {
                tabId: tabId
            },
            files: ["./js/lib/jquery-3.5.1.min.js"]
        }), chrome.scripting.executeScript({
            target: {
                tabId: tabId
            },
            files: ["./js/lib/jquery-ui.js"]
        }), chrome.scripting.executeScript({
            target: {
                tabId: tabId
            },
            files: ["./js/widget/content.js"]
        }), chrome.scripting.insertCSS({
            target: {
                tabId: tabId
            },
            files: ["./css/content.css"]
        }), chrome.scripting.insertCSS({
            target: {
                tabId: tabId
            },
            files: ["./css/jquery-ui.css"]
        }), chrome.tabs.sendMessage(tabId, {
            type: "insert-widget-changed",
            isInsert: !0
        }), chrome.tabs.sendMessage(tabId, {
            type: "notify-inject-content",
            isInject: !0
        })))
    })))
}

function reinsertWidgetToTab(e, t) {
    chrome.tabs.sendMessage(e, {
        type: "check-already-insert"
    }, (function(n) {
        n && (n.isInsert || (console.log("the tab has no widget"), addTabWithWidget(e, t), chrome.tabs.sendMessage(e, {
            type: "insert-widget-changed",
            isInsert: !0
        }), chrome.tabs.sendMessage(e, {
            type: "re-insert-widget"
        })))
    }))
}

function addTabWithWidget(e, t) {
    var n = [];
    readTabListWithWidget().then((r => {
        console.log("tabs with widget : " + r);
        for (var i = (n = r).length, o = !1, s = 0; s < i; s++)
            if (console.log("insert list = " + n[s]), e == n[s].tabId) {
                o = !0;
                break
            } var a = !1;
        if (checkUrlIsSildeEdit(t) && (a = !0), console.log("isSlidePage : " + a), !o) {
            var c = {};
            c.tabId = e, c.slidePage = a, c.left = 52, c.top = 0, c.width = 480, c.height = 270, c.active = !1, c.slideInfo = [], c.slideInfo.push({}), n.push(c), chrome.storage.local.set({
                insertWidgetTabs: n
            })
        }
    }))
}

function pageUpdated(e) {
    cancelRecordUIWithTabRemove(e.tab.id), chrome.tabs.query({
        active: !0,
        currentWindow: !0
    }, (function(t) {
        var n = t[0];
        insertWidgetTabs.length;
        console.log("pageUpdated current tab = " + n.url), getRequestTabIndex(e.tab.id).then((t => {
            var r = t.index; - 1 != r ? (console.log("pageUpdated tabIndex = " + r), chrome.tabs.sendMessage(e.tab.id, {
                type: "check-already-insert"
            }, (function(t) {
                if (console.log(t), t) {
                    if (t.isInsert) return void console.log("widget already insert");
                    console.log("widget-not insert =============================="), recoverWidget(e, r, n)
                } else console.log("widget-not insert =============================="), recoverWidget(e, r, n)
            }))) : n.url.includes("google.com/presentation") && n.url.includes("edit") && (console.log("pageUpdated slide page , excute script"), restoreSlideWidgetConfig(n.url, n))
        }))
    }))
}

function recoverWidget(e, t, n) {
    var r = !1,
        i = [];
    readTabListWithWidget().then((n => {
        if (i = n, console.log("refresh : " + i[t].slidePage + ", " + r), i[t].slidePage != r) console.log("change between non-slide edit page and slide page, do not re-insert"), i.splice(t, 1);
        else {
            chrome.storage.local.set({
                isFreeze: !1
            }), chrome.runtime.sendMessage({
                type: "unfreeze-request"
            }), console.log("recoverWidget prepareWidgetInsert()"), prepareWidgetInsert(e.tab.id)
        }
    }))
}

function updateWidgetParam(e, t) {
    getRequestTabIndex(e.tab.id).then((n => {
        var r = n.index;
        if (-1 != r) {
            var i = [];
            readTabListWithWidget().then((n => {
                if (i = n, -1 != t.pageId) {
                    var o = getRequestSlidePageIndex(r, t.pageId);
                    i[r].slideInfo[o].widthRatio = t.param.widthRatio, i[r].slideInfo[o].heightRatio = t.param.heightRatio, i[r].slideInfo[o].topRatio = t.param.topRatio, 0 == t.param.leftRatio ? i[r].slideInfo[o].leftRatio = 1e-4 : i[r].slideInfo[o].leftRatio = t.param.leftRatio;
                    var s = parsePresentationId(e.tab.url),
                        a = gettPresentationIndex(s); - 1 != a && (presentationStore[a].slideInfo = i[r].slideInfo, chrome.storage.local.set({
                        presentationStore: presentationStore
                    }))
                } else i[r].top = t.param.top, i[r].left = t.param.left, i[r].width = t.param.width, i[r].height = t.param.height;
                i[r].active = !0, chrome.storage.local.set({
                    insertWidgetTabs: i
                })
            }))
        }
    }))
}

function removeTabFromList(e) {
    var t = [];
    readTabListWithWidget().then((n => {
        console.log("tabs with widget : " + n), t = n, console.log("before remove : tab counts : " + t.length), getRequestTabIndex(e).then((e => {
            var n = e.index; - 1 != n && (t.splice(n, 1), chrome.storage.local.set({
                insertWidgetTabs: t
            }), console.log("after remove : tab counts : " + t.length))
        }))
    }))
}

function readTabListWithWidget() {
    return new Promise(((e, t) => {
        chrome.storage.local.get(["insertWidgetTabs"], (function(t) {
            e(t.insertWidgetTabs)
        }))
    })).catch((function(e) {
        console.log("catch:", e)
    }))
}

function getRequestTabIndex(e) {
    return new Promise(((t, n) => {
        var r = -1,
            i = [];
        readTabListWithWidget().then((n => {
            for (var o = (i = n).length, s = 0; s < o; s++)
                if (e == i[s].tabId) {
                    r = s;
                    break
                } var a = {};
            a.index = r, a.tabList = i, t(a)
        }))
    })).catch((function(e) {
        console.log("catch:", e)
    }))
}

function checkUrlIsSildeEdit(e) {
    return console.log(e), !1
}

function checkPermissionGrant() {
    var e = !1,
        t = !1;
    return new Promise(((n, r) => {
        navigator.permissions.query({
            name: "camera"
        }).then((t => {
            "granted" == t.state && (e = !0)
        })), navigator.permissions.query({
            name: "microphone"
        }).then((e => {
            "granted" == e.state && (t = !0)
        })).then((r => {
            n(e && t)
        }))
    })).catch((function(e) {
        console.log("catch:", e)
    }))
}

function cancelRecordUIWithTabRemove(e) {
    isRecording && e == startRecordTabId && (isRecording = !1, chrome.storage.local.set({
        isRecording: isRecording
    }), chrome.storage.local.set({
        startRecordTabId: -1
    }), canelRecordWithUpdate = !0)
}

function updateRecordUIWithWidgetRemove() {
    readTabListWithWidget().then((e => {
        var t;
        t = e;
        for (var n = 0; n < t.length; n++) chrome.tabs.sendMessage(t[n].tabId, {
            type: "sync-record-uistate",
            isRecording: !1
        })
    }))
}

function getSlidePageId(e) {
    var t = e.indexOf("id.");
    return e.substring(t + 3)
}

function parsePresentationId(e) {
    var t = "presentation/d/";
    return startIndex = e.indexOf(t), endIndex = e.indexOf("/edit"), e.substring(startIndex + t.length, endIndex)
}
scanAndUpdateWidgetTabList(), chrome.contextMenus.onClicked.addListener(((e, t) => {
    console.log("context menu clicked"), contextMenuInsert && rightClickToInsert()
})), chrome.tabs.onUpdated.addListener((function(e, t, n) {
    t.url && checkPermissionGrant().then((e => {
        e ? createInsertWidgetMenu() : removeInsertWidgetMenu()
    }))
})), chrome.tabs.onActivated.addListener((function(e, t, n) {
    chrome.tabs.get(e.tabId, (function(e) {
        e.url.includes("chrome://") || e.url.includes("chrome.com") || e.url.includes("chrome-extension") ? removeInsertWidgetMenu() : checkPermissionGrant().then((e => {
            e ? createInsertWidgetMenu() : removeInsertWidgetMenu()
        }))
    }))
})), chrome.tabs.onRemoved.addListener((function(e, t) {
    console.log("onRemoved : " + e), removeTabFromList(e), cancelRecordUIWithTabRemove(e), readTabListWithWidget().then((e => {
        if (0 == e.length) {
            console.log("Reset the video state parameter");
            chrome.storage.local.set({
                isRecording: !1
            }), chrome.storage.local.set({
                startRecordTabId: -1
            }), chrome.storage.local.set({
                isFreeze: !1
            }), chrome.storage.local.set({
                isPauseRecord: !1
            })
        }
    })), e && startRecordTabId == e && (console.log("The tab with start record remove"), chrome.storage.local.set({
        isRecording: !1
    }), chrome.storage.local.set({
        startRecordTabId: -1
    }))
})), chrome.storage.onChanged.addListener((function(e, t) {
    for (let [t, {
            oldValue: n,
            newValue: r
        }] of Object.entries(e)) "contextMenuInsert" == t ? contextMenuInsert = r : "isRecording" == t ? (n != r && 0 == r && updateRecordUIWithWidgetRemove(), isRecording = r) : "startRecordTabId" == t && (startRecordTabId = r)
})), chrome.runtime.onMessage.addListener((function(e, t, n) {
    if ("insertWidget" == e.type) return console.log("Receive insert widget message"), prepareWidgetInsert(), !0;
    if ("open-file-explor" == e.type) openFileExplor();
    else {
        if ("sources-loaded" == e.type) return console.log("Refresh page------------ : " + t.tab.id), pageUpdated(t), !0;
        if ("update-widget-parameter" == e.type) return updateWidgetParam(t, e), !0;
        if ("widgetInfo-request" == e.type) return getRequestTabIndex(t.tab.id).then((e => {
            var t = e.index;
            console.log("widgetInfo-request--------------index : " + t), -1 != t && (console.log(e.tabList[t]), n({
                widgetInfo: e.tabList[t]
            }))
        })), !0;
        if ("notify-widget-remove" == e.type) chrome.tabs.sendMessage(t.tab.id, {
            type: "insert-widget-changed",
            isInsert: !1
        }), startRecordTabId == t.tab.id && (chrome.storage.local.set({
            isRecording: !1
        }), chrome.storage.local.set({
            startRecordTabId: -1
        })), removeTabFromList(t.tab.id);
        else if ("get-tabid-request" == e.type) return console.log("get-tabid-request--------------  sender :" + t.tab.id), n({
            tabId: t.tab.id
        }), !0
    }
}));