Permalink
Browse files

Added a back/forward function for requests (#63)

  • Loading branch information...
1 parent b2ef555 commit c1c3a9a707cd6b5871abb35b8008f6b622f4fd23 @chao committed May 28, 2012
View
@@ -40,6 +40,7 @@ if (typeof (restclient) === "undefined") {
Components.utils.import("resource://gre/modules/NetUtil.jsm", restclient);
Components.utils.import("resource://gre/modules/FileUtils.jsm", restclient);
+ Components.utils.import("resource://gre/modules/Services.jsm", restclient);
},
setPref : function (name, value) {
return restclient.Preferences.set("extension.restclient." + name, value);
@@ -57,19 +58,6 @@ if (typeof (restclient) === "undefined") {
console.error(e.message);
}
},
- convertStringToUTF8: function(aStr)
- {
- var utf8Converter = Components.classes["@mozilla.org/intl/utf8converterservice;1"].
- getService(Components.interfaces.nsIUTF8ConverterService);
-
- try {
- return utf8Converter.convertStringToUTF8 (aStr, "UTF-8"); //utf8Converter.convertStringToUTF8(aStr, "utf-8", false);
- }
- catch(e) {
- console.error(e);
- return aStr;
- }
- },
log: function(str) {
try{
console.log(str);
@@ -43,10 +43,15 @@ restclient.main = {
rep3: '3',
rep4: '4',
toggleRequest: 'alt+q',
- toggleResponse: 'alt+s'
+ toggleResponse: 'alt+s',
+ back: 'left',
+ forward: 'right'
},
init: function () {
restclient.init();
+ restclient.sqlite.open();
+ restclient.sqlite.initTables();
+
this.initSkin();
restclient.main.navTop = $('.subnav').length && $('.subnav').offset().top - $('.navbar').first().height();
@@ -97,6 +102,7 @@ restclient.main = {
$('.toggle-page-layout').click(restclient.main.toggleLayout);
$('.toggle-header-layout').click(restclient.main.toggleRequestHeaderLayout);
$('.toggle-request-timer').click(restclient.main.toggleRequestTimer);
+ $('.clear-cached-requests').click(restclient.main.clearCachedRequests);
$('#request-body').focus(function () {
if ($(this).innerHeight() < 200 )
$(this).css('height', '200px');
@@ -119,6 +125,62 @@ restclient.main = {
$('#modal-oauth-view').data('source-header-id', headerId);
$('#modal-oauth-view textarea').val($('span[data-header-id="' + headerId + '"]').attr('header-value'));
});
+
+ window.onhashchange = restclient.main.hashChange;
+ restclient.main.hashChange();
+ },
+ unload: function() {
+ restclient.sqlite.close();
+ },
+ hashChange: function() {
+ if (restclient.main.ignoreHashChange === true)
+ {
+ restclient.main.ignoreHashChange = false;
+ return false;
+ }
+ if (location.hash.indexOf('#request-') === 0)
+ {
+ var request = restclient.sqlite.getRequest(location.hash.substr(1));
+ if(typeof request === 'string')
+ try{
+ console.log(request);
+ request = JSON.parse(request);
+ restclient.main.applyRequest(request);
+ }
+ catch(e)
+ {
+ console.warn(e);
+ }
+ else
+ {
+ request = {
+ url: '',
+ method: 'GET',
+ body: '',
+ header: []
+ }
+ restclient.main.applyRequest(request);
+ }
+ restclient.main.clearResult();
+ window.scrollTo(0,0);
+ }
+ },
+ clearCachedRequests: function() {
+ var num = restclient.sqlite.getRequestCount();
+
+ if(num > 0)
+ restclient.sqlite.initTables(true);
+ restclient.message.show({
+ id: 'alertCachedRequestCleared',
+ type: 'message',
+ title: 'Cached requests are cleared!',
+ message: num + ' records have been removed.',
+ buttons: [
+ {title: 'Okay', class: 'btn-danger', callback: function () { $('#alertCachedRequestCleared').alert('close'); }}
+ ],
+ parent: $('#request-error'),
+ exclude: true
+ });
},
changeSkin: function (cssFileName) {
$("link").remove();
@@ -241,6 +303,15 @@ restclient.main = {
restclient.main.toggleResponse();
return false;
});
+
+ $(document).bind('keydown', restclient.main.hotkey.back, function () {
+ window.history.back();
+ return false;
+ });
+ $(document).bind('keydown', restclient.main.hotkey.forward, function () {
+ window.history.forward();
+ return false;
+ });
},
toggleRequest: function (e) {
var toggle = $('.toggle-request');
@@ -1809,6 +1880,15 @@ restclient.main = {
});
return false;
}
+ restclient.sqlite.saveRequest(request, function() {
+ if(arguments.length > 0)
+ {
+ if(location.hash.substr(1) === arguments[0])
+ return false;
+ restclient.main.ignoreHashChange = true;
+ location.hash = arguments[0];
+ }
+ });
restclient.http.sendRequest(request.method, request.url, request.headers, request.overrideMimeType, request.body);
},
donate: function () {
@@ -1838,4 +1918,4 @@ restclient.main = {
};
window.addEventListener("load", function () { restclient.main.init(); }, false);
-window.addEventListener("unload", function () { }, false);
+window.addEventListener("unload", function () { restclient.main.unload(); }, false);
@@ -43,12 +43,27 @@ restclient.overlay = {
.getInterface(Components.interfaces.nsIDOMWindow);
return mainWindow.gBrowser;
},
+ /*transferToLocalStorage: function() {
+ var names = [ 'OAuth.setting', 'OAuth.sign', 'OAuth2.authorize.ignoreWarning', 'basicAuth', 'cachedUrls', 'defaultSkin',
+ 'favoriteHeaders', 'firstRunDone', 'imageWarning', 'oauth2.templates', 'oauth2.tokens', 'pageLayout', 'requestHeaderLayout',
+ 'requestTimer', 'savedRequest', 'sign-warning', 'version'];
+ for(var i = 0, name; name = names[i]; i++) {
+ var value = restclient.Preferences.get("extension.restclient." + name, '');
+ localStorage.setItem(name, value);
+ }
+ },*/
firstRun : function() {
var firstRunPref = "firstRunDone",
versionPref = "version",
- versionNumber = "2.0.1",
+ versionNumber = "2.0.4",
browser = restclient.overlay.getBrowser();
+ /*try{
+ localStorage.getItem('version');
+ }catch(e) {
+ restclient.transferToLocalStorage();
+ }*/
+
if(!restclient.getPref(firstRunPref, false))
{
var navbar = document.getElementById("nav-bar");
@@ -0,0 +1,144 @@
+/* ***** BEGIN LICENSE BLOCK *****
+Copyright (c) 2007-2012, Chao ZHOU (chao@zhou.fr). All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+ * Neither the name of the author nor the names of its contributors may
+ be used to endorse or promote products derived from this software
+ without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ * ***** END LICENSE BLOCK ***** */
+
+"use strict";
+
+restclient.sqlite = {
+ db: null,
+ tables: {
+ requests: "id TEXT PRIMARY KEY, request_method TEXT, request_url TEXT, request_body TEXT, request TEXT,created_datetime TEXT,last_executed TEXT"
+ },
+ open: function() {
+ try{
+ var file = restclient.FileUtils.getFile("ProfD", ["restclient.sqlite"]);
+ //console.log(file.path);
+ restclient.sqlite.db = restclient.Services.storage.openDatabase(file);
+ return true;
+ }
+ catch(e) {
+ console.error(e);
+ }
+ return false;
+ },
+ close: function() {
+ try{
+ restclient.db.asyncClose();
+ }
+ catch(e) {
+ console.error(e);
+ }
+ },
+ initTables: function(force) {
+ if(restclient.sqlite.db.tableExists('requests'))
+ {
+ if (force)
+ {
+ restclient.sqlite.db.executeSimpleSQL('DELETE FROM requests');
+ }
+ }
+ else
+ restclient.sqlite.db.createTable('requests', restclient.sqlite.tables['requests']);
+ },
+ saveRequest: function(request, callback) {
+ var id = "request-" + restclient.helper.sha1(JSON.stringify(request));
+ var savedRequest = restclient.sqlite.getRequest(id);
+ var last_executed = new Date().valueOf();
+ if(savedRequest === false) {
+ var created_datetime = new Date().valueOf();
+ var stmt = restclient.sqlite.db.createStatement("INSERT INTO requests (id,request_method, request_url, request_body, request, created_datetime, last_executed) VALUES "
+ + "(:id, :request_method, :request_url, :request_body, :request, :created_datetime, :last_executed)");
+ var params = stmt.newBindingParamsArray(),
+ binding = params.newBindingParams();
+
+ binding.bindByName("id", id);
+ binding.bindByName("request_method", request.method);
+ binding.bindByName("request_url", request.url);
+ binding.bindByName("request_body", request.body);
+ binding.bindByName("request", JSON.stringify(request));
+ binding.bindByName("created_datetime", created_datetime);
+ binding.bindByName("last_executed", last_executed);
+
+ params.addParams(binding);
+ stmt.bindParameters(params);
+ stmt.executeAsync({
+ handleError: function(aError) {
+ console.error("Error: " + aError.message);
+ },
+ handleCompletion: function(aReason) {
+ if (aReason == Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED
+ && typeof callback === 'function')
+ callback.apply(restclient.main, [id]);
+ }
+ });
+ }
+ else
+ {
+ var stmt = restclient.sqlite.db.createStatement("UPDATE requests SET last_executed=:last_executed WHERE id=:id");
+ var params = stmt.newBindingParamsArray(),
+ binding = params.newBindingParams();
+
+ binding.bindByName("id", id);
+ binding.bindByName("last_executed", last_executed);
+
+ params.addParams(binding);
+ stmt.bindParameters(params);
+ stmt.executeAsync({
+ handleError: function(aError) {
+ console.error("Error: " + aError.message);
+ },
+ handleCompletion: function(aReason) {
+ if (aReason == Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED
+ && typeof callback === 'function')
+ callback.apply(restclient.main, [id]);
+ }
+ });
+ }
+ },
+ getRequest: function(id){
+ if(typeof id !== 'string' || id === '')
+ return false;
+ var stmt = restclient.sqlite.db.createStatement("SELECT request FROM requests WHERE id=:id");
+ var params = stmt.newBindingParamsArray(),
+ binding = params.newBindingParams();
+
+ binding.bindByName("id", id);
+ params.addParams(binding);
+ stmt.bindParameters(params);
+ while (stmt.executeStep()) {
+ return stmt.row.request;
+ }
+ return false;
+ },
+ getRequestCount: function(){
+ var stmt = restclient.sqlite.db.createStatement("SELECT count(id) as num FROM requests");
+ while (stmt.executeStep()) {
+ return stmt.row.num;
+ }
+ return false;
+ }
+}
View
@@ -77,6 +77,7 @@
<li class="divider"></li>
<li class="nav-header">Develop</li>
<li><a style="cursor:pointer" class="toggle-request-timer" data-timer="disabled">Enable request execution timer</a></li>
+ <li><a style="cursor:pointer" class="clear-cached-requests">Clear cached requests</a></li>
<li class="divider"></li>
<li class="nav-header">Skins (bootswatch.com)</li>
<li><a css="bootstrap.cerulean.css">Cerulean</a></li>
@@ -604,5 +605,7 @@ <h3>OAuth2</h3>
<script type="text/javascript" src="js/restclient.oauth2.js"></script>
<script type="text/javascript" src="js/restclient.oauth2.template.js"></script>
+
+<script type="text/javascript" src="js/restclient.sqlite.js"></script>
</body>
</html>
View
@@ -10,10 +10,12 @@
<script src="../js/restclient.js"></script>
<script src="../js/restclient.helper.js"></script>
<script src="../js/restclient.oauth.js"></script>
+ <script src="../js/restclient.sqlite.js"></script>
<!-- unit tests -->
<script src="unit/restclient.helper.js"></script>
<script src="unit/restclient.oauth.js"></script>
+ <script src="unit/restclient.sqlite.js"></script>
<body>
<div>
<h1 id="qunit-header">RESTClient Test Suite</h1>
@@ -0,0 +1,29 @@
+$(function () {
+ module("restclient.sqlite.js");
+ restclient.init();
+ test("Test init function", function () {
+ restclient.sqlite.open();
+ ok(typeof restclient.sqlite.db === 'object', 'inited');
+ });
+ test("Test create table function", function () {
+ restclient.sqlite.initTables();
+ ok(typeof restclient.sqlite.db === 'object', 'inited');
+ });
+
+ test("Test save request function", function () {
+ var request = {
+ request_method : 'GET',
+ request_url : 'https://developer.mozilla.org/en/Storage',
+ request_body : 'a=b&c=d'
+ }
+ restclient.sqlite.saveRequest(request);
+ ok(typeof restclient.sqlite.db === 'object', 'inited');
+ });
+
+ test("Test save request function", function () {
+
+ var request = restclient.sqlite.getRequest('request-db54a85dd2f2119102e22e8fe061435a0d0b0e77');
+ ok(typeof request === 'string', JSON.stringify(request) );
+ });
+
+});

0 comments on commit c1c3a9a

Please sign in to comment.