ChromeBridgeSDK

Chrome Extension RPC bridge SDK (library)

このスクリプトは単体で利用できません。右のようなメタデータを含むスクリプトから、ライブラリとして読み込まれます: // @require https://update.greasyfork.org/scripts/578920/1830750/ChromeBridgeSDK.js

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         ChromeBridgeSDK
// @namespace    chromebridge.sdk
// @version      1.0
// @description  Chrome Extension RPC bridge SDK (library)
// @author       IgnaV
// @grant        none
// ==/UserScript==
(function () {
    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 }),
        };
    }

    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 }),
        };
    }

    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"),
        },
    };
})();