Show More
@@ -1,132 +1,133 b'' | |||||
1 | var ITEM_FILE_SOURCE = 'fileSource'; |
|
1 | var ITEM_FILE_SOURCE = 'fileSource'; | |
2 |
|
2 | |||
3 | $('input[name=image]').wrap($('<div class="file_wrap"></div>')); |
|
3 | $('input[name=image]').wrap($('<div class="file_wrap"></div>')); | |
4 |
|
4 | |||
5 | $('body').on('change', 'input[name=image]', function(event) { |
|
5 | $('body').on('change', 'input[name=image]', function(event) { | |
6 | var file = event.target.files[0]; |
|
6 | var file = event.target.files[0]; | |
7 |
|
7 | |||
8 | if(file.type.match('image.*')) { |
|
8 | if(file.type.match('image.*')) { | |
9 | var fileReader = new FileReader(); |
|
9 | var fileReader = new FileReader(); | |
10 |
|
10 | |||
11 | fileReader.addEventListener("load", function(event) { |
|
11 | fileReader.addEventListener("load", function(event) { | |
12 | var wrapper = $('.file_wrap'); |
|
12 | var wrapper = $('.file_wrap'); | |
13 |
|
13 | |||
14 | wrapper.find('.file-thumb').remove(); |
|
14 | wrapper.find('.file-thumb').remove(); | |
15 | wrapper.append( |
|
15 | wrapper.append( | |
16 | $('<div class="file-thumb" style="background-image: url('+event.target.result+')"></div>') |
|
16 | $('<div class="file-thumb" style="background-image: url('+event.target.result+')"></div>') | |
17 | ); |
|
17 | ); | |
18 | }); |
|
18 | }); | |
19 |
|
19 | |||
20 | fileReader.readAsDataURL(file); |
|
20 | fileReader.readAsDataURL(file); | |
21 | } |
|
21 | } | |
22 | }); |
|
22 | }); | |
23 |
|
23 | |||
24 | var form = $('#form'); |
|
24 | var form = $('#form'); | |
25 | $('textarea').keypress(function(event) { |
|
25 | $('textarea').keypress(function(event) { | |
26 | if (event.which == 13 && event.ctrlKey) { |
|
26 | if (event.which == 13 && event.ctrlKey) { | |
27 | form.find('input[type=submit]').click(); |
|
27 | form.find('input[type=submit]').click(); | |
28 | } |
|
28 | } | |
29 | }); |
|
29 | }); | |
30 |
|
30 | |||
31 | $('#preview-button').click(function() { |
|
31 | $('#preview-button').click(function() { | |
32 | var data = { |
|
32 | var data = { | |
33 | raw_text: $('textarea').val() |
|
33 | raw_text: $('textarea').val() | |
34 | } |
|
34 | } | |
35 |
|
35 | |||
36 | var diffUrl = '/api/preview/'; |
|
36 | var diffUrl = '/api/preview/'; | |
37 |
|
37 | |||
38 | $.post(diffUrl, |
|
38 | $.post(diffUrl, | |
39 | data, |
|
39 | data, | |
40 | function(data) { |
|
40 | function(data) { | |
41 | var previewTextBlock = $('#preview-text'); |
|
41 | var previewTextBlock = $('#preview-text'); | |
42 | previewTextBlock.html(data); |
|
42 | previewTextBlock.html(data); | |
43 | previewTextBlock.show(); |
|
43 | previewTextBlock.show(); | |
44 | }) |
|
44 | }) | |
45 | }); |
|
45 | }); | |
46 |
|
46 | |||
47 | /** |
|
47 | /** | |
48 | * Show text in the errors row of the form. |
|
48 | * Show text in the errors row of the form. | |
49 | * @param form |
|
49 | * @param form | |
50 | * @param text |
|
50 | * @param text | |
51 | */ |
|
51 | */ | |
52 | function showAsErrors(form, text) { |
|
52 | function showAsErrors(form, text) { | |
53 | form.children('.form-errors').remove(); |
|
53 | form.children('.form-errors').remove(); | |
54 |
|
54 | |||
55 | if (text.length > 0) { |
|
55 | if (text.length > 0) { | |
56 | var errorList = $('<div class="form-errors">' + text + '<div>'); |
|
56 | var errorList = $('<div class="form-errors">' + text + '<div>'); | |
57 | errorList.appendTo(form); |
|
57 | errorList.appendTo(form); | |
58 | } |
|
58 | } | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | function addHiddenInput(form, name, value) { |
|
61 | function addHiddenInput(form, name, value) { | |
62 | form.find('input[name=' + name + ']').val(value); |
|
62 | form.find('input[name=' + name + ']').val(value); | |
63 | } |
|
63 | } | |
64 |
|
64 | |||
65 | function selectFileChoice() { |
|
65 | function selectFileChoice() { | |
66 | var file_input = $('#id_file'); |
|
66 | var file_input = $('#id_file'); | |
67 | var url_input = $('#id_file_url'); |
|
67 | var url_input = $('#id_file_url'); | |
68 |
|
68 | |||
69 | var file_input_row = file_input.parent().parent(); |
|
69 | var file_input_row = file_input.parent().parent(); | |
70 | var url_input_row = url_input.parent().parent(); |
|
70 | var url_input_row = url_input.parent().parent(); | |
71 |
|
71 | |||
72 | file_input_row.toggle(); |
|
72 | file_input_row.toggle(); | |
73 | url_input_row.toggle(); |
|
73 | url_input_row.toggle(); | |
74 | url_input.val(''); |
|
74 | url_input.val(''); | |
75 | file_input.val(''); |
|
75 | file_input.val(''); | |
76 |
|
76 | |||
77 | var source; |
|
77 | var source; | |
78 | if (file_input_row.is(':visible')) { |
|
78 | if (file_input_row.is(':visible')) { | |
79 | source = 'file'; |
|
79 | source = 'file'; | |
80 | } else { |
|
80 | } else { | |
81 | source = 'url'; |
|
81 | source = 'url'; | |
82 | } |
|
82 | } | |
83 | localStorage.setItem(ITEM_FILE_SOURCE, source); |
|
83 | localStorage.setItem(ITEM_FILE_SOURCE, source); | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | $(document).ready(function() { |
|
86 | $(document).ready(function() { | |
87 | var powDifficulty = parseInt($('body').attr('data-pow-difficulty')); |
|
87 | var powDifficulty = parseInt($('body').attr('data-pow-difficulty')); | |
88 | if (powDifficulty > 0) { |
|
88 | if (powDifficulty > 0) { | |
89 | var worker = new Worker($('#powScript').attr('src')); |
|
89 | var worker = new SharedWorker($('#powScript').attr('src')); | |
90 | worker.onmessage = function(e) { |
|
90 | worker.port.onmessage = function(e) { | |
91 | var form = $('#form'); |
|
91 | var form = $('#form'); | |
92 | addHiddenInput(form, 'timestamp', e.data.timestamp); |
|
92 | addHiddenInput(form, 'timestamp', e.data.timestamp); | |
93 | addHiddenInput(form, 'iteration', e.data.iteration); |
|
93 | addHiddenInput(form, 'iteration', e.data.iteration); | |
94 | addHiddenInput(form, 'guess', e.data.guess); |
|
94 | addHiddenInput(form, 'guess', e.data.guess); | |
95 |
|
95 | |||
96 | form.submit(); |
|
96 | form.submit(); | |
97 | $('.post-form-w').unblock(); |
|
97 | $('.post-form-w').unblock(); | |
98 | }; |
|
98 | }; | |
|
99 | worker.port.start(); | |||
99 |
|
100 | |||
100 | var form = $('#form'); |
|
101 | var form = $('#form'); | |
101 | var submitButton = form.find('input[type=submit]'); |
|
102 | var submitButton = form.find('input[type=submit]'); | |
102 | submitButton.click(function() { |
|
103 | submitButton.click(function() { | |
103 | showAsErrors(form, gettext('Computing PoW...')); |
|
104 | showAsErrors(form, gettext('Computing PoW...')); | |
104 | $('.post-form-w').block({ message: gettext('Computing PoW...') }) |
|
105 | $('.post-form-w').block({ message: gettext('Computing PoW...') }) | |
105 |
|
106 | |||
106 | var msg = $('textarea').val().trim(); |
|
107 | var msg = $('textarea').val().trim(); | |
107 |
|
108 | |||
108 | var data = { |
|
109 | var data = { | |
109 | msg: msg, |
|
110 | msg: msg, | |
110 | difficulty: parseInt($('body').attr('data-pow-difficulty')), |
|
111 | difficulty: parseInt($('body').attr('data-pow-difficulty')), | |
111 | hasher: $('#sha256Script').attr('src') |
|
112 | hasher: $('#sha256Script').attr('src') | |
112 | }; |
|
113 | }; | |
113 | worker.postMessage(data); |
|
114 | worker.port.postMessage(data); | |
114 |
|
115 | |||
115 | return false; |
|
116 | return false; | |
116 | }); |
|
117 | }); | |
117 | } |
|
118 | } | |
118 |
|
119 | |||
119 | var source = localStorage.getItem(ITEM_FILE_SOURCE); |
|
120 | var source = localStorage.getItem(ITEM_FILE_SOURCE); | |
120 | if (source == null) { |
|
121 | if (source == null) { | |
121 | source = 'file'; |
|
122 | source = 'file'; | |
122 | } |
|
123 | } | |
123 | if (source == 'file') { |
|
124 | if (source == 'file') { | |
124 | $('#id_file_url').parent().parent().hide(); |
|
125 | $('#id_file_url').parent().parent().hide(); | |
125 | } else { |
|
126 | } else { | |
126 | $('#id_file').parent().parent().hide(); |
|
127 | $('#id_file').parent().parent().hide(); | |
127 | } |
|
128 | } | |
128 |
|
129 | |||
129 | $('#file-source-button').click(function() { |
|
130 | $('#file-source-button').click(function() { | |
130 | selectFileChoice(); |
|
131 | selectFileChoice(); | |
131 | }); |
|
132 | }); | |
132 | }); |
|
133 | }); |
@@ -1,54 +1,63 b'' | |||||
1 | var POW_COMPUTING_TIMEOUT = 2; |
|
1 | var POW_COMPUTING_TIMEOUT = 2; | |
2 | var POW_HASH_LENGTH = 16; |
|
2 | var POW_HASH_LENGTH = 16; | |
3 |
|
3 | |||
|
4 | var hasher; | |||
4 |
|
5 | |||
5 | function computeHash(iteration, guess, target, payload, timestamp, hasher) { |
|
6 | ||
|
7 | function computeHash(iteration, guess, target, payload, timestamp, hasher, port) { | |||
6 | iteration += 1; |
|
8 | iteration += 1; | |
7 | var hash = hasher(payload + iteration).toString(); |
|
9 | var hash = hasher(payload + iteration).toString(); | |
8 | guess = hash.substring(0, POW_HASH_LENGTH); |
|
10 | guess = hash.substring(0, POW_HASH_LENGTH); | |
9 |
|
11 | |||
10 | if (guess <= target) { |
|
12 | if (guess <= target) { | |
11 | //console.log("Iteration: ", iteration); |
|
13 | //console.log("Iteration: ", iteration); | |
12 | //console.log("Guess: ", guess); |
|
14 | //console.log("Guess: ", guess); | |
13 | //console.log("Target: ", target); |
|
15 | //console.log("Target: ", target); | |
14 |
|
16 | |||
15 | var data = { |
|
17 | var data = { | |
16 | iteration: iteration, |
|
18 | iteration: iteration, | |
17 | timestamp: timestamp, |
|
19 | timestamp: timestamp, | |
18 | guess: guess |
|
20 | guess: guess | |
19 | }; |
|
21 | }; | |
20 |
|
|
22 | port.postMessage(data); | |
21 | } else { |
|
23 | } else { | |
22 | //console.log("Iteration: ", iteration); |
|
24 | //console.log("Iteration: ", iteration); | |
23 | //console.log("Guess: ", guess); |
|
25 | //console.log("Guess: ", guess); | |
24 | //console.log("Target: ", target); |
|
26 | //console.log("Target: ", target); | |
25 |
|
27 | |||
26 | setTimeout(function() { |
|
28 | setTimeout(function() { | |
27 | computeHash(iteration, guess, target, payload, timestamp, hasher); |
|
29 | computeHash(iteration, guess, target, payload, timestamp, hasher, port); | |
28 | }, POW_COMPUTING_TIMEOUT); |
|
30 | }, POW_COMPUTING_TIMEOUT); | |
29 | } |
|
31 | } | |
30 | } |
|
32 | } | |
31 |
|
33 | |||
32 | function doWork(message, hasher, difficulty) { |
|
34 | function doWork(message, hasher, difficulty, port) { | |
33 | var timestamp = Date.now(); |
|
35 | var timestamp = Date.now(); | |
34 | var iteration = 0; |
|
36 | var iteration = 0; | |
35 | var payload = timestamp + message; |
|
37 | var payload = timestamp + message; | |
36 |
|
38 | |||
37 | var target = parseInt(Math.pow(2, POW_HASH_LENGTH * 3) / difficulty).toString(); |
|
39 | var target = parseInt(Math.pow(2, POW_HASH_LENGTH * 3) / difficulty).toString(); | |
38 | while (target.length < POW_HASH_LENGTH) { |
|
40 | while (target.length < POW_HASH_LENGTH) { | |
39 | target = '0' + target; |
|
41 | target = '0' + target; | |
40 | } |
|
42 | } | |
41 |
|
43 | |||
42 | var guess = target + '0'; |
|
44 | var guess = target + '0'; | |
43 |
|
45 | |||
44 | setTimeout(function() { |
|
46 | setTimeout(function() { | |
45 | computeHash(iteration, guess, target, payload, timestamp, hasher); |
|
47 | computeHash(iteration, guess, target, payload, timestamp, hasher, port); | |
46 | }, POW_COMPUTING_TIMEOUT); |
|
48 | }, POW_COMPUTING_TIMEOUT); | |
47 | } |
|
49 | } | |
48 |
|
50 | |||
49 |
|
|
51 | onconnect = function(e) { | |
50 | var difficulty = e.data.difficulty; |
|
52 | var port = e.ports[0]; | |
51 | importScripts(e.data.hasher); |
|
53 | port.start(); | |
52 | var hasher = CryptoJS.SHA256; |
|
54 | ||
53 | self.doWork(e.data.msg, hasher, difficulty); |
|
55 | port.onmessage = function(e) { | |
54 | }; |
|
56 | var difficulty = e.data.difficulty; | |
|
57 | importScripts(e.data.hasher); | |||
|
58 | var hasher = CryptoJS.SHA256; | |||
|
59 | ||||
|
60 | self.doWork(e.data.msg, hasher, difficulty, port); | |||
|
61 | }; | |||
|
62 | } | |||
|
63 |
General Comments 0
You need to be logged in to leave comments.
Login now