Permalink
Browse files

Selections goes to popup input automatically.

Updated jsbeautifier.
Fixed html in colored source bug.
Button in upper part of popup.
Minor fixes.
  • Loading branch information...
1 parent 0854ecd commit a08a1304a140022324fc090cf10bd6c363a6ca52 @Dither committed Jan 22, 2012
Showing with 304 additions and 151 deletions.
  1. +8 −5 css/popup.css
  2. +8 −0 includes/common.js
  3. +144 −122 includes/injected.js
  4. +29 −0 includes/injected_selector.js
  5. +73 −20 includes/readable_js.js
  6. +11 −0 index.html
  7. +31 −4 popup.html
View
@@ -1,13 +1,13 @@
-* {
+* {
margin: 0 0 0 0;
padding: 0 0 0 0;
}
body {
font-size:14px;
font-family: "arial", "helvetica", sans-serif;
overflow:hidden;
-padding-right:4px;
-padding-top:4px;
+padding-right:5px;
+padding-top:0px;
}
fieldset{
border: none;
@@ -16,6 +16,9 @@ textarea {
font-family: "consolas", "Terminus", "lucida console", "courier new", "courier", monospace;
font-size: 12px;
}
+label {
+text-align:center;
+}
button {
background:#eee;
padding:3px 5px;
@@ -32,8 +35,8 @@ border-top-color:#ccc;
border-top-style:solid;
border-top-width:1px;
cursor:pointer;
-margin-top:2px;
+margin-top:0px;
+margin-bottom:7px;
width:100%
}
-
#makereadable:lang(ru){content: 'Сделать читабельным';/*Make readable*/}
View
@@ -14,6 +14,14 @@ function trim_comments(str)
tmp = tmp.replace(/^\s+/g, '');
return tmp;
}
+
+String.prototype.escapeHTMLforJS = function () {
+ return(
+ this.replace(/>/g,'>').
+ replace(/</g,'&lt;')/*.
+ replace(/"/g,'&quot;')*/
+ );
+};
function unpacker_filter(source)
{
View
@@ -3,26 +3,32 @@
// ==/UserScript==
(function () {
-// opera doesn't set title unless it's error response
-if (!window.location.href.match(/\.js(?:\?|$)/) || typeof document == undefined || document.getElementsByTagName('TITLE')[0] || !document.body) return;
-
-var storage = widget.preferences;
-function log(){ if (s2b(storage['debug_output'])) opera.postError(Array.prototype.slice.call(arguments))}
-
-///////////////////////////////////// STYLES AND TRANSLATION /////////////////////////////////////////
-
-var JSREADABLE_STRINGS = (function(locale){ var lang = window.navigator.language.slice(0,2); return locale[lang] || locale["en"];})({
- ru: {
- hint: "\u0424\u0430\u0439\u043b \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 JavaScript. \u041a\u043b\u0438\u043a\u043d\u0438\u0442\u0435 \u043d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u043f\u043b\u044b\u0432\u0430\u044e\u0449\u0435\u043c \u043e\u043a\u043e\u0448\u043a\u0435 \u0434\u043b\u044f \u0435\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0410\u0432\u0442\u043e\u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 ",
- sec: "\u0441\u0435\u043a.",
- },
- en: {
- hint: 'This file looks like a JavaScript. Click this popup to format it. It will close in ',
- sec: 'sec.',
- }
-});
+ // current versions of Opera doesn't set title unless it's error response
+ if (!window.location.href.match(/\.js(?:\?|$)/) || typeof document == undefined || document.getElementsByTagName('TITLE')[0] || !document.body) return;
+
+ var storage = widget.preferences;
-var cssinfo = '/** CSS for bar mode */\
+ function log() {
+ if (s2b(storage['debug_output'])) opera.postError('[Readable JavaScript]: ' + Array.prototype.slice.call(arguments))
+ }
+
+ ///////////////////////////////////// STYLES AND TRANSLATION /////////////////////////////////////////
+
+ var READABLEJS_STRINGS = (function (locale) {
+ var lang = window.navigator.language.slice(0, 2);
+ return locale[lang] || locale["en"];
+ })({
+ ru: {
+ hint: "\u0424\u0430\u0439\u043b \u043f\u043e\u0445\u043e\u0436 \u043d\u0430 JavaScript. \u041a\u043b\u0438\u043a\u043d\u0438\u0442\u0435 \u043d\u0430 \u044d\u0442\u043e\u043c \u0432\u0441\u043f\u043b\u044b\u0432\u0430\u044e\u0449\u0435\u043c \u043e\u043a\u043e\u0448\u043a\u0435 \u0434\u043b\u044f \u0435\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f. \u0410\u0432\u0442\u043e\u0437\u0430\u043a\u0440\u044b\u0442\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 ",
+ sec: "\u0441\u0435\u043a.",
+ },
+ en: {
+ hint: 'This file looks like a JavaScript. Click this popup to format it. It will close in ',
+ sec: 'sec.',
+ }
+ });
+
+ var cssinfo = '/** CSS for bar mode */\
#jsb4c-bar {\
display: none;\
height: 22px;\
@@ -62,33 +68,34 @@ var cssinfo = '/** CSS for bar mode */\
z-index: 21475;\
}';
-var csshighlight = '/* Used with pretty_js.js */\
- .str { color: blue4; }\
- .kwd { color: #069; }\
- .com { color: #008200; }\
- .typ { color: #606; }\
- .lit { color: #066; }\
- .pun { color: #660; }\
- .pln { color: #000; }\
- .tag { color: #008; }\
- .atn { color: #606; }\
- .atv { color: #080; }\
- .dec { color: #606; }\
+ var csshighlight = '/* Used with pretty_js.js */\
+.pln { color: #000; } /* plain text */\
+.str { color: blue4; } /* string content */\
+.kwd { color: #069; } /* a keyword */\
+.com { color: #008200; } /* a comment */\
+.typ { color: #606; } /* a type name */\
+.lit { color: #066; } /* a literal value */\
+.pun, .opn, .clo { color: #660; } /* punctuation, lisp open bracket, lisp close bracket */\
+.tag { color: #008; } /* a markup tag name */\
+.atn { color: #606; } /* a markup attribute name */\
+.atv { color: #080; } /* a markup attribute value */\
+.dec, .var{ color: #606; } /* a declaration; a variable name */\
+.fun { color: green } /* a function name */\
/* Specify class=linenums on a pre to get line numbering */\
- li.L0,\
- li.L1,\
- li.L2,\
- li.L3,\
- li.L5,\
- li.L6,\
- li.L7,\
- li.L8 { list-style-type: none }\
+li.L0,\
+li.L1,\
+li.L2,\
+li.L3,\
+li.L5,\
+li.L6,\
+li.L7,\
+li.L8 { list-style-type: none }\
/* Alternate shading for lines */\
- li.L1,\
- li.L3,\
- li.L5,\
- li.L7,\
- li.L9 { background: #eee }\
+li.L1,\
+li.L3,\
+li.L5,\
+li.L7,\
+li.L9 { background: #eee }\
@media print {\
.str { color: #060; }\
.kwd { color: #006; font-weight: bold; }\
@@ -102,99 +109,114 @@ var csshighlight = '/* Used with pretty_js.js */\
.atv { color: #060; }\
}';
-////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+ ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
-log('[ReadableJS]: extension Readable JavaScript started.');
+ log('extension started...');
-if (s2b(storage['autoparse_js'])) { parseJavaScript(); return; }
+ if (s2b(storage['autoparse_js'])) {
+ parseJavaScript();
+ return;
+ }
-var notification = document.createElement('div'),
- close = document.createElement('a'),
- autoHideInterval, secondsTillHide = 12,
- formatConfirmElement;
+ var notification = document.createElement('div'),
+ close = document.createElement('a'),
+ autoHideInterval, secondsTillHide = 12,
+ formatConfirmElement;
-var css = document.createElement('style');
-css.type = 'text/css';
-css.appendChild(document.createTextNode(cssinfo));
+ var css = document.createElement('style');
+ css.type = 'text/css';
+ css.appendChild(document.createTextNode(cssinfo));
-(document.getElementsByTagName('head')[0] || document.documentElement).appendChild(css);
+ (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(css);
-notification.id = 'jsb4c-bar';
-close.id = 'jsb4c-close';
+ notification.id = 'jsb4c-bar';
+ close.id = 'jsb4c-close';
-notification.appendChild(document.createTextNode(JSREADABLE_STRINGS.hint));
-var timer = document.createElement('span');
-timer.id = 'jsb4c-timer';
-notification.appendChild(timer);
-notification.appendChild(document.createTextNode(JSREADABLE_STRINGS.sec));
+ notification.appendChild(document.createTextNode(READABLEJS_STRINGS.hint));
+ var timer = document.createElement('span');
+ timer.id = 'jsb4c-timer';
+ notification.appendChild(timer);
+ notification.appendChild(document.createTextNode(READABLEJS_STRINGS.sec));
-close.style.backgroundImage = 'url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAgAAAAICAMAAADz0U65AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0W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vYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6MzBFNjRDRUY0QjZFMTFFMDlBQjJGRTYzRjk5RjJFNDEiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6MzBFNjRDRjA0QjZFMTFFMDlBQjJGRTYzRjk5RjJFNDEiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDozMEU2NENFRDRCNkUxMUUwOUFCMkZFNjNGOTlGMkU0MSIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDozMEU2NENFRTRCNkUxMUUwOUFCMkZFNjNGOTlGMkU0MSIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PvBP+boAAACEUExURZ2dnfXmr6yrpLWzrfbptbi2r/7wtvPlsPTmtf7us6uqo/Tntv3usvXnsKGgmv7vtqGgmbWzrPPmsf3vs7m2sKuqpK+sn/fptfTms/fqtqyqpP7vs42Njbe1rvPls/3tr/XnsaGhofXotL27s7Sxorq2p6+vr/Xos7CvqPbnrrCtn6ysrEXyoSsAAABLSURBVHjaDMXFAYAwEACww90d6qW+/36QT+Dy5ZA3/gCPRkF6tMO5cWt5TWBJcQhYPNCaVevMVDDHQcpwF/DCxFjy51RHaaTcJ8AA5rIGa1D6WN8AAAAASUVORK5CYII=)';
-notification.appendChild(close);
-document.body.appendChild(notification);
+ notification.appendChild(close);
+ document.body.appendChild(notification);
-close.addEventListener('click', function (event) {
- hideNotification();
- event.preventDefault();
- event.stopPropagation();
-}, false);
+ close.addEventListener('click', function (event) {
+ hideNotification();
+ event.preventDefault();
+ event.stopPropagation();
+ }, false);
-notification.addEventListener('click', function (event) {
- hideNotification();
- event.preventDefault();
- event.stopPropagation();
+ notification.addEventListener('click', function (event) {
+ hideNotification();
+ event.preventDefault();
+ event.stopPropagation();
- parseJavaScript();
-}, false);
+ parseJavaScript();
+ }, false);
-function parseJavaScript() {
- log('[ReadableJS]: JavaScript formatting options:\n indent_size = ' + Number(storage['indent_size'], 10) + '\n indent_char = "' + storage['indent_char'] + '"' + '\n preserve_newlines = ' + s2b(storage['preserve_newlines']) + '\n braces_on_own_line = ' + s2b(storage['braces_on_own_line']) + '\n keep_array_indentation = ' + s2b(storage['keep_array_indentation']));
-
- var code = document.getElementsByTagName('PRE')[0];
- setTimeout(function () {
- log('[ReadableJS]: Formatting JavaScript...');
- code.textContent = js_beautify((s2b(storage['detect_packers']) ? unpacker_filter(code.textContent) : code.textContent), {
- indent_size: parseInt(storage['indent_size'], 10),
- indent_char: storage['indent_char'],
- preserve_newlines: s2b(storage['preserve_newlines']),
- braces_on_own_line: s2b(storage['braces_on_own_line']),
- keep_array_indentation: s2b(storage['keep_array_indentation']),
- space_after_anon_function: true
- });
- }, s2b(storage['autoparse_js']) ? 0 : 300);
-
- if (s2b(storage['enable_coloring'])) {
- document.getElementsByTagName('PRE')[0].className = 'prettyprint';
- document.getElementsByTagName('PRE')[0].id = 'javascript'
-
- var css1 = document.createElement('style');
- css1.type = 'text/css';
- css1.appendChild(document.createTextNode(csshighlight));
- (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(css1);
+ function parseJavaScript() {
+ log('formatting options:\n indent_size = ' + Number(storage['indent_size'], 10) + '\n indent_char = "' + storage['indent_char'] + '"' + '\n preserve_newlines = ' + s2b(storage['preserve_newlines']) + '\n brace_style = ' + (s2b(storage['braces_on_own_line']) ? 'expand' : 'collapse') + '\n keep_array_indentation = ' + s2b(storage['keep_array_indentation']));
+ var code = document.getElementsByTagName('PRE')[0];
setTimeout(function () {
- log('[ReadableJS]: Coloring JavaScript...');
- init_pretty_print();
- window.prettyPrint();
- }, s2b(storage['autoparse_js']) ? 100 : 400);
+ log('formatting JavaScript...');
+ code.textContent = js_beautify((s2b(storage['detect_packers']) ? unpacker_filter(code.textContent) : code.textContent), {
+ indent_size: parseInt(storage['indent_size'], 10),
+ indent_char: storage['indent_char'],
+ preserve_newlines: s2b(storage['preserve_newlines']),
+ brace_style: s2b(storage['braces_on_own_line']) ? 'expand' : 'collapse', //storage['brace_style'], //"collapse"|"expand"|"end-expand"|"expand-strict"
+ keep_array_indentation: s2b(storage['keep_array_indentation']),
+ space_before_conditional: true, //storage['space_before_conditional'],
+ space_after_anon_function: true,
+ });
+ }, s2b(storage['autoparse_js']) ? 0 : 300);
+
+ if (s2b(storage['enable_coloring'])) {
+ code.textContent = code.textContent.escapeHTMLforJS();
+
+ document.getElementsByTagName('PRE')[0].className = 'prettyprint lang-javascript';
+ document.getElementsByTagName('PRE')[0].id = 'javascript'
+
+ var css1 = document.createElement('style');
+ css1.type = 'text/css';
+ css1.appendChild(document.createTextNode(csshighlight));
+ (document.getElementsByTagName('head')[0] || document.documentElement).appendChild(css1);
+
+ setTimeout(function () {
+ log('coloring JavaScript...');
+ init_pretty_print();
+ window.prettyPrint(false);
+ }, s2b(storage['autoparse_js']) ? 100 : 400);
+ }
}
-}
-
-function hideNotification() {
- notification.className = 'hidden';
- setTimeout(function () { notification.parentNode.removeChild(notification); }, 1000);
-}
-
-setTimeout(function () { notification.className = 'visible'; }, 350);
-
-// Autotimer to make infobar go away
-document.querySelector('#jsb4c-timer').innerHTML = secondsTillHide;
-autoHideInterval = setInterval(function () {
- if (secondsTillHide <= 0) { hideNotification(); clearInterval(autoHideInterval); }
- else {
- if (!document.querySelector('#jsb4c-timer')) { clearInterval(autoHideInterval); return; }
- document.querySelector('#jsb4c-timer').innerHTML = secondsTillHide;
- secondsTillHide--;
+
+ function hideNotification() {
+ notification.className = 'hidden';
+ setTimeout(function () {
+ notification.parentNode.removeChild(notification);
+ }, 1000);
}
-}, 1000);
+
+ setTimeout(function () {
+ notification.className = 'visible';
+ }, 350);
+
+ // Autotimer to make infobar go away
+ document.querySelector('#jsb4c-timer').innerHTML = secondsTillHide;
+ autoHideInterval = setInterval(function () {
+ if (secondsTillHide <= 0) {
+ hideNotification();
+ clearInterval(autoHideInterval);
+ } else {
+ if (!document.querySelector('#jsb4c-timer')) {
+ clearInterval(autoHideInterval);
+ return;
+ }
+ document.querySelector('#jsb4c-timer').innerHTML = secondsTillHide;
+ secondsTillHide--;
+ }
+ }, 1000);
}());
@@ -0,0 +1,29 @@
+(function () {
+ // Opera specific
+ function selText(w) { var t; return w ? w.document.getSelection() || (t = w.opera.lastClick && w.opera.lastClick.textArea) && t.value.substring(t.selectionStart, t.selectionEnd) : '' };
+
+ var background;
+ opera.extension.onmessage = function( event ){
+ if(event.data == 'send_bg_port' ){
+ background = event.source; // in case of need to send anything to background
+ var channel = new MessageChannel();
+ event.ports[0].postMessage('send_tab_port', [channel.port2] );
+ channel.port1.onmessage = onPopupMessageHandler;
+ }
+ }
+
+ function onPopupMessageHandler(event){
+ switch (event.data) {
+ case 'get-selection':
+ var seltext = selText(window);
+ if (seltext) {
+ event.source.postMessage({type:'give-selection', text: seltext});
+ }
+ //else if (~window.location.href.indexOf('javascript:')) {
+ // event.source.postMessage({type:'give-selection', text: window.location.href});
+ //}
+ break;
+ default:
+ }
+ }
+}());
Oops, something went wrong.

0 comments on commit a08a130

Please sign in to comment.