Browse Source

Fixed upload prompt/upload not working for both firefox and chrome

master
Teknikode 3 years ago
parent
commit
0882fddfa8
9 changed files with 89 additions and 53 deletions
  1. 2
    12
      manifest.json
  2. 36
    7
      scripts/background.js
  3. 5
    1
      scripts/common.js
  4. 0
    3
      scripts/paste.js
  5. 21
    12
      scripts/popup.js
  6. 0
    2
      scripts/shorten.js
  7. 4
    14
      scripts/upload.js
  8. 19
    0
      views/background.html
  9. 2
    2
      views/popup.html

+ 2
- 12
manifest.json View File

@@ -37,18 +37,8 @@
},

"background": {
"scripts": [
"scripts/JQuery/jquery-3.1.1.min.js",
"scripts/Crypto-js/crypto-js.min.js",
"scripts/secure-random/secure-random.js",
"scripts/Mozilla/browser-polyfill.js",
"scripts/common.js",
"scripts/upload.js",
"scripts/paste.js",
"scripts/shorten.js",
"scripts/background.js"
],
"persistent": false
"page": "views/background.html",
"persistent": true
},
"options_ui": {

+ 36
- 7
scripts/background.js View File

@@ -1,3 +1,15 @@
$(document).ready(function () {
// Handle the addition of files
$('#files').change(function (e) {
var files = e.target.files;
for (var i = 0; i < files.length; i++) {
var f = files[i];
var fileUrl = window.URL.createObjectURL(f);
uploadFromUrl(fileUrl);
}
});
});

/*
-------- Global State Variable ---------
*/
@@ -57,7 +69,8 @@ browser.contextMenus.onClicked.addListener(function(info, tab) {
// Handle messages to the background processor
browser.runtime.onMessage.addListener(processMessage);

function processMessage(msg, sender, sendResponse) {
function processMessage(msg, sender, sendResponse) {
var returnVal;
switch (msg.cmd) {
case 'progress': // Get the progress of an item
break;
@@ -66,19 +79,35 @@ function processMessage(msg, sender, sendResponse) {
break;
case 'del-item':
delProcessItem(msg.id);
sendResponse(true);
returnVal = true;
break;
case 'get-item':
var foundItem = getProcessItem(msg.id);
sendResponse(foundItem);
returnVal = getProcessItem(msg.id);
break;
case 'get-all-items':
sendResponse(itemList);
returnVal = itemList;
break;
case 'upload-file':
uploadFromUrl(msg.file);
case 'upload-files':
for (var i = 0; i < msg.files.length; i++) {
var f = msg.files[i];
uploadFromUrl(f);
}
break;
case 'prompt-upload':
if (sendResponse) {
// Click the files input
$('#files').click();
returnVal = true;
}
else {
returnVal = false;
}
break;
}
if (sendResponse) {
sendResponse(returnVal);
}
return returnVal;
}

// Updates the process item list

+ 5
- 1
scripts/common.js View File

@@ -4,7 +4,7 @@ We'll just log success/failure here.
*/
function onCreated(n) {
if (browser.runtime.lastError) {
console.log('Error: ${browser.runtime.lastError}');
console.log('Error: ' + browser.runtime.lastError);
} else {
console.log("Item created successfully");
}
@@ -38,6 +38,10 @@ function onError(error) {
console.log('Error: ' + error);
}

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

function notify(title, message) {
console.log(title + ": " + message);
browser.notifications.create({

+ 0
- 3
scripts/paste.js View File

@@ -8,9 +8,6 @@ function createPaste(text) {
data: { code: text },
success: function (response) {
if (response.result) {
var pasteUrl = response.result.url;
browser.tabs.create({ url: pasteUrl });
// Update the browser action
addProcessItem('paste', response.result);
}

+ 21
- 12
scripts/popup.js View File

@@ -2,26 +2,35 @@ $(document).ready(function () {
$('#settings').click(function () {
var opening = browser.runtime.openOptionsPage();
opening.then(onOpened, onError);
});
});
$('#upload').click(function (e) {
// Prompt for uploading a file
$('#files').click();
var sendingFiles = browser.runtime.sendMessage({
cmd: 'prompt-upload'
});
sendingFiles.then(function(result) {
if (!result) {
// Error'ed out, so let's try here
$('#files').click();
}
}, onError);
});
// Handle the addition of files
// Handle the addition of files (Chrome Only)
$('#files').change(function (e) {
var files = e.target.files;
var fileList = [];
for (var i = 0; i < files.length; i++) {
var f = files[i];
var fileUrl = window.URL.createObjectURL(f);
browser.runtime.sendMessage({
cmd: 'upload-file',
file: fileUrl
});
fileList.push(fileUrl);
}
var fileObj = $('#files');
fileObj.replaceWith(fileObj = fileObj.clone(true));
// Send the files for uplaod
browser.runtime.sendMessage({
cmd: 'upload-files',
files: fileList
});
sendingFiles.then(fillItems, onError);
});
});

@@ -61,7 +70,7 @@ function fillItem(item) {
itemDiv.attr('id', 'process-item-' + item.id);
itemDiv.find('#copy').click(function () {
copyTextToClipboard(item.id);
copyItem(item.id);
});
itemDiv.find('#open').click(function () {
@@ -110,7 +119,7 @@ function fillItem(item) {
itemDiv.find('#title').html('Upload');
var fullUrl = item.data.url;
var key = '';
if (item.data.key != null) {
if (item.data.key !== null) {
key = item.data.key;
}
if (!item.data.saveKey) {

+ 0
- 2
scripts/shorten.js View File

@@ -8,8 +8,6 @@ function shortenUrl(url) {
data: { url: url },
success: function (response) {
if (response.result) {
var shortUrl = response.result.shortUrl;
// add the new item
addProcessItem('shorten', response.result);
}

+ 4
- 14
scripts/upload.js View File

@@ -14,7 +14,7 @@ function uploadFromUrl(contentUrl) {
if (data.lengthComputable) {
var percentComplete = parseInt(((data.loaded / data.total) * 100), 10);
// display loaded progress
console.log("Download Progress" + percentComplete + '% Downloaded');
console.log("Download Progress: " + percentComplete + '% Downloaded');
}
}
xhr.send();
@@ -61,7 +61,7 @@ function encryptFile(fileBlob, contentType, saveKey, serverSideEncrypt, genDelKe
worker.onerror = function (err) {
// An error occured
/* NOTIFY SOMEONE */
onError("Upload Error: " + err.message);
onError("Upload Encryption Error: " + err.message);
}

// Execute worker with data
@@ -83,7 +83,7 @@ function encryptFile(fileBlob, contentType, saveKey, serverSideEncrypt, genDelKe
if (data.lengthComputable) {
var percentComplete = parseInt(((data.loaded / data.total) * 100), 10);
// display loaded progress
console.log("Upload Progress" + percentComplete + '% Loaded');
console.log("Upload Progress: " + percentComplete + '% Loaded');
}
}

@@ -127,26 +127,16 @@ function uploadFile(data, key, iv, keySize, blockSize, contentType, saveKey, ser
function uploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
console.log("Upload Progress" + percentComplete + '% Uploaded');
console.log("Upload Progress: " + percentComplete + '% Uploaded');
}
}

function uploadComplete(key, saveKey, evt) {
obj = JSON.parse(evt.target.responseText);
if (obj.result != null) {
var fullUrl = obj.result.url;
if (obj.result.key != null)
key = obj.result.key;
if (!saveKey) {
fullUrl = fullUrl + '#' + key;
}
// Add the saveKey to the results
obj.result.saveKey = saveKey;
// Open a new tab to the upload
browser.tabs.create({ url: fullUrl });
// Add the completed item
addProcessItem('upload', obj.result);
}

+ 19
- 0
views/background.html View File

@@ -0,0 +1,19 @@
<html>
<head>
<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 />
<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>
<script type="application/javascript" src="../scripts/secure-random/secure-random.js"></script>
<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/upload.js"></script>
<script type="application/javascript" src="../scripts/paste.js"></script>
<script type="application/javascript" src="../scripts/shorten.js"></script>
<script type="application/javascript" src="../scripts/background.js"></script>
</body>
</html>

+ 2
- 2
views/popup.html View File

@@ -68,8 +68,8 @@
</div>
</div>
<!-- hidden form for uploads -->
<input type="file" name="files[]" style="opacity: 0; height: 0px;" id="files" multiple />
<!-- hidden form for uploads (Chrome Only) -->
<input type="file" name="files[]" style="opacity: 0; float: left; position: absolute;" id="files" multiple />
<script type="application/javascript" src="../scripts/JQuery/jquery-3.1.1.min.js"></script>
<script type="application/javascript" src="../scripts/Mozilla/browser-polyfill.js"></script>

Loading…
Cancel
Save