diff --git a/background.js b/background.js index 56b0096..a3577b7 100644 --- a/background.js +++ b/background.js @@ -25,17 +25,8 @@ let AiClassifier; try { const store = await browser.storage.local.get(["endpoint", "templateName", "customTemplate", "customSystemPrompt", "aiParams", "debugLogging"]); logger.setDebug(store.debugLogging); - await browser.aiFilter.initConfig(store); + AiClassifier.setConfig(store); logger.aiLog("configuration loaded", {debug: true}, store); - try { - await browser.DomContentScript.registerWindow( - "chrome://messenger/content/FilterEditor.xhtml", - "resource://aifilter/content/filterEditor.js" - ); - logger.aiLog("registered FilterEditor content script", {debug: true}); - } catch (e) { - logger.aiLog("failed to register content script", {level: 'error'}, e); - } } catch (err) { logger.aiLog("failed to load config", {level: 'error'}, err); } @@ -51,8 +42,8 @@ browser.runtime.onMessage.addListener(async (msg) => { logger.aiLog("aiFilter:test – criterion", {debug: true}, criterion); try { - logger.aiLog("Calling browser.aiFilter.classify()", {debug: true}); - const result = await browser.aiFilter.classify(text, criterion); + logger.aiLog("Calling AiClassifier.classifyText()", {debug: true}); + const result = await AiClassifier.classifyText(text, criterion); logger.aiLog("classify() returned", {debug: true}, result); return { match: result }; } diff --git a/content/filterEditor.js b/content/filterEditor.js deleted file mode 100644 index cbd56bd..0000000 --- a/content/filterEditor.js +++ /dev/null @@ -1,50 +0,0 @@ -(function() { - function patch(container) { - if (!container || container.getAttribute("ai-filter-patched") === "true") { - return; - } - while (container.firstChild) { - container.firstChild.remove(); - } - let frag = window.MozXULElement.parseXULToFragment( - ` - ` - ); - container.appendChild(frag); - if (container.hasAttribute("value")) { - container.firstChild.value = container.getAttribute("value"); - } - container.classList.add("flexelementcontainer"); - container.setAttribute("ai-filter-patched", "true"); - } - - function check(node) { - if (!(node instanceof Element)) { - return; - } - if ( - node.classList.contains("search-value-custom") && - node.getAttribute("searchAttribute") === "aifilter#classification" - ) { - patch(node); - } - node - .querySelectorAll('.search-value-custom[searchAttribute="aifilter#classification"]') - .forEach(patch); - } - - const observer = new MutationObserver(mutations => { - for (let mutation of mutations) { - if (mutation.type === "childList") { - mutation.addedNodes.forEach(check); - } else if (mutation.type === "attributes") { - check(mutation.target); - } - } - }); - - const termList = document.getElementById("searchTermList") || document; - observer.observe(termList, { childList: true, attributes: true, subtree: true }); - check(termList); -})(); diff --git a/experiment/DomContentScript/implementation.js b/experiment/DomContentScript/implementation.js deleted file mode 100644 index 541facd..0000000 --- a/experiment/DomContentScript/implementation.js +++ /dev/null @@ -1,80 +0,0 @@ - - -var { AppConstants } = ChromeUtils.importESModule("resource://gre/modules/AppConstants.sys.mjs"); -var DomContent_ESM = parseInt(AppConstants.MOZ_APP_VERSION, 10) >= 128; - -var { ExtensionCommon } = ChromeUtils.importESModule( - "resource://gre/modules/ExtensionCommon.sys.mjs" -); - -var { ExtensionUtils } = DomContent_ESM - ? ChromeUtils.importESModule("resource://gre/modules/ExtensionUtils.sys.mjs") - : ChromeUtils.import("resource://gre/modules/ExtensionUtils.jsm"); - -var { ExtensionError } = ExtensionUtils; - -var registeredWindows = new Map(); - -var DomContentScript = class extends ExtensionCommon.ExtensionAPI { - constructor(extension) { - super(extension); - - this._windowListener = { - // nsIWindowMediatorListener functions - onOpenWindow(appWindow) { - // A new window has opened. - let domWindow = appWindow.docShell.domWindow; - - /** - * Set up listeners to run the callbacks on the given window. - * - * @param aWindow {nsIDOMWindow} The window to set up. - * @param aID {String} Optional. ID of the new caller that has registered right now. - */ - domWindow.addEventListener( - "DOMContentLoaded", - function() { - // do stuff - let windowChromeURL = domWindow.document.location.href; - if (registeredWindows.has(windowChromeURL)) { - let jsPath = registeredWindows.get(windowChromeURL); - Services.scriptloader.loadSubScript(jsPath, domWindow, "UTF-8"); - } - }, - { once: true } - ); - }, - - onCloseWindow(appWindow) { - // One of the windows has closed. - let domWindow = appWindow.docShell.domWindow; // we don't need to do anything (script only loads once) - }, - }; - - Services.wm.addListener(this._windowListener); - - } - - - - - - onShutdown(isAppShutdown) { - if (isAppShutdown) { - return; // the application gets unloaded anyway - } - Services.wm.removeListener(this._windowListener); - } - - getAPI(context) { - /** API IMPLEMENTATION **/ - return { - DomContentScript: { - // only returns something, if a user pref value is set - registerWindow: async function (windowUrl,jsPath) { - registeredWindows.set(windowUrl,jsPath); - } - }, - }; - } -}; diff --git a/experiment/DomContentScript/schema.json b/experiment/DomContentScript/schema.json deleted file mode 100644 index 32d779c..0000000 --- a/experiment/DomContentScript/schema.json +++ /dev/null @@ -1,25 +0,0 @@ -[ - { - "namespace": "DomContentScript", - "functions": [ - { - "name": "registerWindow", - "type": "function", - "async": true, - "description": "Register a script for onDOMContentLoaded", - "parameters": [ - { - "name": "windowUrl", - "type": "string", - "description": "chrome URL of the window " - }, - { - "name": "jsPath", - "type": "string", - "description": "chrome URL of the script" - } - ] - } - ] - } -] diff --git a/experiment/api.js b/experiment/api.js deleted file mode 100644 index 0523193..0000000 --- a/experiment/api.js +++ /dev/null @@ -1,89 +0,0 @@ -var { ExtensionCommon } = ChromeUtils.importESModule("resource://gre/modules/ExtensionCommon.sys.mjs"); -var { Services } = globalThis || ChromeUtils.importESModule("resource://gre/modules/Services.sys.mjs"); -var { MailServices } = ChromeUtils.importESModule("resource:///modules/MailServices.sys.mjs"); -var AiClassifier; - -var aiLog = (...args) => console.log("[ai-filter][api]", ...args); -var setDebug = () => {}; - -console.log("[ai-filter][api] Experiment API module loading"); - -var resProto = Cc["@mozilla.org/network/protocol;1?name=resource"] - .getService(Ci.nsISubstitutingProtocolHandler); - -function registerResourceUrl(extension, namespace) { - aiLog(`[api] registerResourceUrl called for namespace="${namespace}"`, {debug: true}); - if (resProto.hasSubstitution(namespace)) { - aiLog(`[api] namespace="${namespace}" already registered, skipping`, {debug: true}); - return; - } - let uri = Services.io.newURI(".", null, extension.rootURI); - aiLog(`[api] setting substitution for "${namespace}" → ${uri.spec}`, {debug: true}); - resProto.setSubstitutionWithFlags(namespace, uri, resProto.ALLOW_CONTENT_ACCESS); -} - -var AIFilterMod; - -var aiFilter = class extends ExtensionCommon.ExtensionAPI { - async onStartup() { - let { extension } = this; - - registerResourceUrl(extension, "aifilter"); - - // Import logger using the resource URL we just registered - let loggerMod = ChromeUtils.import("resource://aifilter/modules/logger.jsm"); - aiLog = loggerMod.aiLog; - setDebug = loggerMod.setDebug; - - // Now that the resource URL is registered, import the classifier - AiClassifier = ChromeUtils.importESModule("resource://aifilter/modules/AiClassifier.js"); - aiLog("[api] onStartup()", {debug: true}); - - - try { - aiLog("[api] importing ExpressionSearchFilter.jsm", {debug: true}); - AIFilterMod = ChromeUtils.import("resource://aifilter/modules/ExpressionSearchFilter.jsm"); - aiLog("[api] ExpressionSearchFilter.jsm import succeeded", {debug: true}); - } - catch (err) { - aiLog("[api] failed to import ExpressionSearchFilter.jsm", {level: 'error'}, err); - } - } - - onShutdown(isAppShutdown) { - aiLog("[api] onShutdown()", {debug: true}, isAppShutdown); - if (!isAppShutdown && resProto.hasSubstitution("aifilter")) { - aiLog("[api] removing substitution for namespace='aifilter'", {debug: true}); - resProto.setSubstitution("aifilter", null); - } - } - - getAPI(context) { - aiLog("[api] getAPI()", {debug: true}); - return { - aiFilter: { - initConfig: async (config) => { - try { - AiClassifier.setConfig(config); - if (typeof config.debugLogging === "boolean") { - setDebug(config.debugLogging); - } - aiLog("[api] configuration applied", {debug: true}, config); - } catch (err) { - aiLog("[api] failed to apply config", {level: 'error'}, err); - } - }, - classify: async (text, criterion) => { - aiLog("[api] classify() called", {debug: true}, text, criterion); - try { - return await AiClassifier.classifyText(text, criterion); - } - catch (err) { - aiLog("[api] error in classify()", {level: 'error'}, err); - throw err; - } - } - } - }; - } -}; diff --git a/experiment/schema.json b/experiment/schema.json deleted file mode 100644 index 991abc4..0000000 --- a/experiment/schema.json +++ /dev/null @@ -1,25 +0,0 @@ -[ - { - "namespace": "aiFilter", - "functions": [ - { - "name": "initConfig", - "type": "function", - "async": true, - "parameters": [ - { "name": "config", "type": "any" } - ] - }, - { - "name": "classify", - "type": "function", - "parameters": [ - { - "name": "msg", - "type": "any" - } - ] - } - ] - } -] diff --git a/manifest.json b/manifest.json index 0764b49..12430c7 100644 --- a/manifest.json +++ b/manifest.json @@ -19,25 +19,6 @@ "128": "resources/img/logo128.png" }, "background": { "scripts": [ "background.js" ] }, - "experiment_apis": { - "aiFilter": { - "schema": "experiment/schema.json", - "parent": { - "scopes": [ "addon_parent" ], - "paths": [ [ "aiFilter" ] ], - "script": "experiment/api.js", - "events": [ "startup" ] - } - }, - "DomContentScript": { - "schema": "experiment/DomContentScript/schema.json", - "parent": { - "scopes": [ "addon_parent" ], - "paths": [ [ "DomContentScript" ] ], - "script": "experiment/DomContentScript/implementation.js" - } - } - }, "options_ui": { "page": "options/options.html", "open_in_tab": true diff --git a/options/options.js b/options/options.js index a1bf0e5..dfc3bd9 100644 --- a/options/options.js +++ b/options/options.js @@ -88,7 +88,6 @@ document.addEventListener('DOMContentLoaded', async () => { const debugLogging = debugToggle.checked; await browser.storage.local.set({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging }); try { - await browser.aiFilter.initConfig({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging }); AiClassifier.setConfig({ endpoint, templateName, customTemplate: customTemplateText, customSystemPrompt, aiParams: aiParamsSave, debugLogging }); logger.setDebug(debugLogging); } catch (e) {