API loadExtensionScripts (#2233)

* Inject MV3 scripts immediately

* Add api.loadExtensionScripts
This commit is contained in:
toasted-nutbread 2022-09-24 16:41:21 -04:00 committed by GitHub
parent 1e91bf151f
commit 115c3ce3d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View File

@ -130,7 +130,8 @@ class Backend {
['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}], ['testMecab', {async: true, contentScript: true, handler: this._onApiTestMecab.bind(this)}],
['textHasJapaneseCharacters', {async: false, contentScript: true, handler: this._onApiTextHasJapaneseCharacters.bind(this)}], ['textHasJapaneseCharacters', {async: false, contentScript: true, handler: this._onApiTextHasJapaneseCharacters.bind(this)}],
['getTermFrequencies', {async: true, contentScript: true, handler: this._onApiGetTermFrequencies.bind(this)}], ['getTermFrequencies', {async: true, contentScript: true, handler: this._onApiGetTermFrequencies.bind(this)}],
['findAnkiNotes', {async: true, contentScript: true, handler: this._onApiFindAnkiNotes.bind(this)}] ['findAnkiNotes', {async: true, contentScript: true, handler: this._onApiFindAnkiNotes.bind(this)}],
['loadExtensionScripts', {async: true, contentScript: true, handler: this._onApiLoadExtensionScripts.bind(this)}]
]); ]);
this._messageHandlersWithProgress = new Map([ this._messageHandlersWithProgress = new Map([
]); ]);
@ -771,6 +772,16 @@ class Backend {
return await this._anki.findNotes(query); return await this._anki.findNotes(query);
} }
async _onApiLoadExtensionScripts({files}, sender) {
if (!sender || !sender.tab) { throw new Error('Invalid sender'); }
const tabId = sender.tab.id;
if (typeof tabId !== 'number') { throw new Error('Sender has invalid tab ID'); }
const {frameId} = sender;
for (const file of files) {
await this._scriptManager.injectScript(file, tabId, frameId, false, true, 'document_start');
}
}
// Command handlers // Command handlers
async _onCommandOpenSearchPage(params) { async _onCommandOpenSearchPage(params) {

View File

@ -290,6 +290,7 @@ class ScriptManager {
_injectScriptMV3(file, tabId, frameId, allFrames) { _injectScriptMV3(file, tabId, frameId, allFrames) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const details = { const details = {
injectImmediately: true,
files: [file], files: [file],
target: {tabId, allFrames} target: {tabId, allFrames}
}; };

View File

@ -176,6 +176,10 @@ class API {
return this._invoke('findAnkiNotes', {query}); return this._invoke('findAnkiNotes', {query});
} }
loadExtensionScripts(files) {
return this._invoke('loadExtensionScripts', {files});
}
// Utilities // Utilities
_createActionPort(timeout=5000) { _createActionPort(timeout=5000) {