2014-09-21 02:29:33 +02:00
/ *
* options / user - js . js - allow user enter custom javascripts
*
* Copyright ( c ) 2014 Marcin Łabanowski < marcin @ 6 irc . 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" ) ) ;
2015-03-11 11:01:59 +01:00
$ ( "<h3 style='margin:0'>" + _ ( "Do not paste code here unless you absolutely trust the source or have read it yourself!" ) + "</h3><span class='unimportant'>" + _ ( "Untrusted code pasted here could do malicious things such as spam the site under your IP." ) + "</span>" ) . appendTo ( tab . content ) ;
2014-09-21 02:29:33 +02:00
var textarea = $ ( "<textarea></textarea>" ) . css ( {
2015-03-11 11:01:59 +01:00
"height" : "74%" ,
2015-02-14 01:01:23 +01:00
"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-03-11 11:01:59 +01:00
textarea . text ( "/* " + _ ( "Enter your own Javascript code here..." ) + " */\n" +
2015-02-26 04:51:45 +01:00
"/* " + _ ( "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.
}
} ( ) ;