Skip to content

Commit

Permalink
version 1.2
Browse files Browse the repository at this point in the history
* supports Firefox 6
* uses nsIPromptService instead of window.alert
* is able to test the settings
* doesn't dump crap output if API-URL is incorrect
  • Loading branch information
binfalse committed Aug 21, 2011
1 parent 4539cbe commit 3133dfb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
4 changes: 4 additions & 0 deletions chrome/content/options.xul
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<label control="maxwait" tooltiptext="How much time should we spend waiting for the API to respond? Minimum is 2 seconds." value="Max wait time (seconds)"/>
<textbox id="maxwait" preference="yourls-shortener-maxwait"/>
</row>
<row>
<spacer/>
<button id="test" tooltiptext="Test the config by shorten the URL http://binfalse.de" label="test configuration" onclick="YOURLSshortener.test()"/>
</row>
</rows>
</grid>
</groupbox>
Expand Down
59 changes: 47 additions & 12 deletions chrome/content/yourlsshortener.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
var YOURLSshortener = function () {
var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService (Components.interfaces.nsIPrefBranch);
var prompts = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
return {
gohome : function () {
var api = prefManager.getCharPref ("extensions.yourls-shortener.api");
if (api.substr (-1) != '/')
api += '/';
openUILinkIn(api + "admin/", 'tab');
openUILinkIn(api + "admin/", "tab");
return;
},
test : function () {
var api = document.getElementById("api");
var signature = document.getElementById("signature");
var maxwait = document.getElementById("maxwait");
var askforkey = document.getElementById("askforkey");
var fail = "";

if (!api || !signature || !maxwait || !askforkey)
{
prompts.alert(null, "YOURLS shortener: test failed", "A bug occured!\nSry, please contact the developer at http://binfalse.de");
return;
}

if (!api.value)
fail += "Please specify an API-URL!\n";
else if (!api.value.match (/^http\S+$/))
fail += "API-URL has to start with http, white-spaces are not allowed!\n";

if (fail)
{
prompts.alert(null, "YOURLS shortener: test failed", "Test failed:\n" + fail);
return;
}

alert (askforkey.value);
alert (maxwait.value);
prefManager.setCharPref ("extensions.yourls-shortener.api", api.value);
prefManager.setCharPref ("extensions.yourls-shortener.signature", signature.value);
prefManager.setBoolPref ("extensions.yourls-shortener.askforkey", askforkey.checked);
prefManager.setIntPref ("extensions.yourls-shortener.maxwait", maxwait.value);

this.run ("http://binfalse.de/");
return;
},
run : function (long) {
if (!long)
{
alert ("lost my URL!?");
prompts.alert(null, "YOURLS shortener: failed", "no URL specified!?");
return;
}

Expand All @@ -20,11 +55,11 @@ var YOURLSshortener = function () {
api += '/';
api += "yourls-api.php";

if (api && api != 'http://yoursite/')
if (api && api != "http://yoursite/")
{
try
{
var params = 'action=shorturl&format=simple&url=' + long + '&signature=' + prefManager.getCharPref ("extensions.yourls-shortener.signature");
var params = "action=shorturl&format=simple&url=" + long + "&signature=" + prefManager.getCharPref ("extensions.yourls-shortener.signature");

if (prefManager.getBoolPref ("extensions.yourls-shortener.askforkey"))
{
Expand All @@ -37,7 +72,7 @@ var YOURLSshortener = function () {

var key = prompt ("Type your keyword here (leave empty to generate)", sel.toLowerCase ());
if (key)
params += '&keyword=' + key;
params += "&keyword=" + key;
}

var maxwait = 1000 * prefManager.getIntPref ("extensions.yourls-shortener.maxwait");
Expand All @@ -52,29 +87,29 @@ var YOURLSshortener = function () {

var requestTimer = setTimeout (function () {
request.abort ();
alert ("Did not get an answer from server!\nTry again later or increase maximum waiting time.");
prompts.alert(null, "YOURLS shortener: failed", "Did not get an answer from server!\nTry again later or increase maximum waiting time.");
return;
}, maxwait);
request.onreadystatechange = function () {
if (request.readyState != 4)
return;
clearTimeout (requestTimer);
if (request.status == 200 || request.status == 201)
if ((request.status == 200 || request.status == 201) && request.responseText.match(/^\s*\S+\s*$/))
{
if (request.responseText)
{
alert ('Your shorten URL:\n' + request.responseText);
prompts.alert(null, "YOURLS shortener: short URL", long + "\n\nis shortened by:\n\n" + request.responseText);
return;
}
else
{
alert ("Shorten failed.. Maybe invalid key!?")
prompts.alert(null, "YOURLS shortener: failed", "Shortening failed.. Maybe invalid key!?");
return;
}
}
else
{
alert ("API returned crap!");
prompts.alert(null, "YOURLS shortener: failed", "API returned crap! Please check your signature and the API-URL.");
return;
}
}
Expand All @@ -83,12 +118,12 @@ var YOURLSshortener = function () {
}
catch (e)
{
alert ("Failed to start XMLHttpRequest:\n" + e.message);
prompts.alert(null, "YOURLS shortener: failed", "Failed to start XMLHttpRequest:\n" + e.message);
}

}
else
alert ("No API-URL specified... Check your settings!");
prompts.alert(null, "YOURLS shortener: failed", "No API-URL specified... Check your settings!");
}
};
} ();
4 changes: 2 additions & 2 deletions install.rdf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<Description about="urn:mozilla:install-manifest">
<em:id>[email protected]</em:id>
<em:name>YOURLS shortener</em:name>
<em:version>1.1</em:version>
<em:version>1.2</em:version>
<em:type>2</em:type>
<em:creator>Martin Scharm</em:creator>
<em:description>Shorten URLs with your own YOURLS instance</em:description>
Expand All @@ -14,7 +14,7 @@
<Description>
<em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
<em:minVersion>3.0</em:minVersion>
<em:maxVersion>5.*</em:maxVersion>
<em:maxVersion>6.*</em:maxVersion>
</Description>
</em:targetApplication>
</Description>
Expand Down

0 comments on commit 3133dfb

Please sign in to comment.