diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /toolkit/mozapps/extensions/content/drag-drop-addon-installer.js | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/mozapps/extensions/content/drag-drop-addon-installer.js')
-rw-r--r-- | toolkit/mozapps/extensions/content/drag-drop-addon-installer.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/toolkit/mozapps/extensions/content/drag-drop-addon-installer.js b/toolkit/mozapps/extensions/content/drag-drop-addon-installer.js new file mode 100644 index 0000000000..0a4f3f749c --- /dev/null +++ b/toolkit/mozapps/extensions/content/drag-drop-addon-installer.js @@ -0,0 +1,81 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +/* import-globals-from aboutaddonsCommon.js */ + +"use strict"; + +class DragDropAddonInstaller extends HTMLElement { + connectedCallback() { + window.addEventListener("drop", this); + } + + disconnectedCallback() { + window.removeEventListener("drop", this); + } + + canInstallFromEvent(e) { + let types = e.dataTransfer.types; + return ( + types.includes("text/uri-list") || + types.includes("text/x-moz-url") || + types.includes("application/x-moz-file") + ); + } + + handleEvent(e) { + if (!XPINSTALL_ENABLED) { + // Nothing to do if we can't install add-ons. + return; + } + + if (e.type == "drop" && this.canInstallFromEvent(e)) { + this.onDrop(e); + } + } + + async onDrop(e) { + e.preventDefault(); + + let dataTransfer = e.dataTransfer; + let browser = getBrowserElement(); + let urls = []; + + // Convert every dropped item into a url, without this should be sync. + for (let i = 0; i < dataTransfer.mozItemCount; i++) { + let url = dataTransfer.mozGetDataAt("text/uri-list", i); + if (!url) { + url = dataTransfer.mozGetDataAt("text/x-moz-url", i); + } + if (url) { + url = url.split("\n")[0]; + } else { + let file = dataTransfer.mozGetDataAt("application/x-moz-file", i); + if (file) { + url = Services.io.newFileURI(file).spec; + } + } + + if (url) { + urls.push(url); + } + } + + // Install the add-ons, the await clears the event data so we do this last. + for (let url of urls) { + let install = await AddonManager.getInstallForURL(url, { + telemetryInfo: { + source: "about:addons", + method: "drag-and-drop", + }, + }); + + AddonManager.installAddonFromAOM( + browser, + document.documentURIObject, + install + ); + } + } +} +customElements.define("drag-drop-addon-installer", DragDropAddonInstaller); |