aboutsummaryrefslogtreecommitdiff
path: root/ext/js/background/script-manager.js
blob: ea1702e955666a0897218ab53257f779076d7629 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2023-2024  Yomitan Authors
 * Copyright (C) 2021-2022  Yomichan Authors
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

/**
 * Injects a stylesheet into a tab.
 * @param {'file'|'code'} type The type of content to inject; either 'file' or 'code'.
 * @param {string} content The content to inject.
 *   - If type is `'file'`, this argument should be a path to a file.
 *   - If type is `'code'`, this argument should be the CSS content.
 * @param {number} tabId The id of the tab to inject into.
 * @param {number|undefined} frameId The id of the frame to inject into.
 * @param {boolean} allFrames Whether or not the stylesheet should be injected into all frames.
 * @returns {Promise<void>}
 */
export function injectStylesheet(type, content, tabId, frameId, allFrames) {
    return new Promise((resolve, reject) => {
        /** @type {chrome.scripting.InjectionTarget} */
        const target = {
            tabId,
            allFrames
        };
        /** @type {chrome.scripting.CSSInjection} */
        const details = (
            type === 'file' ?
            {origin: 'AUTHOR', files: [content], target} :
            {origin: 'USER', css: content, target}
        );
        if (!allFrames && typeof frameId === 'number') {
            details.target.frameIds = [frameId];
        }
        chrome.scripting.insertCSS(details, () => {
            const e = chrome.runtime.lastError;
            if (e) {
                reject(new Error(e.message));
            } else {
                resolve();
            }
        });
    });
}

/**
 * Checks whether or not a content script is registered.
 * @param {string} id The identifier used with a call to `registerContentScript`.
 * @returns {Promise<boolean>} `true` if a script is registered, `false` otherwise.
 */
export async function isContentScriptRegistered(id) {
    const scripts = await getRegisteredContentScripts([id]);
    for (const script of scripts) {
        if (script.id === id) {
            return true;
        }
    }
    return false;
}

/**
 * Registers a dynamic content script.
 * Note: if the fallback handler is used and the 'webNavigation' permission isn't granted,
 * there is a possibility that the script can be injected more than once due to the events used.
 * Therefore, a reentrant check may need to be performed by the content script.
 * @param {string} id A unique identifier for the registration.
 * @param {import('script-manager').RegistrationDetails} details The script registration details.
 * @throws An error is thrown if the id is already in use.
 */
export async function registerContentScript(id, details) {
    if (await isContentScriptRegistered(id)) {
        throw new Error('Registration already exists');
    }

    const details2 = createContentScriptRegistrationOptions(details, id);
    await /** @type {Promise<void>} */ (new Promise((resolve, reject) => {
        chrome.scripting.registerContentScripts([details2], () => {
            const e = chrome.runtime.lastError;
            if (e) {
                reject(new Error(e.message));
            } else {
                resolve();
            }
        });
    }));
}

/**
 * Unregisters a previously registered content script.
 * @param {string} id The identifier passed to a previous call to `registerContentScript`.
 * @returns {Promise<void>}
 */
export async function unregisterContentScript(id) {
    return new Promise((resolve, reject) => {
        chrome.scripting.unregisterContentScripts({ids: [id]}, () => {
            const e = chrome.runtime.lastError;
            if (e) {
                reject(new Error(e.message));
            } else {
                resolve();
            }
        });
    });
}

/**
 * @param {import('script-manager').RegistrationDetails} details
 * @param {string} id
 * @returns {chrome.scripting.RegisteredContentScript}
 */
function createContentScriptRegistrationOptions(details, id) {
    const {css, js, allFrames, matches, runAt, world} = details;
    /** @type {chrome.scripting.RegisteredContentScript} */
    const options = {
        id: id,
        persistAcrossSessions: true
    };
    if (Array.isArray(css)) {
        options.css = [...css];
    }
    if (Array.isArray(js)) {
        options.js = [...js];
    }
    if (typeof allFrames !== 'undefined') {
        options.allFrames = allFrames;
    }
    if (Array.isArray(matches)) {
        options.matches = [...matches];
    }
    if (typeof runAt !== 'undefined') {
        options.runAt = runAt;
    }
    if (typeof world !== 'undefined') {
        options.world = world;
    }
    return options;
}

/**
 * @param {string[]} ids
 * @returns {Promise<chrome.scripting.RegisteredContentScript[]>}
 */
function getRegisteredContentScripts(ids) {
    return new Promise((resolve, reject) => {
        chrome.scripting.getRegisteredContentScripts({ids}, (result) => {
            const e = chrome.runtime.lastError;
            if (e) {
                reject(new Error(e.message));
            } else {
                resolve(result);
            }
        });
    });
}