Browse Source

Added shortcut for uploads, added more settings, cleaned up item styles in popup

master
Teknikode 3 years ago
parent
commit
93f3106914
10 changed files with 133 additions and 59 deletions
  1. 12
    0
      css/common.css
  2. 6
    14
      css/popup.css
  3. 7
    0
      manifest.json
  4. 24
    7
      scripts/background.js
  5. 25
    0
      scripts/common.js
  6. 19
    14
      scripts/options.js
  7. 2
    2
      scripts/popup.js
  8. 31
    20
      scripts/upload.js
  9. 2
    2
      views/background.html
  10. 5
    0
      views/options.html

+ 12
- 0
css/common.css View File

@@ -1,3 +1,15 @@
.clear {
clear: both;
}

.btn {
cursor: pointer;
}

.btn:hover {
opacity: 0.6;
}

.btn-danger {
color: #d9534f;
}

+ 6
- 14
css/popup.css View File

@@ -9,18 +9,6 @@ body {
margin: 0;
}

.clear {
clear: both;
}

.btn {
cursor: pointer;
}

.btn:hover {
opacity: 0.6;
}

/***********************
Tools Panel
************************/
@@ -74,16 +62,20 @@ body {
}

.item-panel .item-details {
white-space: nowrap;
overflow: hidden;
}

.item-panel .item-details .item-content {
float:left;
display: inline-block;
vertical-align: top;
margin: 5px;
font-size: 16px;
}

.item-panel .item-details .item-actions {
float: left;
display: inline-block;
vertical-align: top;
padding: 6px 8px 6px 6px;
border-right: 1px solid #e7e7e7;
}

+ 7
- 0
manifest.json View File

@@ -30,6 +30,13 @@
"default_title": "Teknik Services",
"default_popup": "views/popup.html"
},
"commands": {
"upload-files": {
"suggested_key": { "default": "Ctrl+Shift+U" },
"description": "Open a prompt for uploading a file."
}
},

"icons": {
"16": "images/favicon-16.png",

+ 24
- 7
scripts/background.js View File

@@ -1,15 +1,21 @@
$(document).ready(function () {
// Handle the addition of files
$('#files').change(function (e) {
var files = e.target.files;
/* Create a file input for prompting for file uploads */
var fileChooser = document.createElement('input');
fileChooser.type = 'file';
fileChooser.addEventListener('change', function () {
var files = fileChooser.files;
for (var i = 0; i < files.length; i++) {
var f = files[i];
var fileUrl = window.URL.createObjectURL(f);
uploadFromUrl(fileUrl);
}
});
form.reset(); // <-- Resets the input so we do get a `change` event,
// even if the user chooses the same file
});

/* Wrap it in a form for resetting */
var form = document.createElement('form');
form.appendChild(fileChooser);

/*
-------- Global State Variable ---------
*/
@@ -66,6 +72,18 @@ browser.contextMenus.onClicked.addListener(function(info, tab) {
}
});

/*
* Fired when a registered command is activated using a keyboard shortcut.
*/
browser.commands.onCommand.addListener((command) => {
switch (command) {
case 'upload-files':
// Click the files input
fileChooser.click();
break;
}
});

// Handle messages to the background processor
browser.runtime.onMessage.addListener(processMessage);

@@ -95,8 +113,7 @@ function processMessage(msg, sender, sendResponse) {
break;
case 'prompt-upload':
if (sendResponse) {
// Click the files input
$('#files').click();
fileChooser.click();
returnVal = true;
}
else {

+ 25
- 0
scripts/common.js View File

@@ -38,6 +38,31 @@ function onError(error) {
console.log('Error: ' + error);
}

/* Get the local storage object */
function getSettings(callback) {
var getting = browser.storage.local.get();
getting.then(function(settings) {
// Load Defaults
if (!settings.auth) {
settings.auth = {
username: '',
authToken: ''
};
}
if (!settings.uploads) {
settings.uploads = {
encrypt: false,
genDelKey: false,
keySize: 256,
blockSize: 128,
chunkSize: 1000
};
}
// Call the callback with the settings
callback(settings);
}, onError);
}

function closeTab(tab) {
var removing = browser.tabs.remove(tab.id);
}

+ 19
- 14
scripts/options.js View File

@@ -1,24 +1,29 @@
function saveOptions(e) {
e.preventDefault();
browser.storage.local.set({
username: document.querySelector("#username").value,
authToken: document.querySelector("#authToken").value
// Auth Settings
auth: {
username: document.querySelector("#username").value,
authToken: document.querySelector("#authToken").value,
},
uploads: {
encrypt: document.querySelector('#encrypt').checked,
genDelKey: false,
keySize: 256,
blockSize: 128,
chunkSize: 1000
}
});
}

function restoreOptions() {

function setCurrentChoice(result) {
document.querySelector("#username").value = result.username || "";
document.querySelector("#authToken").value = result.authToken || "";
}

function onError(error) {
console.log('Error: ${error}');
}
function setCurrentChoice(result) {
document.querySelector("#username").value = result.auth.username;
document.querySelector("#authToken").value = result.auth.authToken;
document.querySelector('#encrypt').checked = result.uploads.encrypt;
}

var getting = browser.storage.local.get();
getting.then(setCurrentChoice, onError);
function restoreOptions() {
getSettings(setCurrentChoice);
}

document.addEventListener("DOMContentLoaded", restoreOptions);

+ 2
- 2
scripts/popup.js View File

@@ -102,7 +102,7 @@ function fillItem(item) {
}
// Add to the main div
$('#process-list').append(itemDiv);
$('#process-list').prepend(itemDiv);
}
// Add the data info depending on the type
@@ -122,7 +122,7 @@ function fillItem(item) {
if (item.data.key !== null) {
key = item.data.key;
}
if (!item.data.saveKey) {
if (item.data.encrypted) {
fullUrl = fullUrl + '#' + key;
}
itemDiv.find('#url').html(fullUrl);

+ 31
- 20
scripts/upload.js View File

@@ -4,8 +4,11 @@ function uploadFromUrl(contentUrl) {
xhr.open('GET', contentUrl);
xhr.responseType = 'blob';
xhr.onload = function() {
// Encrypt the downloaded file
encryptFile(xhr.response, xhr.response.type, false, false, false, 256, 128, 1000, uploadFile);
// Get the current settings before encrypting
getSettings(function(settings) {
// Encrypt the downloaded file
encryptFile(xhr.response, xhr.response.type, settings, uploadFile);
});
};
xhr.onerror = function(err) {
onError("Upload Error: " + err);
@@ -21,7 +24,7 @@ function uploadFromUrl(contentUrl) {
}

// Function to encrypt a file, overide the file's data attribute with the encrypted value, and then call a callback function if supplied
function encryptFile(fileBlob, contentType, saveKey, serverSideEncrypt, genDelKey, keySize, blockSize, chunkSize, callback) {
function encryptFile(fileBlob, contentType, settings, callback) {
var encScriptSrc = browser.extension.getURL('scripts/EncryptionWorker.js');
var aesScriptSrc = browser.extension.getURL('scripts/Crypto-js/crypto-js.min.js');
@@ -32,12 +35,12 @@ function encryptFile(fileBlob, contentType, saveKey, serverSideEncrypt, genDelKe
reader.onload = (function (callback) {
return function (e) {
// Create random key and iv (divide size by 8 for character length)
var keyStr = randomString((keySize / 8), '#aA');
var ivStr = randomString((blockSize / 8), '#aA');
var keyStr = randomString((settings.uploads.keySize / 8), '#aA');
var ivStr = randomString((settings.uploads.blockSize / 8), '#aA');

// Encrypt on the server side if they ask for it
if (serverSideEncrypt) {
callback(e.target.result, keyStr, ivStr, keySize, blockSize, contentType, saveKey, serverSideEncrypt, genDelKey);
if (!settings.uploads.encrypt) {
callback(e.target.result, keyStr, ivStr, contentType, settings);
}
else {
var worker = new Worker(encScriptSrc);
@@ -52,7 +55,7 @@ function encryptFile(fileBlob, contentType, saveKey, serverSideEncrypt, genDelKe
case 'finish':
if (callback != null) {
// Finish
callback(e.data.buffer, keyStr, ivStr, keySize, blockSize, contentType, saveKey, serverSideEncrypt, genDelKey);
callback(e.data.buffer, keyStr, ivStr, contentType, settings);
}
break;
}
@@ -60,7 +63,6 @@ function encryptFile(fileBlob, contentType, saveKey, serverSideEncrypt, genDelKe

worker.onerror = function (err) {
// An error occured
/* NOTIFY SOMEONE */
onError("Upload Encryption Error: " + err.message);
}

@@ -71,7 +73,7 @@ function encryptFile(fileBlob, contentType, saveKey, serverSideEncrypt, genDelKe
script: aesScriptSrc,
key: keyStr,
iv: ivStr,
chunkSize: chunkSize,
chunkSize: settings.uploads.chunkSize,
file: e.target.result
};
worker.postMessage(objData, [objData.file]);
@@ -91,7 +93,7 @@ function encryptFile(fileBlob, contentType, saveKey, serverSideEncrypt, genDelKe
reader.readAsArrayBuffer(fileBlob);
}

function uploadFile(data, key, iv, keySize, blockSize, contentType, saveKey, serverSideEncrypt, genDelKey)
function uploadFile(data, key, iv, contentType, settings)
{
// Get the upload url
var uploadAPI = "https://api.teknik.io/v1/Upload";
@@ -103,24 +105,28 @@ function uploadFile(data, key, iv, keySize, blockSize, contentType, saveKey, ser
var fd = new FormData();
fd.append('file', blob);
fd.append('contentType', contentType);
fd.append('saveKey', saveKey);
if (saveKey)
fd.append('saveKey', !settings.uploads.encrypt);
if (!settings.uploads.encrypt)
{
fd.append('key', key);
}
fd.append('keySize', keySize);
fd.append('keySize', settings.uploads.keySize);
fd.append('iv', iv);
fd.append('blockSize', blockSize);
fd.append('encrypt', serverSideEncrypt);
fd.append('genDeletionKey', genDelKey);
fd.append('blockSize', settings.uploads.blockSize);
fd.append('encrypt', !settings.uploads.encrypt);
fd.append('genDeletionKey', settings.uploads.genDelKey);

// Send the form data and report the results
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", uploadProgress.bind(null), false);
xhr.addEventListener("load", uploadComplete.bind(null, key, saveKey), false);
xhr.addEventListener("load", uploadComplete.bind(null, key, settings.uploads.encrypt), false);
xhr.addEventListener("error", uploadFailed.bind(null), false);
xhr.addEventListener("abort", uploadCanceled.bind(null), false);
xhr.open("POST", uploadAPI);
// If they have basic auth settings, use them
if (settings.auth && settings.auth.username !== '' && settings.auth.authToken !== '') {
xhr.setRequestHeader('Authorization', 'Basic ' + btoa(settings.auth.username + ':' + settings.auth.authToken));
}
xhr.send(fd);
}

@@ -131,11 +137,16 @@ function uploadProgress(evt) {
}
}

function uploadComplete(key, saveKey, evt) {
function uploadComplete(key, encrypted, evt) {
obj = JSON.parse(evt.target.responseText);
if (obj.result != null) {
// Add the saveKey to the results
obj.result.saveKey = saveKey;
obj.result.encrypted = encrypted;
// If we encrypted the file, let's add the key here
if (encrypted) {
obj.result.key = key;
}
// Add the completed item
addProcessItem('upload', obj.result);

+ 2
- 2
views/background.html View File

@@ -3,8 +3,8 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<!-- hidden form for uploads -->
<input type="file" name="files[]" style="opacity: 0; float: left; position: absolute;" id="files" multiple />
<!-- input for uploads -->
<input type="file" name="files[]" id="files" multiple />
<script type="application/javascript" src="../scripts/JQuery/jquery-3.1.1.min.js"></script>
<script type="application/javascript" src="../scripts/Crypto-js/crypto-js.min.js"></script>

+ 5
- 0
views/options.html View File

@@ -20,12 +20,17 @@
<br />
<input type="text" id="authToken" />
<br />
<br />
Encrypt Uploads: <input type="checkbox" id="encrypt" />
<br />
<br />
<button type="submit">Save</button>
</form>

<script type="application/javascript" src="../scripts/Mozilla/browser-polyfill.js"></script>
<script type="application/javascript" src="../scripts/common.js"></script>
<script type="application/javascript" src="../scripts/options.js"></script>

</body>

Loading…
Cancel
Save