/*\ title: $:/plugins/dropbox/dropbox.js type: application/javascript module-type: startup Startup the Dropbox integration plugin \*/ (function(){ /*jslint node: true, browser: true */ /*global $tw: false */ "use strict"; // Obfuscated API key var apiKey = "m+qwjj8wFRA=|1TSoitGS9Nz2RTwv+jrUJnsAj0yy57NhQJ4TkZ/+Hw=="; // Tiddler titles var titleIsLoggedIn = "$:/plugins/dropbox/IsLoggedIn", titleUserName = "$:/plugins/dropbox/UserName", titlePublicAppUrl = "$:/plugins/dropbox/PublicAppUrl"; // Query string marker for forcing authentication var queryLoginMarker = "login=true"; $tw.plugins.dropbox = { client: null // Dropbox.js client object }; // Error handling $tw.plugins.dropbox.showError = function(error) { alert("Dropbox error: " + error); console.log("Dropbox error: " + error); }; // Authenticate $tw.plugins.dropbox.login = function() { $tw.plugins.dropbox.client.authenticate(function(error, client) { if(error) { return $tw.plugins.dropbox.showError(error); } // Mark us as logged in $tw.wiki.addTiddler({title: titleIsLoggedIn, text: "yes"},true); // Get user information $tw.plugins.dropbox.getUserInfo(function() { // Invoke any dropbox-startup modules var mods = $tw.modules.types["dropbox-startup"]; for(var m=0; m> 18; // 16515072 = (2^6 - 1) << 18 b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12 c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6 d = chunk & 63; // 63 = 2^6 - 1 // Convert the raw binary segments to the appropriate ASCII encoding base64.push(charmap[a],charmap[b],charmap[c],charmap[d]); } // Deal with the remaining bytes and padding if(byteRemainder === 1) { chunk = data[mainLength]; a = (chunk & 252) >> 2; // 252 = (2^6 - 1) << 2 // Set the 4 least significant bits to zero b = (chunk & 3) << 4; // 3 = 2^2 - 1 base64.push(charmap[a],charmap[b],"=="); } else if(byteRemainder === 2) { chunk = (data[mainLength] << 8) | data[mainLength + 1]; a = (chunk & 64512) >> 10; // 64512 = (2^6 - 1) << 10 b = (chunk & 1008) >> 4; // 1008 = (2^6 - 1) << 4 // Set the 2 least significant bits to zero c = (chunk & 15) << 2; // 15 = 2^4 - 1 base64.push(charmap[a],charmap[b],charmap[c],"="); } return base64.join(""); }; // Rewrite the document location to include a force login marker $tw.plugins.dropbox.forceLogin = function() { if(document.location.search.indexOf(queryLoginMarker) === -1) { document.location.search = queryLoginMarker; } }; exports.startup = function() { if(!$tw.browser) { return; } // Mark us as not logged in $tw.wiki.addTiddler({title: titleIsLoggedIn, text: "no"},true); // Initialise Dropbox for sandbox access $tw.plugins.dropbox.client = new Dropbox.Client({key: apiKey, sandbox: true}); // Use the basic redirection authentication driver $tw.plugins.dropbox.client.authDriver(new Dropbox.Drivers.Redirect({rememberUser: true})); // Authenticate ourselves if the marker is in the document query string if(document.location.search.indexOf(queryLoginMarker) !== -1) { $tw.plugins.dropbox.login(); } }; })();