diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /toolkit/mozapps/extensions/content/drag-drop-addon-installer.js | |
parent | Initial commit. (diff) | |
download | thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-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); |