Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Elijah Grey committed Jul 21, 2009
1 parent e2d2d7d commit 49e0b3a
Show file tree
Hide file tree
Showing 10 changed files with 383 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .htaccess
@@ -0,0 +1,11 @@
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.html [L,QSA]

RewriteCond %{HTTP:User-Agent} Gecko/
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /http-index-format_automator.php?dir=%{REQUEST_FILENAME} [L,QSA]
45 changes: 45 additions & 0 deletions COPYING.md
@@ -0,0 +1,45 @@
This program is dual-licensed under the GNU GPL v3 and the X11/MIT license.

X11/MIT license
---------------

Copyright (c) 2009 Elijah Grey

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

GNU GPL v3 license
------------------

Copyright (c) 2009 Elijah Grey

This software is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
1 change: 1 addition & 0 deletions README.md
@@ -0,0 +1 @@
The jData host JavaScript library
93 changes: 93 additions & 0 deletions api/jdata.js
@@ -0,0 +1,93 @@
/*
jData Host - v0.0.3 - code.eligrey.com/jdata/host/
@license Dual-licensed GNU GPL v3 and the X11/MIT license
@author Elijah Grey - www.eligrey.com
const Contact_Email_Address = ['\x65\x6c\x69\x6a\x61\x68','\x67\x72\x65\x79\x2e\x6e\x61\x6d\x65'].join('\x40');
*/

// conditional operator statements instead of a lot of if...else statements to make smallest code possible when minified

var u = 'undefined', f = false, t = true, h = 'trustedHosts', s = typeof localStorage != u ? localStorage : globalStorage[location.hostname], w = 'string';

if ( typeof postMessage != u )
{

if ( typeof JSON == u ) document.documentElement.appendChild(document.createElement('script')).src='../json2.js';

if ( !s.getItem(h) ) {
s.setItem(h, "[]");
}

if ( !Array.prototype.indexOf ) { // implement Array.indexOf in IE without fromIndex as it won't be used
Array.prototype.indexOf = function(obj){
for (var i=0; i<this.length; i++) {
if (this[i] == obj) {
return i;
}
}
return -1;
}
}

function m(evt) { // window postMessage listener(evt)
var request = JSON.parse(evt.data),
messageID = request.id,
trustedHosts = JSON.parse(s.getItem(h).toString()),
item = request.item,
requestMethod = request.method,
originHost = evt.origin.replace(/^.+?:(\/\/)?/,''); // User doesn't need to see the protocol:// part
if ( typeof item == w ) var lowerCaseItem = item.toLowerCase();
function reply(r, isStorageItem) { // reply function(response)
var responseObject = {};
if ( typeof messageID != u ) responseObject.id = messageID;
responseObject.response = isStorageItem && typeof r != u && r !== null ? r.toString() : r;
responseObject.jdata = true;
evt.source.postMessage(JSON.stringify(responseObject), '*');
}
if ( requestMethod == 'get' && typeof item == w ) reply(s.getItem(lowerCaseItem)||null, t);
else if ( ( requestMethod == 'set' || requestMethod == 'remove' ) && typeof item == w ) {
var requestSet = requestMethod == 'set';
if ( item == h ) reply(f);
else trustedHosts.indexOf(evt.origin)+1
|| (!requestSet ? !s.getItem(lowerCaseItem) : false)
|| confirm('Allow '+originHost+' to '+(requestSet? 'modify the value of':'remove')+' the publicly shared item, '+item+'?')
&& item != h
? ( requestSet && typeof request.value == w
? s.setItem(lowerCaseItem, request.value)
: s.removeItem(lowerCaseItem), reply(t)
) : reply(f);
}
else if ( requestMethod == 'list' ) {
var itemList = [];
for ( i = 0; i<s.length; i++ ) {
itemList.push(s.key(i));
}
reply(itemList);
}
else if ( requestMethod == 'length' ) reply(s.length);
else if ( requestMethod == 'trust' ) trustedHosts.indexOf(evt.origin)+1
? reply(t) : confirm('Add '+originHost+' to trusted hosts?\nA trusted host does not need your permission to modify publicly shared data.\nYou can manage trusted hosts at '+location.href.substr(0,location.href.length-20)+'/manage')
? (trustedHosts.push(evt.origin), trustedHosts = JSON.stringify(trustedHosts), s.setItem(h, trustedHosts), trustedHosts = JSON.parse(trustedHosts), reply(t)) : reply(f);
else if ( requestMethod == 'untrust' ) {
if ( trustedHosts.indexOf(evt.origin)+1 ) {
trustedHosts.splice(trustedHosts.indexOf(evt.origin), 1),
trustedHosts = JSON.stringify(trustedHosts),
s.setItem(h, trustedHosts);
}
reply(t);
}
}

function v( obj, type, fn ) { // addevent
if ( obj.attachEvent ) {
obj.attachEvent( 'on'+type, fn );
} else {
obj.addEventListener( type, fn, false );
}
}

onload = function() {
v(this, "message", m)
}

}
1 change: 1 addition & 0 deletions api/postMessage.html
@@ -0,0 +1 @@
<!doctype html><script type="text/javascript" src="jdata.js"></script>
1 change: 1 addition & 0 deletions json2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 49 additions & 0 deletions manage.html
@@ -0,0 +1,49 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>jData Manager</title>
<style type="text/css">
body {
text-align: center;
}
input[type="text"], select {
min-width: 200px;
max-width: 500px;
width: 40%;
}
</style>
</head>
<body>
<h1>jData Manager</h1>
<form action="" method="get" onsubmit="localStorage.setItem(document.getElementById('chosenItem').value,document.getElementById('chosenItemValue').value);reloadItemList();return false;">
<fieldset>
<legend><label for="chosenItem">Remove/View/Modify Items</label></legend>
<p><label for="chosenItem">Item </label><select id="chosenItem" onchange="getValue(this.value)"><option/></select></p>
<p><label for="chosenItemValue">Value </label><input type="text" id="chosenItemValue" /></p>
<p><input type="submit" value="Modify Value" /> <input type="button" onclick="localStorage.removeItem(document.getElementById('chosenItem').value);reloadItemList()" value="Remove" /> <input type="button" value="Reload" onclick="reloadItemList()" /></p>
</fieldset>
</form>
<form action="" method="get" id="setform" onsubmit="if(getElementById('set').value!='')localStorage.setItem(document.getElementById('set').value, document.getElementById('val').value),reloadItemList();return false;">
<fieldset>
<legend><label for="set">Add/Change Items</label></legend>
<p><label for="set">Item </label><input type="text" id="set"/></p>
<p><label for="val">Value </label><input type="text" id="val"/></p>
<p><input type="submit" value="Add" /></p>
</fieldset>
</form>
<form action="" method="get" onsubmit="removeHost(document.getElementById('chosenHost').value);reloadHostList();return false;">
<fieldset>
<legend><label for="chosenHost">Manage Trusted Hosts</label></legend>
<p><label for="chosenHost">Host </label><select id="chosenHost"><option/></select></p>
<p><input type="submit" value="Remove"/> <input type="button" value="Reload" onclick="reloadHostList()"/></p>
</fieldset>
</form>
<fieldset>
<legend><label for="removeItems">Remove All Items</label></legend>
<p>Once you remove all items from this host, you cannot restore it unless you <a href="migrate">migrated</a> it to another jData host.</p>
<p><input type="button" id="removeItems" value="Do It" onclick="removeAllItems()" /></p>
</fieldset>
<script type="text/javascript" src="manage.js"></script>
</body>
</html>
102 changes: 102 additions & 0 deletions manage.js
@@ -0,0 +1,102 @@

if ( typeof JSON == "undefined" ) { // load JSON if it doesn't already exist
document.getElementsByTagName('head')[0].appendChild(document.createElement('script')).src = 'json2.js';
}

if ( typeof localStorage == "undefined" && typeof globalStorage != "undefined" ) {
var localStorage = globalStorage[location.hostname];
}

var tHStorage = 'trustedHosts';

if ( !localStorage.getItem(tHStorage) ) {
localStorage.setItem(tHStorage, '[]');
}

var trustedHostsList;

function reloadHosts() {
trustedHostsList = JSON.parse(localStorage.getItem(tHStorage).toString());
}

function reloadHostList() {
document.getElementById('chosenHost').innerHTML = '';
enumerateHosts();
}

function enumerateHosts() {
reloadHosts();
var hostList = document.getElementById('chosenHost'),
ranOnce = false;
for ( var i = 0; i<trustedHostsList.length; i++ ) {
var option = document.createElement('option');
if (ranOnce == false) {
option.setAttribute('selected','true');
ranOnce = true;
}
option.setAttribute('value', i);
option.innerHTML = trustedHostsList[i];
hostList.appendChild(option);
}
}

function removeHost(hostIndex) {
trustedHostsList.splice(hostIndex, 1);
localStorage.setItem(tHStorage, JSON.stringify(trustedHostsList));
reloadHostList();
}

function enumerateItems() {
var itemList = document.getElementById('chosenItem'),
itemValue = document.getElementById('chosenItemValue'),
ranOnce = false;
for (var i=0; i<localStorage.length; i++)
{
var itemName = localStorage.key(i);
if ( itemName != 'trustedHosts' ) {
var option = document.createElement('option');
if (ranOnce == false) {
itemValue.value = localStorage.getItem(itemName);
option.setAttribute('selected','true');
ranOnce = true;
}
option.setAttribute('value', itemName);
option.innerHTML = itemName;
itemList.appendChild(option);
}
}
};

function getValue(item) {
var itemValue = document.getElementById('chosenItemValue');
itemValue.value = localStorage.getItem(item);
}

function reloadItemList() {
document.getElementById('chosenItem').innerHTML = '';
document.getElementById('chosenItemValue').value = '';
enumerateItems();
}

function getItemList() {
var itemList = [];
for ( i = 0; i<localStorage.length; i++ ) {
itemList.push(localStorage.key(i));
}
return itemList;
}

function removeAllItems() {
if ( confirm('Are you sure you want to remove all items from '+location.host+'?') ) {
var items = getItemList();
for ( i=0; i<items.length; i++) {
if ( items[i] != 'trustedHosts' ) localStorage.removeItem(items[i]);
}
reloadItemList();
}
}

window.onload = function() {
reloadItemList();
reloadHostList();
};
35 changes: 35 additions & 0 deletions migrate.html
@@ -0,0 +1,35 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="http://code.eligrey.com/jdata/jil/latest/jil.min.js"></script>
<title>jData Migration Manager</title>
<style type="text/css">
body {
text-align: center;
}
input[type="text"], select {
min-width: 200px;
max-width: 500px;
width: 40%;
}
</style>
</head>
<body>
<h1>jData Migration</h1>
<form action="" method="get" onsubmit="copyDataToHost(document.getElementById('host').value, document.getElementById('path').value);return false;">
<fieldset>
<legend><label for="host">Copy Data To New Host</label></legend>
<p><label for="host">jData Origin </label><input type="text" id="host" value="http://" /> (Do not include ending slash)</p>
<p><label for="path">Path </label><input type="text" id="path" value="" /> (Leave blank if you don't know what this is)</p>
<p><input type="submit" value="Copy Data" /></p>
</fieldset>
</form>
<fieldset>
<legend><label for="removeItems">Remove All Items</label></legend>
<p>Once you remove all items from this host, you cannot restore it unless you migrated it to another jData host.</p>
<p><input type="button" id="removeItems" value="Do It" onclick="removeAllItems()" /></p>
</fieldset>
<script type="text/javascript" src="migrate.js"></script>
</body>
</html>
45 changes: 45 additions & 0 deletions migrate.js
@@ -0,0 +1,45 @@
if ( typeof localStorage == "undefined" && typeof globalStorage != "undefined" ) {
var localStorage = globalStorage[location.hostname];
}


function getItemList() {
var itemList = [];
for ( i = 0; i<localStorage.length; i++ ) {
itemList.push(localStorage.key(i));
}
return itemList;
}



function copyDataToHost(newHost, path) {
if ( JIL.ready == true ) {
var originalOrigin = JIL.origin, originalPath = JIL.path, items = getItemList();
JIL.origin = newHost;
JIL.path = path;
JIL.loadFrame(function() {
JIL.trust(function(trusted) {
if (trusted == true) {
for ( var i=0; i<items.length; i++ ) { if ( items[i] != 'trustedHosts' && items[i] === items[i].toLowerCase() ) { // can't be trusedHosts or non-lowercase items
JIL.set(items[i], localStorage.getItem(items[i]));
}}
JIL.untrust();
alert('Data copy complete.');
}
});
});
JIL.origin = originalOrigin;
JIL.path = originalPath;
JIL.loadFrame();
}
}

function removeAllItems() {
if ( confirm('Are you sure you want to remove all items from '+location.host+'?') ) {
var items = getItemList();
for ( i=0; i<items.length; i++) {
if ( items[i] != 'trustedHosts' && items[i] === items[i].toLowerCase() ) localStorage.removeItem(items[i]);
}
}
}

0 comments on commit 49e0b3a

Please sign in to comment.