2014-09-21 02:29:33 +02:00
|
|
|
/*
|
|
|
|
* options/user-js.js - allow user enter custom javascripts
|
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Marcin Łabanowski <marcin@6irc.net>
|
|
|
|
*
|
|
|
|
* Usage:
|
|
|
|
* $config['additional_javascript'][] = 'js/jquery.min.js';
|
|
|
|
* $config['additional_javascript'][] = 'js/options.js';
|
|
|
|
* $config['additional_javascript'][] = 'js/options/user-js.js';
|
|
|
|
*/
|
|
|
|
|
|
|
|
+function(){
|
|
|
|
|
|
|
|
var tab = Options.add_tab("user-js", "code", _("User JS"));
|
|
|
|
|
|
|
|
var textarea = $("<textarea></textarea>").css({
|
2015-02-14 01:01:23 +01:00
|
|
|
"height" : "85%",
|
|
|
|
"width" : "100%",
|
|
|
|
"font-size" : "9pt",
|
|
|
|
"font-family": "monospace",
|
2014-09-21 02:29:33 +02:00
|
|
|
}).appendTo(tab.content);
|
2015-02-14 01:07:38 +01:00
|
|
|
var submit = $("<input type='button' value='"+_("Save custom Javascript")+"'>").css({
|
2015-02-14 01:01:23 +01:00
|
|
|
"width": "100%",
|
2014-09-21 02:29:33 +02:00
|
|
|
}).click(function() {
|
|
|
|
localStorage.user_js = textarea.val();
|
|
|
|
document.location.reload();
|
|
|
|
}).appendTo(tab.content);
|
|
|
|
|
|
|
|
var apply_js = function() {
|
|
|
|
var proc = function() {
|
|
|
|
$('.user-js').remove();
|
|
|
|
$('script')
|
|
|
|
.last()
|
|
|
|
.after($("<script></script>")
|
|
|
|
.addClass("user-js")
|
|
|
|
.text(localStorage.user_js)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (/immediate()/.test(localStorage.user_js)) {
|
|
|
|
proc(); // Apply the script immediately
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$(proc); // Apply the script when the page fully loads
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var update_textarea = function() {
|
|
|
|
if (!localStorage.user_js) {
|
2015-02-14 01:06:18 +01:00
|
|
|
textarea.text("/* "+_("You can include JS files from remote servers, for example:")+" */\n" +
|
2014-11-02 00:06:49 +01:00
|
|
|
'/* load_js("http://example.com/script.js"); */');
|
2014-09-21 02:29:33 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
textarea.text(localStorage.user_js);
|
|
|
|
apply_js();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
update_textarea();
|
|
|
|
|
|
|
|
|
|
|
|
// User utility functions
|
|
|
|
window.load_js = function(url) {
|
|
|
|
$('script')
|
|
|
|
.last()
|
|
|
|
.after($("<script></script>")
|
|
|
|
.prop("type", "text/javascript")
|
|
|
|
.prop("src", url)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
window.immediate = function() { // A dummy function.
|
|
|
|
}
|
|
|
|
|
|
|
|
}();
|