From 9db64990cdb9f10a11639eee2c9dede84f417e67 Mon Sep 17 00:00:00 2001 From: Anthony Lieuallen Date: Thu, 24 Jan 2013 10:42:36 -0500 Subject: [PATCH] Install .user.js files dropped into the Add-ons Manager. Refs #1663 --- content/addons4-overlay.js | 16 +++++++++++++++ modules/third-party/droppedUrls.js | 33 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 modules/third-party/droppedUrls.js diff --git a/content/addons4-overlay.js b/content/addons4-overlay.js index aa3b8fbed..be5218437 100644 --- a/content/addons4-overlay.js +++ b/content/addons4-overlay.js @@ -4,6 +4,7 @@ (function private_scope() { Components.utils.import("resource://gre/modules/AddonManager.jsm"); Components.utils.import("resource://greasemonkey/addons4.js"); +Components.utils.import('resource://greasemonkey/third-party/droppedUrls.js'); Components.utils.import('resource://greasemonkey/util.js'); var userScriptViewId = 'addons://list/greasemonkey-user-script'; @@ -24,6 +25,21 @@ createItem = function GM_createItem(aObj, aIsInstall, aIsRemote) { return item; }; +// Patch the default onDrop() to make user script installation work. +var _gDragDrop_onDrop_Orig = gDragDrop.onDrop; +gDragDrop.onDrop = function GM_onDrop(aEvent) { + var urls = droppedUrls(aEvent); + + for (var i = urls.length - 1, url = null; url = urls[i]; i--) { + if (url.match(/\.user\.js$/)) { + GM_util.showInstallDialog( + url, GM_util.getBrowserWindow().gBrowser, GM_util.getService()); + } + } + + _gDragDrop_onDrop_Orig(aEvent); +}; + // Set up an "observer" on the config, to keep the displayed items up to date // with their actual state. var observer = { diff --git a/modules/third-party/droppedUrls.js b/modules/third-party/droppedUrls.js new file mode 100644 index 000000000..0402737cc --- /dev/null +++ b/modules/third-party/droppedUrls.js @@ -0,0 +1,33 @@ +/* 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/. */ + +const EXPORTED_SYMBOLS = ['droppedUrls']; + +function droppedUrls(aEvent) { + var dataTransfer = aEvent.dataTransfer; + var urls = []; + + // Convert every dropped item into a url + for (var i = 0; i < dataTransfer.mozItemCount; i++) { + var url = dataTransfer.mozGetDataAt('text/uri-list', i); + if (url) { + urls.push(url); + continue; + } + + url = dataTransfer.mozGetDataAt('text/x-moz-url', i); + if (url) { + urls.push(url.split('\n')[0]); + continue; + } + + var file = dataTransfer.mozGetDataAt('application/x-moz-file', i); + if (file) { + urls.push(Services.io.newFileURI(file).spec); + continue; + } + } + + return urls; +}