Skip to content

Commit

Permalink
version 1.1
Browse files Browse the repository at this point in the history
 * suports ff 5
 * supports public YOURLS instances
 * async ajax
 * go home opens a new tab
  • Loading branch information
binfalse committed Jul 3, 2011
1 parent 824b84c commit 80ed5b4
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 47 deletions.
11 changes: 5 additions & 6 deletions chrome/content/browser.xul
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
<?xml-stylesheet href="chrome://yourls-shortener/skin/skin.css" type="text/css"?>
<!DOCTYPE overlay SYSTEM "chrome://yourls-shortener/locale/translations.dtd">
<overlay id="sample" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script src="yourlsshortener.js" />
<popup id="contentAreaContextMenu">
<menuitem id="yourls-shortener-context" insertafter="context-bookmarkpage" label="&shorten;" tooltiptext="&shortenlong;" oncommand="YOURLSshortener.run(content.location.href);" />
</popup>
<script src="yourlsshortener.js" />
<popup id="contentAreaContextMenu">
<menuitem id="yourls-shortener-context" insertafter="context-bookmarkpage" label="&shorten;" tooltiptext="&shortenlong;" oncommand="YOURLSshortener.run(content.location.href);" />
</popup>
<statusbar id="status-bar">
<statusbarpanel id="yourls-shortener-status-bar-icon" class="statusbarpanel-iconic" src="chrome://yourls-shortener/skin/favicon.gif" tooltiptext="&shortenlong;" onclick="YOURLSshortener.run(content.location.href);" />
</statusbar>
<toolbarpalette id="BrowserToolbarPalette">
<toolbarbutton id="yourls-shortener-toolbar-button" label="&shorten;" tooltiptext="&shortener;"
class="toolbarbutton-1 chromeclass-toolbar-additional" type="menu">
<toolbarbutton id="yourls-shortener-toolbar-button" label="&shorten;" tooltiptext="&shortener;" class="toolbarbutton-1 chromeclass-toolbar-additional" type="menu">
<menupopup>
<menuitem label="&shorten;" tooltiptext="&shortenlong;" onclick="YOURLSshortener.run(content.location.href);" />
<menuitem label="&gohome;" onclick="YOURLSshortener.gohome();" />
Expand Down
12 changes: 5 additions & 7 deletions chrome/content/options.xul
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

<prefwindow title="YOURLS shortener" xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script src="yourlsshortener.js" />
<script src="yourlsshortener.js" />
<prefpane label="YOURLS shortener preferences">
<preferences>
<preference id="yourls-shortener-api" name="extensions.yourls-shortener.api" type="string"/>
<preference id="yourls-shortener-signature" name="extensions.yourls-shortener.signature" type="string"/>
<preference id="yourls-shortener-usesignature" name="extensions.yourls-shortener.usesignature" type="bool"/>
<preference id="yourls-shortener-askforkey" name="extensions.yourls-shortener.askforkey" type="bool"/>
<preference id="yourls-shortener-maxwait" name="extensions.yourls-shortener.maxwait" type="int"/>
</preferences>

<groupbox>
Expand All @@ -24,21 +24,19 @@
<textbox id="api" preference="yourls-shortener-api"/>
</row>
<row>
<label control="signature" tooltiptext="Your signature to authenticate, e.g. 1234567890" value="Signature"/>
<label control="signature" tooltiptext="Your signature to authenticate at access restricted API's" value="Signature"/>
<textbox id="signature" preference="yourls-shortener-signature"/>
</row>
<row>
<label control="askforkey" tooltiptext="Do you want to provide keywords?" value="Ask for a keyword?"/>
<checkbox id="askforkey" preference="yourls-shortener-askforkey"/>
</row>
<row>
<spacer/>
<button id="test" tooltiptext="Test the config by shorten the URL http://binfalse.de" label="test configuration" onclick="YOURLSshortener.run('http://binfalse.de')"/>
<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>
</rows>
</grid>
</groupbox>

</prefpane>

</prefwindow>
80 changes: 49 additions & 31 deletions chrome/content/yourlsshortener.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var YOURLSshortener = function () {
var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService (Components.interfaces.nsIPrefBranch);
return {
gohome : function () {
var api = prefManager.getCharPref("extensions.yourls-shortener.api");
if (api.substr(-1) != '/')
var api = prefManager.getCharPref ("extensions.yourls-shortener.api");
if (api.substr (-1) != '/')
api += '/';
window.open(api + "admin/");
openUILinkIn(api + "admin/", 'tab');
return;
},
run : function (long) {
Expand All @@ -15,62 +15,80 @@ var YOURLSshortener = function () {
return;
}

var error = null;
var api = prefManager.getCharPref("extensions.yourls-shortener.api");
if (api.substr(-1) != '/')
var api = prefManager.getCharPref ("extensions.yourls-shortener.api");
if (api.substr (-1) != '/')
api += '/';
api += "yourls-api.php";

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"))
if (prefManager.getBoolPref ("extensions.yourls-shortener.askforkey"))
{
var sel = "";
try
{
sel = content.getSelection() + "";
sel = content.getSelection () + "";
}
catch (e) {}

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

var request = new XMLHttpRequest();
request.open("POST", api, false);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader("Content-length", params.length);
request.setRequestHeader("Connection", "close");
request.send(params);
if (request.status == 200 || request.status == 201)
{
if (request.responseText)
{
alert ('Your shorten URL:\n' + request.responseText);
var maxwait = 1000 * prefManager.getIntPref ("extensions.yourls-shortener.maxwait");
if (!maxwait || maxwait < 2000)
maxwait = 2000;

var request = new XMLHttpRequest ();
request.open ("POST", api, false);
request.setRequestHeader ("Content-type", "application/x-www-form-urlencoded");
request.setRequestHeader ("Content-length", params.length);
request.setRequestHeader ("Connection", "close");

var requestTimer = setTimeout (function () {
request.abort ();
alert ("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.responseText)
{
alert ('Your shorten URL:\n' + request.responseText);
return;
}
else
{
alert ("Shorten failed.. Maybe invalid key!?")
return;
}
}
else
error = "Shorten failed.. Maybe invalid key!?"
{
alert ("API returned crap!");
return;
}
}
else
error = "API returned crap!";

request.send (params);
}
catch (e)
{
error = "Failed to start XMLHttpRequest:\n" + e.message;
alert ("Failed to start XMLHttpRequest:\n" + e.message);
}

}
else
error = "No API-URL specified... Check your settings!";

if (error)
alert (error);
alert ("No API-URL specified... Check your settings!");
}
};
}();
} ();
3 changes: 2 additions & 1 deletion defaults/preferences/prefs.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pref("extensions.yourls-shortener.api", "http://yoursite/");
pref("extensions.yourls-shortener.signature", "123456");
pref("extensions.yourls-shortener.askforkey", false);
pref("extensions.yourls-shortener.askforkey", false);
pref("extensions.yourls-shortener.maxwait", 4);
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.0</em:version>
<em:version>1.1</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>4.0.*</em:maxVersion>
<em:maxVersion>5.*</em:maxVersion>
</Description>
</em:targetApplication>
</Description>
Expand Down

0 comments on commit 80ed5b4

Please sign in to comment.