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