Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow user to change filename on install.

  • Loading branch information...
commit 390d1988fdac8cb5d0c4d99e91a40b8c5bff244b 1 parent b69a303
Rafal Chlodnicki authored
Showing with 38 additions and 16 deletions.
  1. +21 −16 js/server_script.js
  2. +10 −0 js/utilities.js
  3. +7 −0 templates/install.html
View
37 js/server_script.js
@@ -46,12 +46,24 @@ function handleRequest(event)
return;
}
+ var install_url = request.bodyItems['install_script'][0];
+ // extract file name from path
+
+ var filename = getFilenameFromPath(install_url);
+ // user name that user defined (or left default)
+ if (request.bodyItems['filename'])
+ filename = request.bodyItems['filename'][0];
+
+ // check if file already exists by trying to read conent (will get useful later)
+ var file_content = readFile(filename)
+
// pre-installation dialog which shows script info and install button
- if (!request.bodyItems['confirm'])
+ // Also show when target filename exists and overwrite param was not send
+ if (!request.bodyItems['confirm'] || (!request.bodyItems['overwrite'] && file_content !== null))
{
var tpldata = {
admin_url : SERVICE_PATH_ADMIN,
- install_url : request.bodyItems['install_script'][0],
+ install_url : install_url,
script_body : document.createTextNode(request.bodyItems['script_body'][0]),
unique_id : request.bodyItems['unique_id'][0],
ask_overwrite : false,
@@ -59,19 +71,16 @@ function handleRequest(event)
new_header : null
};
- // extract file name from path
- var filename = tpldata.install_url.match(/.+\/([^/?]+)/);
if (filename)
{
- filename = filename[1];
+ tpldata.filename = filename;
+
// check if file already exists and ask for overwrite if yes
- var existing_body = readFile(filename);
- if (existing_body !== null)
+ if (file_content !== null)
{
tpldata.ask_overwrite = true;
- tpldata.old_header = Script.parseHeader(existing_body)||{'<missing>':''};
+ tpldata.old_header = Script.parseHeader(file_content)||{'<missing>':''};
tpldata.new_header = Script.parseHeader(unescape(tpldata.script_body.data))||{'<missing>':''};
-
}
}
else
@@ -91,11 +100,7 @@ function handleRequest(event)
var
script_uri = unescape(request.bodyItems['install_script'][0]),
script_body = unescape(request.bodyItems['script_body'][0]),
- overwrite = ( request.bodyItems['overwrite'] ? true : false ),
- filename = script_uri.match(/.+\/([^/?]+)/);
-
- // we are pretty sure that regexp above will match as it did in install dialog already
- filename = filename[1];
+ overwrite = ( request.bodyItems['overwrite'] ? true : false );
// save download url for future use
saveScriptDownloadURL(filename, script_uri);
@@ -139,10 +144,10 @@ function handleRequest(event)
// update global array of shared script
var new_shared = [];
+
for (var i=0; i<tpldata.scripts.length; i++)
- {
new_shared.push(tpldata.scripts[i].printpath);
- }
+
shared_scripts = new_shared;
var template = new Markuper('templates/remote.html', tpldata);
View
10 js/utilities.js
@@ -23,6 +23,16 @@ function getPropOfArrayItem(prop, arr)
return null;
}
+function getFilenameFromPath(filepath)
+{
+ var filename = filepath.match(/.+\/([^/?]+)/);
+
+ if (filename)
+ filename = filename[1];
+
+ return filename;
+}
+
function trim(str)
{
return str.replace(/^[\s\-]+|\s+$/, '');
View
7 templates/install.html
@@ -16,7 +16,14 @@
<input type="hidden" name="confirm" value="1">
<input type="hidden" name="unique_id" value="{{unique_id}}">
<input type="hidden" name="install_script" value="{{install_url}}">
+ <input type="hidden" name="filename" value="{{filename}}" data-keep-if="ask_overwrite">
<textarea name="script_body">{{script_body}}</textarea>
+
+ <p>User script will be saved with this name: <input name="filename" value="{{filename}}"></p>
+
+ <span>You may change name but keep in mind that changing extension from <code>user.js</code>
+ to <code>js</code> or vice-versa might break script.</span>
+
<p class="warning" data-keep-if="ask_overwrite">Script with that name already
exists in your user scripts directory.<br>It will be overwritten if you proceed.
</p>
Please sign in to comment.
Something went wrong with that request. Please try again.