Skip to content

Commit

Permalink
Install .user.js files dropped into the Add-ons Manager.
Browse files Browse the repository at this point in the history
Refs #1663
  • Loading branch information
arantius committed Jan 24, 2013
1 parent 2d092a0 commit 9db6499
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
16 changes: 16 additions & 0 deletions content/addons4-overlay.js
Expand Up @@ -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';
Expand All @@ -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 = {
Expand Down
33 changes: 33 additions & 0 deletions 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;
}

0 comments on commit 9db6499

Please sign in to comment.