Skip to content

Commit

Permalink
On "new script", with valid script on clipbard, just install it.
Browse files Browse the repository at this point in the history
  • Loading branch information
arantius committed Sep 27, 2012
1 parent 26ba574 commit 43638a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
15 changes: 2 additions & 13 deletions content/newscript.js
Expand Up @@ -39,19 +39,8 @@ function doInstall() {

// finish making the script object ready to install
// (put this created script into a file -- only way to install it)
var scope = {};
Components.utils.import('resource://greasemonkey/remoteScript.js', scope);
var remoteScript = new scope.RemoteScript();
var tempFileName = scope.cleanFilename(script.name, 'gm_script') + '.user.js';
var tempFile = GM_util.getTempFile(remoteScript._tempDir, tempFileName);
GM_util.writeToFile(scriptSrc, tempFile, function() {
// install this script
remoteScript.setScript(script, tempFile);
remoteScript.install();
// and fire up the editor!
GM_util.openInEditor(script);

// persist namespace value
GM_util.installScriptFromSource(scriptSrc, function() {
// Persist namespace value.
GM_prefRoot.setValue("newscript_namespace", script.namespace);
// Now that async write is complete, close the window.
close();
Expand Down
20 changes: 20 additions & 0 deletions modules/util/installScriptFromSource.js
@@ -0,0 +1,20 @@
Components.utils.import('resource://greasemonkey/parseScript.js');
Components.utils.import('resource://greasemonkey/remoteScript.js');
Components.utils.import('resource://greasemonkey/util.js');

const EXPORTED_SYMBOLS = ['installScriptFromSource'];

function installScriptFromSource(aSource, aCallback) {
var remoteScript = new RemoteScript();
var script = parse(aSource);
var tempFileName = cleanFilename(script.name, 'gm_script') + '.user.js';
var tempFile = GM_util.getTempFile(remoteScript._tempDir, tempFileName);
GM_util.writeToFile(aSource, tempFile, function() {
// install this script
remoteScript.setScript(script, tempFile);
remoteScript.install();
// and fire up the editor!
GM_util.openInEditor(script);
});
if (aCallback) aCallback();
}
37 changes: 33 additions & 4 deletions modules/util/newUserScript.js
@@ -1,10 +1,39 @@
Components.utils.import('resource://greasemonkey/parseScript.js');
Components.utils.import('resource://greasemonkey/util.js');

const EXPORTED_SYMBOLS = ['newUserScript'];

const ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
const gClipboard = Components.classes["@mozilla.org/widget/clipboard;1"]
.getService(Components.interfaces.nsIClipboard);
const gWindowWatcher = Components
.classes["@mozilla.org/embedcomp/window-watcher;1"]
.getService(Components.interfaces.nsIWindowWatcher);

function newUserScript(aWin) {
ww.openWindow(aWin,
"chrome://greasemonkey/content/newscript.xul", null,
"chrome,dependent,centerscreen,resizable,dialog", null);
var clipText = '';
try {
var transferable = Components.classes["@mozilla.org/widget/transferable;1"]
.createInstance(Components.interfaces.nsITransferable);
transferable.addDataFlavor('text/unicode');
gClipboard.getData(transferable, gClipboard.kGlobalClipboard);
var str = new Object(), strLen = new Object();
transferable.getTransferData('text/unicode', str, strLen);
if (str) {
str = str.value.QueryInterface(Components.interfaces.nsISupportsString);
clipText = str.data.substring(0, strLen.value / 2);
}
} catch (e) {
dump('Error reading clipboard:\n' + e + '\n');
}

// If there is a valid script with metadata on the clipboard ...
if (clipText && extractMeta(clipText)) {
// ... just install it!
GM_util.installScriptFromSource(clipText);
} else {
// Otherwise do GUIfied script creation.
gWindowWatcher.openWindow(aWin,
"chrome://greasemonkey/content/newscript.xul", null,
"chrome,dependent,centerscreen,resizable,dialog", null);
}
}

0 comments on commit 43638a2

Please sign in to comment.