##// END OF EJS Templates
Add worker js errors to the console
neko259 -
r1464:11612d6d default
parent child Browse files
Show More
@@ -1,133 +1,136 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 SharedWorker($('#powScript').attr('src'));
89 var worker = new SharedWorker($('#powScript').attr('src'));
90 worker.port.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.onerror = function(event){
100 throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
101 };
99 worker.port.start();
102 worker.port.start();
100
103
101 var form = $('#form');
104 var form = $('#form');
102 var submitButton = form.find('input[type=submit]');
105 var submitButton = form.find('input[type=submit]');
103 submitButton.click(function() {
106 submitButton.click(function() {
104 showAsErrors(form, gettext('Computing PoW...'));
107 showAsErrors(form, gettext('Computing PoW...'));
105 $('.post-form-w').block({ message: gettext('Computing PoW...') })
108 $('.post-form-w').block({ message: gettext('Computing PoW...') })
106
109
107 var msg = $('textarea').val().trim();
110 var msg = $('textarea').val().trim();
108
111
109 var data = {
112 var data = {
110 msg: msg,
113 msg: msg,
111 difficulty: parseInt($('body').attr('data-pow-difficulty')),
114 difficulty: parseInt($('body').attr('data-pow-difficulty')),
112 hasher: $('#sha256Script').attr('src')
115 hasher: $('#sha256Script').attr('src')
113 };
116 };
114 worker.port.postMessage(data);
117 worker.port.postMessage(data);
115
118
116 return false;
119 return false;
117 });
120 });
118 }
121 }
119
122
120 var source = localStorage.getItem(ITEM_FILE_SOURCE);
123 var source = localStorage.getItem(ITEM_FILE_SOURCE);
121 if (source == null) {
124 if (source == null) {
122 source = 'file';
125 source = 'file';
123 }
126 }
124 if (source == 'file') {
127 if (source == 'file') {
125 $('#id_file_url').parent().parent().hide();
128 $('#id_file_url').parent().parent().hide();
126 } else {
129 } else {
127 $('#id_file').parent().parent().hide();
130 $('#id_file').parent().parent().hide();
128 }
131 }
129
132
130 $('#file-source-button').click(function() {
133 $('#file-source-button').click(function() {
131 selectFileChoice();
134 selectFileChoice();
132 });
135 });
133 });
136 });
@@ -1,158 +1,161 b''
1 /*
1 /*
2 @licstart The following is the entire license notice for the
2 @licstart The following is the entire license notice for the
3 JavaScript code in this page.
3 JavaScript code in this page.
4
4
5
5
6 Copyright (C) 2013 neko259
6 Copyright (C) 2013 neko259
7
7
8 The JavaScript code in this page is free software: you can
8 The JavaScript code in this page is free software: you can
9 redistribute it and/or modify it under the terms of the GNU
9 redistribute it and/or modify it under the terms of the GNU
10 General Public License (GNU GPL) as published by the Free Software
10 General Public License (GNU GPL) as published by the Free Software
11 Foundation, either version 3 of the License, or (at your option)
11 Foundation, either version 3 of the License, or (at your option)
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
12 any later version. The code is distributed WITHOUT ANY WARRANTY;
13 without even the implied warranty of MERCHANTABILITY or FITNESS
13 without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
14 FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
15
15
16 As additional permission under GNU GPL version 3 section 7, you
16 As additional permission under GNU GPL version 3 section 7, you
17 may distribute non-source (e.g., minimized or compacted) forms of
17 may distribute non-source (e.g., minimized or compacted) forms of
18 that code without the copy of the GNU GPL normally required by
18 that code without the copy of the GNU GPL normally required by
19 section 4, provided you include this license notice and a URL
19 section 4, provided you include this license notice and a URL
20 through which recipients can access the Corresponding Source.
20 through which recipients can access the Corresponding Source.
21
21
22 @licend The above is the entire license notice
22 @licend The above is the entire license notice
23 for the JavaScript code in this page.
23 for the JavaScript code in this page.
24 */
24 */
25
25
26 var ITEM_VOLUME_LEVEL = 'volumeLevel';
26 var ITEM_VOLUME_LEVEL = 'volumeLevel';
27
27
28 /**
28 /**
29 * An email is a hidden file to prevent spam bots from posting. It has to be
29 * An email is a hidden file to prevent spam bots from posting. It has to be
30 * hidden.
30 * hidden.
31 */
31 */
32 function hideEmailFromForm() {
32 function hideEmailFromForm() {
33 $('.form-email').parent().parent().hide();
33 $('.form-email').parent().parent().hide();
34 }
34 }
35
35
36 /**
36 /**
37 * Highlight code blocks with code highlighter
37 * Highlight code blocks with code highlighter
38 */
38 */
39 function highlightCode(node) {
39 function highlightCode(node) {
40 node.find('pre code').each(function(i, e) {
40 node.find('pre code').each(function(i, e) {
41 hljs.highlightBlock(e);
41 hljs.highlightBlock(e);
42 });
42 });
43 }
43 }
44
44
45 function updateFavPosts(data) {
45 function updateFavPosts(data) {
46 var includePostBody = $('#fav-panel').is(":visible");
46 var includePostBody = $('#fav-panel').is(":visible");
47
47
48 var allNewPostCount = 0;
48 var allNewPostCount = 0;
49
49
50 if (includePostBody) {
50 if (includePostBody) {
51 var favoriteThreadPanel = $('#fav-panel');
51 var favoriteThreadPanel = $('#fav-panel');
52 favoriteThreadPanel.empty();
52 favoriteThreadPanel.empty();
53 }
53 }
54
54
55 $.each($.parseJSON(data), function (_, dict) {
55 $.each($.parseJSON(data), function (_, dict) {
56 var newPostCount = dict.new_post_count;
56 var newPostCount = dict.new_post_count;
57 allNewPostCount += newPostCount;
57 allNewPostCount += newPostCount;
58
58
59 if (includePostBody) {
59 if (includePostBody) {
60 var favThreadNode = $('<div class="post"></div>');
60 var favThreadNode = $('<div class="post"></div>');
61 favThreadNode.append($(dict.post_url));
61 favThreadNode.append($(dict.post_url));
62 favThreadNode.append(' ');
62 favThreadNode.append(' ');
63 favThreadNode.append($('<span class="title">' + dict.title + '</span>'));
63 favThreadNode.append($('<span class="title">' + dict.title + '</span>'));
64
64
65 if (newPostCount > 0) {
65 if (newPostCount > 0) {
66 favThreadNode.append(' (<a href="' + dict.newest_post_link + '">+' + newPostCount + "</a>)");
66 favThreadNode.append(' (<a href="' + dict.newest_post_link + '">+' + newPostCount + "</a>)");
67 }
67 }
68
68
69 favoriteThreadPanel.append(favThreadNode);
69 favoriteThreadPanel.append(favThreadNode);
70
70
71 addRefLinkPreview(favThreadNode[0]);
71 addRefLinkPreview(favThreadNode[0]);
72 }
72 }
73 });
73 });
74
74
75 var newPostCountNode = $('#new-fav-post-count');
75 var newPostCountNode = $('#new-fav-post-count');
76 if (allNewPostCount > 0) {
76 if (allNewPostCount > 0) {
77 newPostCountNode.text('(+' + allNewPostCount + ')');
77 newPostCountNode.text('(+' + allNewPostCount + ')');
78 newPostCountNode.show();
78 newPostCountNode.show();
79 } else {
79 } else {
80 newPostCountNode.hide();
80 newPostCountNode.hide();
81 }
81 }
82 }
82 }
83
83
84 function initFavPanel() {
84 function initFavPanel() {
85 var favPanelButton = $('#fav-panel-btn');
85 var favPanelButton = $('#fav-panel-btn');
86 if (favPanelButton.length > 0) {
86 if (favPanelButton.length > 0) {
87 var worker = new SharedWorker($('body').attr('data-update-script'));
87 var worker = new SharedWorker($('body').attr('data-update-script'));
88 worker.port.onmessage = function(e) {
88 worker.port.onmessage = function(e) {
89 updateFavPosts(e.data);
89 updateFavPosts(e.data);
90 };
90 };
91 worker.port.onerror = function(event){
92 throw new Error(event.message + " (" + event.filename + ":" + event.lineno + ")");
93 };
91 worker.port.start();
94 worker.port.start();
92
95
93 $(favPanelButton).click(function() {
96 $(favPanelButton).click(function() {
94 var favPanel = $('#fav-panel');
97 var favPanel = $('#fav-panel');
95 favPanel.toggle();
98 favPanel.toggle();
96
99
97 worker.port.postMessage({ includePostBody: favPanel.is(':visible')});
100 worker.port.postMessage({ includePostBody: favPanel.is(':visible')});
98
101
99 return false;
102 return false;
100 });
103 });
101
104
102 $(document).on('keyup.removepic', function(e) {
105 $(document).on('keyup.removepic', function(e) {
103 if(e.which === 27) {
106 if(e.which === 27) {
104 $('#fav-panel').hide();
107 $('#fav-panel').hide();
105 }
108 }
106 });
109 });
107 }
110 }
108 }
111 }
109
112
110 function setVolumeLevel(level) {
113 function setVolumeLevel(level) {
111 localStorage.setItem(ITEM_VOLUME_LEVEL, level);
114 localStorage.setItem(ITEM_VOLUME_LEVEL, level);
112 }
115 }
113
116
114 function getVolumeLevel() {
117 function getVolumeLevel() {
115 var level = localStorage.getItem(ITEM_VOLUME_LEVEL);
118 var level = localStorage.getItem(ITEM_VOLUME_LEVEL);
116 if (level == null) {
119 if (level == null) {
117 level = 1.0;
120 level = 1.0;
118 }
121 }
119 return level
122 return level
120 }
123 }
121
124
122 function processVolumeUser(node) {
125 function processVolumeUser(node) {
123 node.prop("volume", getVolumeLevel());
126 node.prop("volume", getVolumeLevel());
124 node.on('volumechange', function(event) {
127 node.on('volumechange', function(event) {
125 setVolumeLevel(event.target.volume);
128 setVolumeLevel(event.target.volume);
126 $("video,audio").prop("volume", getVolumeLevel());
129 $("video,audio").prop("volume", getVolumeLevel());
127 });
130 });
128 }
131 }
129
132
130 /**
133 /**
131 * Add all scripts than need to work on post, when the post is added to the
134 * Add all scripts than need to work on post, when the post is added to the
132 * document.
135 * document.
133 */
136 */
134 function addScriptsToPost(post) {
137 function addScriptsToPost(post) {
135 addRefLinkPreview(post[0]);
138 addRefLinkPreview(post[0]);
136 highlightCode(post);
139 highlightCode(post);
137 processVolumeUser(post.find("video,audio"));
140 processVolumeUser(post.find("video,audio"));
138 }
141 }
139
142
140 $( document ).ready(function() {
143 $( document ).ready(function() {
141 hideEmailFromForm();
144 hideEmailFromForm();
142
145
143 $("a[href='#top']").click(function() {
146 $("a[href='#top']").click(function() {
144 $("html, body").animate({ scrollTop: 0 }, "slow");
147 $("html, body").animate({ scrollTop: 0 }, "slow");
145 return false;
148 return false;
146 });
149 });
147
150
148 addImgPreview();
151 addImgPreview();
149
152
150 addRefLinkPreview();
153 addRefLinkPreview();
151
154
152 highlightCode($(document));
155 highlightCode($(document));
153
156
154 initFavPanel();
157 initFavPanel();
155
158
156 var volumeUsers = $("video,audio");
159 var volumeUsers = $("video,audio");
157 processVolumeUser(volumeUsers);
160 processVolumeUser(volumeUsers);
158 });
161 });
General Comments 0
You need to be logged in to leave comments. Login now