ChromeBridgeSDK

Chrome Extension RPC bridge SDK (library)

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greatest.deepsurf.us/scripts/578920/1832038/ChromeBridgeSDK.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         ChromeBridgeSDK
// @namespace    chromebridge.sdk
// @version      1.0
// @description  Chrome Extension RPC bridge SDK (library)
// @author       IgnaV
// @grant        none
// ==/UserScript==
(function () {
    const SDK_VERSION = "1.0";
    const SECRET_KEY = "a3f8c2e1d94b76f05e2a1c8b3d7f4e90";

    function rpc(method, params = {}) {
        const id = Math.random().toString(36).slice(2);

        return new Promise((resolve, reject) => {
            const timeout = setTimeout(() => {
                window.removeEventListener("message", handler);
                reject(new Error("EXT timeout"));
            }, 10000);

            function handler(event) {
                if (event.source !== window) return;
                const msg = event.data;
                if (msg?.type !== "EXT_RESPONSE" || msg.id !== id) return;

                clearTimeout(timeout);
                window.removeEventListener("message", handler);

                if (msg.error) reject(new Error(msg.error));
                else resolve(msg.result);
            }

            window.addEventListener("message", handler);

            window.postMessage({ type: "EXT_CALL", id, key: SECRET_KEY, method, params }, "*");
        });
    }

    function makeTabPromise(promise) {
        return new Proxy(promise, {
            get(target, prop) {
                if (prop === 'then' || prop === 'catch' || prop === 'finally' || typeof prop === 'symbol') {
                    const v = target[prop];
                    return typeof v === 'function' ? v.bind(target) : v;
                }
                return (...args) => makeTabPromise(promise.then(tab => tab[prop](...args)));
            }
        });
    }

    function makeWindowPromise(promise) {
        return new Proxy(promise, {
            get(target, prop) {
                if (prop === 'then' || prop === 'catch' || prop === 'finally' || typeof prop === 'symbol') {
                    const v = target[prop];
                    return typeof v === 'function' ? v.bind(target) : v;
                }
                return (...args) => makeWindowPromise(promise.then(win => win[prop](...args)));
            }
        });
    }

    function wrapTab(tab) {
        if (!tab) return tab;
        return {
            ...tab,
            update: (p) => rpc("tabs.update", { tabId: tab.id, props: p }).then(wrapTab),
            move: (p) => rpc("tabs.move", { tabId: tab.id, move: p }).then(wrapTab),
            remove: () => rpc("tabs.remove", { tabId: tab.id }),
            duplicate: () => rpc("tabs.duplicate", { tabId: tab.id }).then(wrapTab),
            reload: (props) => rpc("tabs.reload", { tabId: tab.id, reloadProperties: props }),
            discard: () => rpc("tabs.discard", { tabId: tab.id }).then(wrapTab),
            goBack: () => rpc("tabs.goBack", { tabId: tab.id }),
            goForward: () => rpc("tabs.goForward", { tabId: tab.id }),
            detectLanguage: () => rpc("tabs.detectLanguage", { tabId: tab.id }),
            captureVisible: (options) => rpc("tabs.captureVisibleTab", { windowId: tab.windowId, options }),
            getZoom: () => rpc("tabs.getZoom", { tabId: tab.id }),
            setZoom: (zoomFactor) => rpc("tabs.setZoom", { tabId: tab.id, zoomFactor }),
            getZoomSettings: () => rpc("tabs.getZoomSettings", { tabId: tab.id }),
            setZoomSettings: (zoomSettings) => rpc("tabs.setZoomSettings", { tabId: tab.id, zoomSettings }),
            toggleReaderMode: () => rpc("tabs.toggleReaderMode", { tabId: tab.id }),
            sendMessage: (message, options) => rpc("tabs.sendMessage", { tabId: tab.id, message, options }),
            window: (getInfo) => rpc("windows.get", { windowId: tab.windowId, getInfo }).then(wrapWindow),
        };
    }

    function wrapWindow(win) {
        if (!win) return win;
        return {
            ...win,
            remove: () => rpc("windows.remove", { windowId: win.id }),
            update: (updateInfo) => rpc("windows.update", { windowId: win.id, updateInfo }).then(wrapWindow),
            captureVisibleTab: (options) => rpc("tabs.captureVisibleTab", { windowId: win.id, options }),
            tabs: {
                create: (p) => rpc("tabs.create", { ...p, windowId: win.id }).then(wrapTab),
                query: (p) => rpc("tabs.query", { ...p, windowId: win.id }).then((tabs) => tabs.map(wrapTab)),
            },
        };
    }

    window.ChromeBridgeSDK = {
        tabs: {
            create: (p) => makeTabPromise(rpc("tabs.create", p).then(wrapTab)),
            query: (p) => rpc("tabs.query", p).then((tabs) => tabs.map(wrapTab)),
            update: (id, p) => makeTabPromise(rpc("tabs.update", { tabId: id, props: p }).then(wrapTab)),
            move: (id, p) => makeTabPromise(rpc("tabs.move", { tabId: id, move: p }).then(wrapTab)),
            remove: (id) => rpc("tabs.remove", { tabId: id }),
            get: (id) => makeTabPromise(rpc("tabs.get", { tabId: id }).then(wrapTab)),
            getCurrent: () => makeTabPromise(rpc("tabs.getCurrent").then(wrapTab)),
            duplicate: (id) => makeTabPromise(rpc("tabs.duplicate", { tabId: id }).then(wrapTab)),
            reload: (id, props) => rpc("tabs.reload", { tabId: id, reloadProperties: props }),
            highlight: (info) => rpc("tabs.highlight", info),
            discard: (id) => makeTabPromise(rpc("tabs.discard", { tabId: id }).then(wrapTab)),
            goBack: (id) => rpc("tabs.goBack", { tabId: id }),
            goForward: (id) => rpc("tabs.goForward", { tabId: id }),
            detectLanguage: (id) => rpc("tabs.detectLanguage", { tabId: id }),
            captureVisibleTab: (windowId, options) => rpc("tabs.captureVisibleTab", { windowId, options }),
            getZoom: (id) => rpc("tabs.getZoom", { tabId: id }),
            setZoom: (id, zoomFactor) => rpc("tabs.setZoom", { tabId: id, zoomFactor }),
            getZoomSettings: (id) => rpc("tabs.getZoomSettings", { tabId: id }),
            setZoomSettings: (id, zoomSettings) => rpc("tabs.setZoomSettings", { tabId: id, zoomSettings }),
            toggleReaderMode: (id) => rpc("tabs.toggleReaderMode", { tabId: id }),
            sendMessage: (id, message, options) => rpc("tabs.sendMessage", { tabId: id, message, options }),
            group: (opts) => rpc("tabs.group", opts),
            ungroup: (tabIds) => rpc("tabs.ungroup", { tabIds }),
            wrap: wrapTab,
        },
        windows: {
            create: (p) => makeWindowPromise(rpc("windows.create", p).then(wrapWindow)),
            get: (id, getInfo) => makeWindowPromise(rpc("windows.get", { windowId: id, getInfo }).then(wrapWindow)),
            getAll: (getInfo) => rpc("windows.getAll", getInfo).then((wins) => wins.map(wrapWindow)),
            getCurrent: (getInfo) => makeWindowPromise(rpc("windows.getCurrent", getInfo).then(wrapWindow)),
            getLastFocused: (getInfo) => makeWindowPromise(rpc("windows.getLastFocused", getInfo).then(wrapWindow)),
            remove: (id) => rpc("windows.remove", { windowId: id }),
            update: (id, updateInfo) => makeWindowPromise(rpc("windows.update", { windowId: id, updateInfo }).then(wrapWindow)),
            wrap: wrapWindow,
        },
        storage: {
            get: (keys) => rpc("storage.get", { keys }),
            set: (items) => rpc("storage.set", { items }),
            remove: (keys) => rpc("storage.remove", { keys }),
            clear: () => rpc("storage.clear"),
        },
        bookmarks: {
            create: (p) => rpc("bookmarks.create", p),
            get: (id) => rpc("bookmarks.get", { id }),
            getTree: () => rpc("bookmarks.getTree"),
            search: (query) => rpc("bookmarks.search", { query }),
            update: (id, changes) => rpc("bookmarks.update", { id, changes }),
            remove: (id) => rpc("bookmarks.remove", { id }),
            removeTree: (id) => rpc("bookmarks.removeTree", { id }),
        },
        downloads: {
            download: (p) => rpc("downloads.download", p),
            search: (p) => rpc("downloads.search", p),
            cancel: (id) => rpc("downloads.cancel", { id }),
            erase: (p) => rpc("downloads.erase", p),
            pause: (id) => rpc("downloads.pause", { id }),
            resume: (id) => rpc("downloads.resume", { id }),
            open: (id) => rpc("downloads.open", { id }),
            show: (id) => rpc("downloads.show", { id }),
        },
        notifications: {
            create: (id, options) => rpc("notifications.create", { id, options }),
            update: (id, options) => rpc("notifications.update", { id, options }),
            clear: (id) => rpc("notifications.clear", { id }),
            getAll: () => rpc("notifications.getAll"),
        },
        cookies: {
            get: (details) => rpc("cookies.get", details),
            getAll: (details) => rpc("cookies.getAll", details),
            set: (details) => rpc("cookies.set", details),
            remove: (details) => rpc("cookies.remove", details),
        },
        clipboard: {
            write: (text) => rpc("clipboard.write", { text }),
            read: () => rpc("clipboard.read"),
        },
    };

    rpc("extension.getVersion").then(extVersion => {
        if (extVersion === SDK_VERSION) {
            console.log(`[ChromeBridge] versión OK: SDK=${SDK_VERSION}, extensión=${extVersion}`);
        } else {
            console.warn(`[ChromeBridge] DISCREPANCIA de versión — SDK=${SDK_VERSION}, extensión=${extVersion}`);
        }
    }).catch(() => {
        console.warn("[ChromeBridge] No se pudo obtener la versión de la extensión");
    });
})();