]>
git.wh0rd.org - tt-rss.git/blob - js/functions.js
1 var loading_progress
= 0 ;
2 var sanity_check_done
= false ;
4 var _label_base_index
= - 1024 ;
5 var notify_hide_timerid
= false ;
7 Ajax
. Base
. prototype . initialize
= Ajax
. Base
. prototype . initialize
. wrap (
8 function ( callOriginal
, options
) {
10 if ( getInitParam ( "csrf_token" ) != undefined ) {
11 Object
. extend ( options
, options
|| { });
13 if ( Object
. isString ( options
. parameters
))
14 options
. parameters
= options
. parameters
. toQueryParams ();
15 else if ( Object
. isHash ( options
. parameters
))
16 options
. parameters
= options
. parameters
. toObject ();
18 options
. parameters
[ "csrf_token" ] = getInitParam ( "csrf_token" );
21 return callOriginal ( options
);
25 /* add method to remove element from array */
27 Array
. prototype . remove = function ( s
) {
28 for ( var i
= 0 ; i
< this . length
; i
++) {
29 if ( s
== this [ i
]) this . splice ( i
, 1 );
33 /* create console.log if it doesn't exist */
35 if (! window
. console
) console
= {};
36 console
. log
= console
. log
|| function ( msg
) { };
37 console
. warn
= console
. warn
|| function ( msg
) { };
38 console
. error
= console
. error
|| function ( msg
) { };
40 function exception_error ( location
, e
, ext_info
) {
41 var msg
= format_exception_error ( location
, e
);
43 if (! ext_info
) ext_info
= false ;
48 ext_info
= JSON
. stringify ( ext_info
);
51 new Ajax
. Request ( "backend.php" , {
52 parameters
: { op
: "rpc" , method
: "log" , logmsg
: msg
},
53 onComplete : function ( transport
) {
54 console
. log ( transport
. responseText
);
58 console
. log ( "Exception while trying to log the error." );
62 msg
+= "<p>" + __ ( "The error will be reported to the configured log destination." ) +
65 var content
= "<div class= \" fatalError \" >" +
66 "<pre>" + msg
+ "</pre>" ;
68 content
+= "<form name= \" exceptionForm \" id= \" exceptionForm \" target= \" _blank \" " +
69 "action= \" http://tt-rss.org/report.php \" method= \" POST \" >" ;
71 content
+= "<textarea style= \" display : none \" name= \" message \" >" + msg
+ "</textarea>" ;
72 content
+= "<textarea style= \" display : none \" name= \" params \" >N/A</textarea>" ;
75 content
+= "<div><b>Additional information:</b></div>" +
76 "<textarea name= \" xinfo \" readonly= \" 1 \" >" + ext_info
+ "</textarea>" ;
79 content
+= "<div><b>Stack trace:</b></div>" +
80 "<textarea name= \" stack \" readonly= \" 1 \" >" + e
. stack
+ "</textarea>" ;
86 content
+= "<div class='dlgButtons'>" ;
88 content
+= "<button dojoType= \" dijit.form.Button \" " +
89 "onclick= \" dijit.byId('exceptionDlg').report() \" >" +
90 __ ( 'Report to tt-rss.org' ) + "</button> " ;
91 content
+= "<button dojoType= \" dijit.form.Button \" " +
92 "onclick= \" dijit.byId('exceptionDlg').hide() \" >" +
93 __ ( 'Close' ) + "</button>" ;
96 if ( dijit
. byId ( "exceptionDlg" ))
97 dijit
. byId ( "exceptionDlg" ). destroyRecursive ();
99 var dialog
= new dijit
. Dialog ({
101 title
: "Unhandled exception" ,
102 style
: "width: 600px" ,
104 if ( confirm ( __ ( "Are you sure to report this exception to tt-rss.org? The report will include information about your web browser and tt-rss configuration. Your IP will be saved in the database." ))) {
106 document
. forms
[ 'exceptionForm' ]. params
. value
= $ H ({
107 browserName
: navigator
. appName
,
108 browserVersion
: navigator
. appVersion
,
109 browserPlatform
: navigator
. platform
,
110 browserCookies
: navigator
. cookieEnabled
,
111 ttrssVersion
: __ttrss_version
,
112 initParams
: JSON
. stringify ( init_params
),
115 document
. forms
[ 'exceptionForm' ]. submit ();
124 console
. log ( "Exception while trying to report an exception. Oh boy." );
126 console
. log ( "Original exception:" );
129 msg
+= " \n\n Additional exception caught while trying to show the error dialog. \n\n " + format_exception_error ( 'exception_error' , ei
);
132 new Ajax
. Request ( "backend.php" , {
133 parameters
: { op
: "rpc" , method
: "log" , logmsg
: msg
},
134 onComplete : function ( transport
) {
135 console
. log ( transport
. responseText
);
139 console
. log ( "Third exception while trying to log the error! Seriously?" );
143 msg
+= " \n\n The error will be reported to the configured log destination." ;
150 function format_exception_error ( location
, e
) {
154 var base_fname
= e
. fileName
. substring ( e
. fileName
. lastIndexOf ( "/" ) + 1 );
156 msg
= "Exception: " + e
. name
+ ", " + e
. message
+
157 " \n Function: " + location
+ "()" +
158 " \n Location: " + base_fname
+ ":" + e
. lineNumber
;
160 } else if ( e
. description
) {
161 msg
= "Exception: " + e
. description
+ " \n Function: " + location
+ "()" ;
163 msg
= "Exception: " + e
+ " \n Function: " + location
+ "()" ;
166 console
. error ( "EXCEPTION: " + msg
);
171 function param_escape ( arg
) {
172 if ( typeof encodeURIComponent
!= 'undefined' )
173 return encodeURIComponent ( arg
);
178 function param_unescape ( arg
) {
179 if ( typeof decodeURIComponent
!= 'undefined' )
180 return decodeURIComponent ( arg
);
182 return unescape ( arg
);
185 function notify_real ( msg
, no_hide
, n_type
) {
191 if ( notify_hide_timerid
) {
192 window
. clearTimeout ( notify_hide_timerid
);
196 if ( n
. hasClassName ( "visible" )) {
197 notify_hide_timerid
= window
. setTimeout ( function () {
198 n
. removeClassName ( "visible" ) }, 0 );
212 msg
= "<span class= \" msg \" > " + __ ( msg
) + "</span>" ;
215 n
. className
= "notify notify_progress visible" ;
216 msg
= "<span><img src='images/indicator_white.gif'></span>" + msg
;
218 } else if ( n_type
== 3 ) {
219 n
. className
= "notify notify_error visible" ;
220 msg
= "<span><img src='images/alert.png'></span>" + msg
;
221 } else if ( n_type
== 4 ) {
222 n
. className
= "notify notify_info visible" ;
223 msg
= "<span><img src='images/information.png'></span>" + msg
;
225 n
. className
= "notify visible" ;
228 msg
+= " <span><img src= \" images/cross.png \" class= \" close \" title= \" " +
229 __ ( "Click to close" ) + " \" onclick= \" notify('') \" ></span>" ;
231 // msg = "<img src='images/live_com_loading.gif'> " + msg;
236 notify_hide_timerid
= window
. setTimeout ( function () {
237 n
. removeClassName ( "visible" ) }, 5 * 1000 );
241 function notify ( msg
, no_hide
) {
242 notify_real ( msg
, no_hide
, 1 );
245 function notify_progress ( msg
, no_hide
) {
246 notify_real ( msg
, no_hide
, 2 );
249 function notify_error ( msg
, no_hide
) {
250 notify_real ( msg
, no_hide
, 3 );
254 function notify_info ( msg
, no_hide
) {
255 notify_real ( msg
, no_hide
, 4 );
258 function setCookie ( name
, value
, lifetime
, path
, domain
, secure
) {
264 d
. setTime ( d
. getTime () + ( lifetime
* 1000 ));
267 console
. log ( "setCookie: " + name
+ " => " + value
+ ": " + d
);
269 int_setCookie ( name
, value
, d
, path
, domain
, secure
);
273 function int_setCookie ( name
, value
, expires
, path
, domain
, secure
) {
274 document
. cookie
= name
+ "=" + escape ( value
) +
275 (( expires
) ? "; expires=" + expires
. toGMTString () : "" ) +
276 (( path
) ? "; path=" + path
: "" ) +
277 (( domain
) ? "; domain=" + domain
: "" ) +
278 (( secure
) ? "; secure" : "" );
281 function delCookie ( name
, path
, domain
) {
282 if ( getCookie ( name
)) {
283 document
. cookie
= name
+ "=" +
284 (( path
) ? ";path=" + path
: "" ) +
285 (( domain
) ? ";domain=" + domain
: "" ) +
286 ";expires=Thu, 01-Jan-1970 00:00:01 GMT" ;
291 function getCookie ( name
) {
293 var dc
= document
. cookie
;
294 var prefix
= name
+ "=" ;
295 var begin
= dc
. indexOf ( "; " + prefix
);
297 begin
= dc
. indexOf ( prefix
);
298 if ( begin
!= 0 ) return null ;
303 var end
= document
. cookie
. indexOf ( ";" , begin
);
307 return unescape ( dc
. substring ( begin
+ prefix
. length
, end
));
310 function gotoPreferences () {
311 document
. location
. href
= "prefs.php" ;
314 function gotoLogout () {
315 document
. location
. href
= "backend.php?op=logout" ;
318 function gotoMain () {
319 document
. location
. href
= "index.php" ;
322 /** * @(#)isNumeric.js * * Copyright (c) 2000 by Sundar Dorai-Raj
323 * * @author Sundar Dorai-Raj
324 * * Email: sdoraira@vt.edu
325 * * This program is free software; you can redistribute it and/or
326 * * modify it under the terms of the GNU General Public License
327 * * as published by the Free Software Foundation; either version 2
328 * * of the License, or (at your option) any later version,
329 * * provided that any use properly credits the author.
330 * * This program is distributed in the hope that it will be useful,
331 * * but WITHOUT ANY WARRANTY; without even the implied warranty of
332 * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
333 * * GNU General Public License for more details at http://www.gnu.org * * */
335 var numbers
= ".0123456789" ;
336 function isNumeric ( x
) {
337 // is x a String or a character?
339 // remove negative sign
341 for ( var j
= 0 ; j
< x
. length
; j
++) {
342 // call isNumeric recursively for each character
343 number
= isNumeric ( x
. substring ( j
, j
+ 1 ));
344 if (! number
) return number
;
349 // if x is number return true
350 if ( numbers
. indexOf ( x
)>= 0 ) return true ;
356 function toggleSelectRowById ( sender
, id
) {
358 return toggleSelectRow ( sender
, row
);
361 function toggleSelectListRow ( sender
) {
362 var row
= sender
. parentNode
;
363 return toggleSelectRow ( sender
, row
);
366 /* this is for dijit Checkbox */
367 function toggleSelectListRow2 ( sender
) {
368 var row
= sender
. domNode
. parentNode
;
369 return toggleSelectRow ( sender
, row
);
372 /* this is for dijit Checkbox */
373 function toggleSelectRow2 ( sender
, row
, is_cdm
) {
377 row
= sender
. domNode
. parentNode
. parentNode
;
379 row
= sender
. domNode
. parentNode
. parentNode
. parentNode
; // oh ffs
381 if ( sender
. checked
&& ! row
. hasClassName ( 'Selected' ))
382 row
. addClassName ( 'Selected' );
384 row
. removeClassName ( 'Selected' );
386 if ( typeof updateSelectedPrompt
!= undefined )
387 updateSelectedPrompt ();
391 function toggleSelectRow ( sender
, row
) {
393 if (! row
) row
= sender
. parentNode
. parentNode
;
395 if ( sender
. checked
&& ! row
. hasClassName ( 'Selected' ))
396 row
. addClassName ( 'Selected' );
398 row
. removeClassName ( 'Selected' );
400 if ( typeof updateSelectedPrompt
!= undefined )
401 updateSelectedPrompt ();
404 function checkboxToggleElement ( elem
, id
) {
406 Effect
. Appear ( id
, { duration
: 0.5 });
408 Effect
. Fade ( id
, { duration
: 0.5 });
412 function dropboxSelect ( e
, v
) {
413 for ( var i
= 0 ; i
< e
. length
; i
++) {
414 if ( e
[ i
]. value
== v
) {
421 function getURLParam ( param
){
422 return String ( window
. location
. href
). parseQuery ()[ param
];
425 function closeInfoBox ( cleanup
) {
427 dialog
= dijit
. byId ( "infoBox" );
429 if ( dialog
) dialog
. hide ();
432 //exception_error("closeInfoBox", e);
438 function displayDlg ( title
, id
, param
, callback
) {
440 notify_progress ( "Loading, please wait..." , true );
442 var query
= "?op=dlg&method=" +
443 param_escape ( id
) + "¶m=" + param_escape ( param
);
445 new Ajax
. Request ( "backend.php" , {
447 onComplete : function ( transport
) {
448 infobox_callback2 ( transport
, title
);
449 if ( callback
) callback ( transport
);
455 function infobox_callback2 ( transport
, title
) {
459 if ( dijit
. byId ( "infoBox" )) {
460 dialog
= dijit
. byId ( "infoBox" );
463 //console.log("infobox_callback2");
466 var content
= transport
. responseText
;
469 dialog
= new dijit
. Dialog ({
472 style
: "width: 600px" ,
473 onCancel : function () {
476 onExecute : function () {
479 onClose : function () {
484 dialog
. attr ( 'title' , title
);
485 dialog
. attr ( 'content' , content
);
492 exception_error ( "infobox_callback2" , e
);
496 function filterCR ( e
, f
)
501 key
= window
. event
. keyCode
; //IE
503 key
= e
. which
; //firefox
506 if ( typeof f
!= 'undefined' ) {
517 function getInitParam ( key
) {
518 return init_params
[ key
];
521 function setInitParam ( key
, value
) {
522 init_params
[ key
] = value
;
525 function fatalError ( code
, msg
, ext_info
) {
529 window
. location
. href
= "index.php" ;
530 } else if ( code
== 5 ) {
531 window
. location
. href
= "public.php?op=dbupdate" ;
534 if ( msg
== "" ) msg
= "Unknown error" ;
537 if ( ext_info
. responseText
) {
538 ext_info
= ext_info
. responseText
;
542 if ( ERRORS
&& ERRORS
[ code
] && ! msg
) {
546 var content
= "<div><b>Error code:</b> " + code
+ "</div>" +
547 "<p>" + msg
+ "</p>" ;
550 content
= content
+ "<div><b>Additional information:</b></div>" +
551 "<textarea style='width: 100%' readonly= \" 1 \" >" +
552 ext_info
+ "</textarea>" ;
555 var dialog
= new dijit
. Dialog ({
556 title
: "Fatal error" ,
557 style
: "width: 600px" ,
567 exception_error ( "fatalError" , e
);
571 function filterDlgCheckAction ( sender
) {
575 var action
= sender
. value
;
577 var action_param
= $( "filterDlg_paramBox" );
580 console
. log ( "filterDlgCheckAction: can't find action param box!" );
584 // if selected action supports parameters, enable params field
585 if ( action
== 4 || action
== 6 || action
== 7 ) {
586 new Effect
. Appear ( action_param
, { duration
: 0.5 });
588 Element
. show ( dijit
. byId ( "filterDlg_actionParam" ). domNode
);
589 Element
. hide ( dijit
. byId ( "filterDlg_actionParamLabel" ). domNode
);
591 Element
. show ( dijit
. byId ( "filterDlg_actionParamLabel" ). domNode
);
592 Element
. hide ( dijit
. byId ( "filterDlg_actionParam" ). domNode
);
595 Element
. hide ( action_param
);
599 exception_error ( "filterDlgCheckAction" , e
);
605 function explainError ( code
) {
606 return displayDlg ( __ ( "Error explained" ), "explainError" , code
);
609 function loading_set_progress ( p
) {
611 loading_progress
+= p
;
613 if ( dijit
. byId ( "loading_bar" ))
614 dijit
. byId ( "loading_bar" ). update ({ progress
: loading_progress
});
616 if ( loading_progress
>= 90 )
620 exception_error ( "loading_set_progress" , e
);
624 function remove_splash () {
626 if ( Element
. visible ( "overlay" )) {
627 console
. log ( "about to remove splash, OMG!" );
628 Element
. hide ( "overlay" );
629 console
. log ( "removed splash!" );
633 function transport_error_check ( transport
) {
635 if ( transport
. responseXML
) {
636 var error
= transport
. responseXML
. getElementsByTagName ( "error" )[ 0 ];
639 var code
= error
. getAttribute ( "error-code" );
640 var msg
= error
. getAttribute ( "error-msg" );
642 fatalError ( code
, msg
);
648 exception_error ( "check_for_error_xml" , e
);
653 function strip_tags ( s
) {
654 return s
. replace ( /<\/?[^>]+(>|$)/g , "" );
657 function truncate_string ( s
, length
) {
658 if (! length
) length
= 30 ;
659 var tmp
= s
. substring ( 0 , length
);
660 if ( s
. length
> length
) tmp
+= "…" ;
664 function hotkey_prefix_timeout () {
667 var date
= new Date ();
668 var ts
= Math
. round ( date
. getTime () / 1000 );
670 if ( hotkey_prefix_pressed
&& ts
- hotkey_prefix_pressed
>= 5 ) {
671 console
. log ( "hotkey_prefix seems to be stuck, aborting" );
672 hotkey_prefix_pressed
= false ;
673 hotkey_prefix
= false ;
674 Element
. hide ( 'cmdline' );
677 setTimeout ( "hotkey_prefix_timeout()" , 1000 );
680 exception_error ( "hotkey_prefix_timeout" , e
);
684 function uploadIconHandler ( rc
) {
688 notify_info ( "Upload complete." );
689 if ( inPreferences ()) {
692 setTimeout ( 'updateFeedList(false, false)' , 50 );
696 notify_error ( "Upload failed: icon is too big." );
699 notify_error ( "Upload failed." );
704 exception_error ( "uploadIconHandler" , e
);
708 function removeFeedIcon ( id
) {
712 if ( confirm ( __ ( "Remove stored feed icon?" ))) {
713 var query
= "backend.php?op=pref-feeds&method=removeicon&feed_id=" + param_escape ( id
);
717 notify_progress ( "Removing feed icon..." , true );
719 new Ajax
. Request ( "backend.php" , {
721 onComplete : function ( transport
) {
722 notify_info ( "Feed icon removed." );
723 if ( inPreferences ()) {
726 setTimeout ( 'updateFeedList(false, false)' , 50 );
733 exception_error ( "removeFeedIcon" , e
);
737 function uploadFeedIcon () {
741 var file
= $( "icon_file" );
743 if ( file
. value
. length
== 0 ) {
744 alert ( __ ( "Please select an image file to upload." ));
746 if ( confirm ( __ ( "Upload new icon for this feed?" ))) {
747 notify_progress ( "Uploading, please wait..." , true );
755 exception_error ( "uploadFeedIcon" , e
);
759 function addLabel ( select
, callback
) {
763 var caption
= prompt ( __ ( "Please enter label caption:" ), "" );
765 if ( caption
!= undefined ) {
768 alert ( __ ( "Can't create label: missing caption." ));
772 var query
= "?op=pref-labels&method=add&caption=" +
773 param_escape ( caption
);
776 query
+= "&output=select" ;
778 notify_progress ( "Loading, please wait..." , true );
780 if ( inPreferences () && ! select
) active_tab
= "labelConfig" ;
782 new Ajax
. Request ( "backend.php" , {
784 onComplete : function ( transport
) {
787 } else if ( inPreferences ()) {
797 exception_error ( "addLabel" , e
);
801 function quickAddFeed () {
803 var query
= "backend.php?op=feeds&method=quickAddFeed" ;
805 // overlapping widgets
806 if ( dijit
. byId ( "batchSubDlg" )) dijit
. byId ( "batchSubDlg" ). destroyRecursive ();
807 if ( dijit
. byId ( "feedAddDlg" )) dijit
. byId ( "feedAddDlg" ). destroyRecursive ();
809 var dialog
= new dijit
. Dialog ({
811 title
: __ ( "Subscribe to Feed" ),
812 style
: "width: 600px" ,
813 execute : function () {
814 if ( this . validate ()) {
815 console
. log ( dojo
. objectToQuery ( this . attr ( 'value' )));
817 var feed_url
= this . attr ( 'value' ). feed
;
819 Element
. show ( "feed_add_spinner" );
821 new Ajax
. Request ( "backend.php" , {
822 parameters
: dojo
. objectToQuery ( this . attr ( 'value' )),
823 onComplete : function ( transport
) {
827 var reply
= JSON
. parse ( transport
. responseText
);
829 Element
. hide ( "feed_add_spinner" );
830 alert ( __ ( "Failed to parse output. This can indicate server timeout and/or network issues. Backend output was logged to browser console." ));
831 console
. log ( 'quickAddFeed, backend returned:' + transport
. responseText
);
835 var rc
= reply
[ 'result' ];
838 Element
. hide ( "feed_add_spinner" );
842 switch ( parseInt ( rc
[ 'code' ])) {
845 notify_info ( __ ( "Subscribed to %s" ). replace ( "%s" , feed_url
));
850 alert ( __ ( "Specified URL seems to be invalid." ));
853 alert ( __ ( "Specified URL doesn't seem to contain any feeds." ));
858 Element
. show ( "fadd_multiple_notify" );
860 var select
= dijit
. byId ( "feedDlg_feedContainerSelect" );
862 while ( select
. getOptions (). length
> 0 )
863 select
. removeOption ( 0 );
865 select
. addOption ({ value
: '' , label
: __ ( "Expand to select feed" )});
868 for ( var feedUrl
in feeds
) {
869 select
. addOption ({ value
: feedUrl
, label
: feeds
[ feedUrl
]});
873 Effect
. Appear ( 'feedDlg_feedsContainer' , { duration
: 0.5 });
877 alert ( __ ( "Couldn't download the specified URL: %s" ).
878 replace ( "%s" , rc
[ 'message' ]));
881 alert ( __ ( "XML validation failed: %s" ).
882 replace ( "%s" , rc
[ 'message' ]));
886 alert ( __ ( "You are already subscribed to this feed." ));
891 exception_error ( "subscribeToFeed" , e
, transport
);
902 exception_error ( "quickAddFeed" , e
);
906 function createNewRuleElement ( parentNode
, replaceNode
) {
908 var form
= document
. forms
[ "filter_new_rule_form" ];
910 form
. reg_exp
. value
= form
. reg_exp
. value
. replace ( /(<([^>]+)>)/ig , "" );
912 var query
= "backend.php?op=pref-filters&method=printrulename&rule=" +
913 param_escape ( dojo
. formToJson ( form
));
917 new Ajax
. Request ( "backend.php" , {
919 onComplete : function ( transport
) {
921 var li
= dojo
. create ( "li" );
923 var cb
= dojo
. create ( "input" , { type
: "checkbox" }, li
);
925 new dijit
. form
. CheckBox ({
926 onChange : function () {
927 toggleSelectListRow2 ( this ) },
930 dojo
. create ( "input" , { type
: "hidden" ,
932 value
: dojo
. formToJson ( form
) }, li
);
934 dojo
. create ( "span" , {
935 onclick : function () {
936 dijit
. byId ( 'filterEditDlg' ). editRule ( this );
938 innerHTML
: transport
. responseText
}, li
);
941 parentNode
. replaceChild ( li
, replaceNode
);
943 parentNode
. appendChild ( li
);
946 exception_error ( "createNewRuleElement" , e
);
950 exception_error ( "createNewRuleElement" , e
);
954 function createNewActionElement ( parentNode
, replaceNode
) {
956 var form
= document
. forms
[ "filter_new_action_form" ];
958 if ( form
. action_id
. value
== 7 ) {
959 form
. action_param
. value
= form
. action_param_label
. value
;
962 var query
= "backend.php?op=pref-filters&method=printactionname&action=" +
963 param_escape ( dojo
. formToJson ( form
));
967 new Ajax
. Request ( "backend.php" , {
969 onComplete : function ( transport
) {
971 var li
= dojo
. create ( "li" );
973 var cb
= dojo
. create ( "input" , { type
: "checkbox" }, li
);
975 new dijit
. form
. CheckBox ({
976 onChange : function () {
977 toggleSelectListRow2 ( this ) },
980 dojo
. create ( "input" , { type
: "hidden" ,
982 value
: dojo
. formToJson ( form
) }, li
);
984 dojo
. create ( "span" , {
985 onclick : function () {
986 dijit
. byId ( 'filterEditDlg' ). editAction ( this );
988 innerHTML
: transport
. responseText
}, li
);
991 parentNode
. replaceChild ( li
, replaceNode
);
993 parentNode
. appendChild ( li
);
997 exception_error ( "createNewActionElement" , e
);
1001 exception_error ( "createNewActionElement" , e
);
1006 function addFilterRule ( replaceNode
, ruleStr
) {
1008 if ( dijit
. byId ( "filterNewRuleDlg" ))
1009 dijit
. byId ( "filterNewRuleDlg" ). destroyRecursive ();
1011 var query
= "backend.php?op=pref-filters&method=newrule&rule=" +
1012 param_escape ( ruleStr
);
1014 var rule_dlg
= new dijit
. Dialog ({
1015 id
: "filterNewRuleDlg" ,
1016 title
: ruleStr
? __ ( "Edit rule" ) : __ ( "Add rule" ),
1017 style
: "width: 600px" ,
1018 execute : function () {
1019 if ( this . validate ()) {
1020 createNewRuleElement ($( "filterDlg_Matches" ), replaceNode
);
1028 exception_error ( "addFilterRule" , e
);
1032 function addFilterAction ( replaceNode
, actionStr
) {
1034 if ( dijit
. byId ( "filterNewActionDlg" ))
1035 dijit
. byId ( "filterNewActionDlg" ). destroyRecursive ();
1037 var query
= "backend.php?op=pref-filters&method=newaction&action=" +
1038 param_escape ( actionStr
);
1040 var rule_dlg
= new dijit
. Dialog ({
1041 id
: "filterNewActionDlg" ,
1042 title
: actionStr
? __ ( "Edit action" ) : __ ( "Add action" ),
1043 style
: "width: 600px" ,
1044 execute : function () {
1045 if ( this . validate ()) {
1046 createNewActionElement ($( "filterDlg_Actions" ), replaceNode
);
1054 exception_error ( "addFilterAction" , e
);
1058 function quickAddFilter () {
1061 if (! inPreferences ()) {
1062 query
= "backend.php?op=pref-filters&method=newfilter&feed=" +
1063 param_escape ( getActiveFeedId ()) + "&is_cat=" +
1064 param_escape ( activeFeedIsCat ());
1066 query
= "backend.php?op=pref-filters&method=newfilter" ;
1071 if ( dijit
. byId ( "feedEditDlg" ))
1072 dijit
. byId ( "feedEditDlg" ). destroyRecursive ();
1074 if ( dijit
. byId ( "filterEditDlg" ))
1075 dijit
. byId ( "filterEditDlg" ). destroyRecursive ();
1077 dialog
= new dijit
. Dialog ({
1078 id
: "filterEditDlg" ,
1079 title
: __ ( "Create Filter" ),
1080 style
: "width: 600px" ,
1082 var query
= "backend.php?" + dojo
. formToQuery ( "filter_new_form" ) + "&savemode=test" ;
1084 if ( dijit
. byId ( "filterTestDlg" ))
1085 dijit
. byId ( "filterTestDlg" ). destroyRecursive ();
1087 var test_dlg
= new dijit
. Dialog ({
1088 id
: "filterTestDlg" ,
1089 title
: "Test Filter" ,
1090 style
: "width: 600px" ,
1095 selectRules : function ( select
) {
1096 $$( "#filterDlg_Matches input[type=checkbox]" ). each ( function ( e
) {
1099 e
. parentNode
. addClassName ( "Selected" );
1101 e
. parentNode
. removeClassName ( "Selected" );
1104 selectActions : function ( select
) {
1105 $$( "#filterDlg_Actions input[type=checkbox]" ). each ( function ( e
) {
1109 e
. parentNode
. addClassName ( "Selected" );
1111 e
. parentNode
. removeClassName ( "Selected" );
1115 editRule : function ( e
) {
1116 var li
= e
. parentNode
;
1117 var rule
= li
. getElementsByTagName ( "INPUT" )[ 1 ]. value
;
1118 addFilterRule ( li
, rule
);
1120 editAction : function ( e
) {
1121 var li
= e
. parentNode
;
1122 var action
= li
. getElementsByTagName ( "INPUT" )[ 1 ]. value
;
1123 addFilterAction ( li
, action
);
1125 addAction : function () { addFilterAction (); },
1126 addRule : function () { addFilterRule (); },
1127 deleteAction : function () {
1128 $$( "#filterDlg_Actions li.[class*=Selected]" ). each ( function ( e
) { e
. parentNode
. removeChild ( e
) });
1130 deleteRule : function () {
1131 $$( "#filterDlg_Matches li.[class*=Selected]" ). each ( function ( e
) { e
. parentNode
. removeChild ( e
) });
1133 execute : function () {
1134 if ( this . validate ()) {
1136 var query
= dojo
. formToQuery ( "filter_new_form" );
1140 new Ajax
. Request ( "backend.php" , {
1142 onComplete : function ( transport
) {
1143 if ( inPreferences ()) {
1153 if (! inPreferences ()) {
1154 var selectedText
= getSelectionText ();
1156 var lh
= dojo
. connect ( dialog
, "onLoad" , function (){
1157 dojo
. disconnect ( lh
);
1159 if ( selectedText
!= "" ) {
1161 var feed_id
= activeFeedIsCat () ? 'CAT:' + parseInt ( getActiveFeedId ()) :
1164 var rule
= { reg_exp
: selectedText
, feed_id
: feed_id
, filter_type
: 1 };
1166 addFilterRule ( null , dojo
. toJson ( rule
));
1170 var query
= "op=rpc&method=getlinktitlebyid&id=" + getActiveArticleId ();
1172 new Ajax
. Request ( "backend.php" , {
1174 onComplete : function ( transport
) {
1175 var reply
= JSON
. parse ( transport
. responseText
);
1179 if ( reply
&& reply
) title
= reply
. title
;
1181 if ( title
|| getActiveFeedId () || activeFeedIsCat ()) {
1183 console
. log ( title
+ " " + getActiveFeedId ());
1185 var feed_id
= activeFeedIsCat () ? 'CAT:' + parseInt ( getActiveFeedId ()) :
1188 var rule
= { reg_exp
: title
, feed_id
: feed_id
, filter_type
: 1 };
1190 addFilterRule ( null , dojo
. toJson ( rule
));
1203 exception_error ( "quickAddFilter" , e
);
1207 function resetPubSub ( feed_id
, title
) {
1209 var msg
= __ ( "Reset subscription? Tiny Tiny RSS will try to subscribe to the notification hub again on next feed update." ). replace ( "%s" , title
);
1211 if ( title
== undefined || confirm ( msg
)) {
1212 notify_progress ( "Loading, please wait..." );
1214 var query
= "?op=pref-feeds&quiet=1&method=resetPubSub&ids=" + feed_id
;
1216 new Ajax
. Request ( "backend.php" , {
1218 onComplete : function ( transport
) {
1219 dijit
. byId ( "pubsubReset_Btn" ). attr ( 'disabled' , true );
1220 notify_info ( "Subscription reset." );
1228 function unsubscribeFeed ( feed_id
, title
) {
1230 var msg
= __ ( "Unsubscribe from %s?" ). replace ( "%s" , title
);
1232 if ( title
== undefined || confirm ( msg
)) {
1233 notify_progress ( "Removing feed..." );
1235 var query
= "?op=pref-feeds&quiet=1&method=remove&ids=" + feed_id
;
1237 new Ajax
. Request ( "backend.php" , {
1239 onComplete : function ( transport
) {
1241 if ( dijit
. byId ( "feedEditDlg" )) dijit
. byId ( "feedEditDlg" ). hide ();
1243 if ( inPreferences ()) {
1246 if ( feed_id
== getActiveFeedId ())
1247 setTimeout ( "viewfeed(-5)" , 100 );
1249 if ( feed_id
< 0 ) updateFeedList ();
1259 function backend_sanity_check_callback ( transport
) {
1263 if ( sanity_check_done
) {
1264 fatalError ( 11 , "Sanity check request received twice. This can indicate " +
1265 "presence of Firebug or some other disrupting extension. " +
1266 "Please disable it and try again." );
1270 var reply
= JSON
. parse ( transport
. responseText
);
1273 fatalError ( 3 , "Sanity check: invalid RPC reply" , transport
. responseText
);
1277 var error_code
= reply
[ 'error' ][ 'code' ];
1279 if ( error_code
&& error_code
!= 0 ) {
1280 return fatalError ( error_code
, reply
[ 'error' ][ 'message' ]);
1283 console
. log ( "sanity check ok" );
1285 var params
= reply
[ 'init-params' ];
1288 console
. log ( 'reading init-params...' );
1291 console
. log ( "IP: " + k
+ " => " + JSON
. stringify ( params
[ k
]));
1292 if ( k
== "label_base_index" ) _label_base_index
= parseInt ( params
[ k
]);
1295 init_params
= params
;
1297 // PluginHost might not be available on non-index pages
1298 window
. PluginHost
&& PluginHost
. run ( PluginHost
. HOOK_PARAMS_LOADED
, init_params
);
1301 sanity_check_done
= true ;
1303 init_second_stage ();
1306 exception_error ( "backend_sanity_check_callback" , e
, transport
);
1310 /*function has_local_storage() {
1312 return 'sessionStorage' in window && window['sessionStorage'] != null;
1318 function catSelectOnChange ( elem
) {
1320 /* var value = elem[elem.selectedIndex].value;
1321 var def = elem.getAttribute('default');
1323 if (value == "ADD_CAT") {
1326 dropboxSelect(elem, def);
1328 elem.selectedIndex = 0;
1334 exception_error ( "catSelectOnChange" , e
);
1338 function quickAddCat ( elem
) {
1340 var cat
= prompt ( __ ( "Please enter category title:" ));
1344 var query
= "?op=rpc&method=quickAddCat&cat=" + param_escape ( cat
);
1346 notify_progress ( "Loading, please wait..." , true );
1348 new Ajax
. Request ( "backend.php" , {
1350 onComplete : function ( transport
) {
1351 var response
= transport
. responseXML
;
1352 var select
= response
. getElementsByTagName ( "select" )[ 0 ];
1353 var options
= select
. getElementsByTagName ( "option" );
1355 dropbox_replace_options ( elem
, options
);
1364 exception_error ( "quickAddCat" , e
);
1368 function genUrlChangeKey ( feed
, is_cat
) {
1371 var ok
= confirm ( __ ( "Generate new syndication address for this feed?" ));
1375 notify_progress ( "Trying to change address..." , true );
1377 var query
= "?op=pref-feeds&method=regenFeedKey&id=" + param_escape ( feed
) +
1378 "&is_cat=" + param_escape ( is_cat
);
1380 new Ajax
. Request ( "backend.php" , {
1382 onComplete : function ( transport
) {
1383 var reply
= JSON
. parse ( transport
. responseText
);
1384 var new_link
= reply
. link
;
1386 var e
= $( 'gen_feed_url' );
1390 e
. innerHTML
= e
. innerHTML
. replace ( /\&key=.*$/ ,
1391 "&key=" + new_link
);
1393 e
. href
= e
. href
. replace ( /\&key=.*$/ ,
1394 "&key=" + new_link
);
1396 new Effect
. Highlight ( e
);
1401 notify_error ( "Could not change feed URL." );
1406 exception_error ( "genUrlChangeKey" , e
);
1411 function labelSelectOnChange ( elem
) {
1413 /* var value = elem[elem.selectedIndex].value;
1414 var def = elem.getAttribute('default');
1416 if (value == "ADD_LABEL") {
1419 dropboxSelect(elem, def);
1421 elem.selectedIndex = 0;
1423 addLabel(elem, function(transport) {
1427 var response = transport.responseXML;
1428 var select = response.getElementsByTagName("select")[0];
1429 var options = select.getElementsByTagName("option");
1431 dropbox_replace_options(elem, options);
1435 exception_error("addLabel", e);
1441 exception_error ( "labelSelectOnChange" , e
);
1445 function dropbox_replace_options ( elem
, options
) {
1448 while ( elem
. hasChildNodes ())
1449 elem
. removeChild ( elem
. firstChild
);
1453 for ( var i
= 0 ; i
< options
. length
; i
++) {
1454 var text
= options
[ i
]. firstChild
. nodeValue
;
1455 var value
= options
[ i
]. getAttribute ( "value" );
1457 if ( value
== undefined ) value
= text
;
1459 var issel
= options
[ i
]. getAttribute ( "selected" ) == "1" ;
1461 var option
= new Option ( text
, value
, issel
);
1463 if ( options
[ i
]. getAttribute ( "disabled" ))
1464 option
. setAttribute ( "disabled" , true );
1466 elem
. insert ( option
);
1468 if ( issel
) sel_idx
= i
;
1471 // Chrome doesn't seem to just select stuff when you pass new Option(x, y, true)
1472 if ( sel_idx
>= 0 ) elem
. selectedIndex
= sel_idx
;
1475 exception_error ( "dropbox_replace_options" , e
);
1479 // mode = all, none, invert
1480 function selectTableRows ( id
, mode
) {
1482 var rows
= $( id
). rows
;
1484 for ( var i
= 0 ; i
< rows
. length
; i
++) {
1489 if ( row
. id
&& row
. className
) {
1490 var bare_id
= row
. id
. replace ( /^[A-Z]*?-/ , "" );
1491 var inputs
= rows
[ i
]. getElementsByTagName ( "input" );
1493 for ( var j
= 0 ; j
< inputs
. length
; j
++) {
1494 var input
= inputs
[ j
];
1496 if ( input
. getAttribute ( "type" ) == "checkbox" &&
1497 input
. id
. match ( bare_id
)) {
1500 dcb
= dijit
. getEnclosingWidget ( cb
);
1506 var issel
= row
. hasClassName ( "Selected" );
1508 if ( mode
== "all" && ! issel
) {
1509 row
. addClassName ( "Selected" );
1511 if ( dcb
) dcb
. set ( "checked" , true );
1512 } else if ( mode
== "none" && issel
) {
1513 row
. removeClassName ( "Selected" );
1515 if ( dcb
) dcb
. set ( "checked" , false );
1517 } else if ( mode
== "invert" ) {
1520 row
. removeClassName ( "Selected" );
1522 if ( dcb
) dcb
. set ( "checked" , false );
1524 row
. addClassName ( "Selected" );
1526 if ( dcb
) dcb
. set ( "checked" , true );
1534 exception_error ( "selectTableRows" , e
);
1539 function getSelectedTableRowIds ( id
) {
1543 var elem_rows
= $( id
). rows
;
1545 for ( var i
= 0 ; i
< elem_rows
. length
; i
++) {
1546 if ( elem_rows
[ i
]. hasClassName ( "Selected" )) {
1547 var bare_id
= elem_rows
[ i
]. id
. replace ( /^[A-Z]*?-/ , "" );
1553 exception_error ( "getSelectedTableRowIds" , e
);
1559 function editFeed ( feed
, event
) {
1562 return alert ( __ ( "You can't edit this kind of feed." ));
1564 var query
= "backend.php?op=pref-feeds&method=editfeed&id=" +
1569 if ( dijit
. byId ( "filterEditDlg" ))
1570 dijit
. byId ( "filterEditDlg" ). destroyRecursive ();
1572 if ( dijit
. byId ( "feedEditDlg" ))
1573 dijit
. byId ( "feedEditDlg" ). destroyRecursive ();
1575 dialog
= new dijit
. Dialog ({
1577 title
: __ ( "Edit Feed" ),
1578 style
: "width: 600px" ,
1579 execute : function () {
1580 if ( this . validate ()) {
1581 // console.log(dojo.objectToQuery(this.attr('value')));
1583 notify_progress ( "Saving data..." , true );
1585 new Ajax
. Request ( "backend.php" , {
1586 parameters
: dojo
. objectToQuery ( dialog
. attr ( 'value' )),
1587 onComplete : function ( transport
) {
1599 exception_error ( "editFeed" , e
);
1603 function feedBrowser () {
1605 var query
= "backend.php?op=feeds&method=feedBrowser" ;
1607 if ( dijit
. byId ( "feedAddDlg" ))
1608 dijit
. byId ( "feedAddDlg" ). hide ();
1610 if ( dijit
. byId ( "feedBrowserDlg" ))
1611 dijit
. byId ( "feedBrowserDlg" ). destroyRecursive ();
1613 var dialog
= new dijit
. Dialog ({
1614 id
: "feedBrowserDlg" ,
1615 title
: __ ( "More Feeds" ),
1616 style
: "width: 600px" ,
1617 getSelectedFeedIds : function () {
1618 var list
= $$( "#browseFeedList li[id*=FBROW]" );
1619 var selected
= new Array ();
1621 list
. each ( function ( child
) {
1622 var id
= child
. id
. replace ( "FBROW-" , "" );
1624 if ( child
. hasClassName ( 'Selected' )) {
1631 getSelectedFeeds : function () {
1632 var list
= $$( "#browseFeedList li.Selected" );
1633 var selected
= new Array ();
1635 list
. each ( function ( child
) {
1636 var title
= child
. getElementsBySelector ( "span.fb_feedTitle" )[ 0 ]. innerHTML
;
1637 var url
= child
. getElementsBySelector ( "a.fb_feedUrl" )[ 0 ]. href
;
1639 selected
. push ([ title
, url
]);
1646 subscribe : function () {
1647 var mode
= this . attr ( 'value' ). mode
;
1651 selected
= this . getSelectedFeeds ();
1653 selected
= this . getSelectedFeedIds ();
1655 if ( selected
. length
> 0 ) {
1656 dijit
. byId ( "feedBrowserDlg" ). hide ();
1658 notify_progress ( "Loading, please wait..." , true );
1660 // we use dojo.toJson instead of JSON.stringify because
1661 // it somehow escapes everything TWICE, at least in Chrome 9
1663 var query
= "?op=rpc&method=massSubscribe&payload=" +
1664 param_escape ( dojo
. toJson ( selected
)) + "&mode=" + param_escape ( mode
);
1668 new Ajax
. Request ( "backend.php" , {
1670 onComplete : function ( transport
) {
1676 alert ( __ ( "No feeds are selected." ));
1680 update : function () {
1681 var query
= dojo
. objectToQuery ( dialog
. attr ( 'value' ));
1683 Element
. show ( 'feed_browser_spinner' );
1685 new Ajax
. Request ( "backend.php" , {
1687 onComplete : function ( transport
) {
1690 Element
. hide ( 'feed_browser_spinner' );
1692 var c
= $( "browseFeedList" );
1694 var reply
= JSON
. parse ( transport
. responseText
);
1696 var r
= reply
[ 'content' ];
1697 var mode
= reply
[ 'mode' ];
1703 dojo
. parser
. parse ( "browseFeedList" );
1706 Element
. show ( dijit
. byId ( 'feed_archive_remove' ). domNode
);
1708 Element
. hide ( dijit
. byId ( 'feed_archive_remove' ). domNode
);
1713 removeFromArchive : function () {
1714 var selected
= this . getSelectedFeedIds ();
1716 if ( selected
. length
> 0 ) {
1718 var pr
= __ ( "Remove selected feeds from the archive? Feeds with stored articles will not be removed." );
1721 Element
. show ( 'feed_browser_spinner' );
1723 var query
= "?op=rpc&method=remarchive&ids=" +
1724 param_escape ( selected
. toString ());;
1726 new Ajax
. Request ( "backend.php" , {
1728 onComplete : function ( transport
) {
1734 execute : function () {
1735 if ( this . validate ()) {
1744 exception_error ( "editFeed" , e
);
1748 function showFeedsWithErrors () {
1750 var query
= "backend.php?op=pref-feeds&method=feedsWithErrors" ;
1752 if ( dijit
. byId ( "errorFeedsDlg" ))
1753 dijit
. byId ( "errorFeedsDlg" ). destroyRecursive ();
1755 dialog
= new dijit
. Dialog ({
1756 id
: "errorFeedsDlg" ,
1757 title
: __ ( "Feeds with update errors" ),
1758 style
: "width: 600px" ,
1759 getSelectedFeeds : function () {
1760 return getSelectedTableRowIds ( "prefErrorFeedList" );
1762 removeSelected : function () {
1763 var sel_rows
= this . getSelectedFeeds ();
1765 console
. log ( sel_rows
);
1767 if ( sel_rows
. length
> 0 ) {
1768 var ok
= confirm ( __ ( "Remove selected feeds?" ));
1771 notify_progress ( "Removing selected feeds..." , true );
1773 var query
= "?op=pref-feeds&method=remove&ids=" +
1774 param_escape ( sel_rows
. toString ());
1776 new Ajax
. Request ( "backend.php" , {
1778 onComplete : function ( transport
) {
1786 alert ( __ ( "No feeds are selected." ));
1789 execute : function () {
1790 if ( this . validate ()) {
1798 exception_error ( "showFeedsWithErrors" , e
);
1803 /* new support functions for SelectByTag */
1805 function get_all_tags ( selObj
){
1807 if ( ! selObj
) return "" ;
1810 var len
= selObj
. options
. length
;
1812 for ( var i
= 0 ; i
< len
; i
++){
1813 if ( selObj
. options
[ i
]. selected
) {
1814 result
+= selObj
[ i
]. value
+ "%2C" ; // is really a comma
1818 if ( result
. length
> 0 ){
1819 result
= result
. substr ( 0 , result
. length
- 3 ); // remove trailing %2C
1825 exception_error ( "get_all_tags" , e
);
1829 function get_radio_checked ( radioObj
) {
1831 if (! radioObj
) return "" ;
1833 var len
= radioObj
. length
;
1835 if ( len
== undefined ){
1836 if ( radioObj
. checked
){
1837 return ( radioObj
. value
);
1843 for ( var i
= 0 ; i
< len
; i
++ ){
1844 if ( radioObj
[ i
]. checked
){
1845 return ( radioObj
[ i
]. value
);
1850 exception_error ( "get_radio_checked" , e
);
1855 function get_timestamp () {
1856 var date
= new Date ();
1857 return Math
. round ( date
. getTime () / 1000 );
1860 function helpDialog ( topic
) {
1862 var query
= "backend.php?op=backend&method=help&topic=" + param_escape ( topic
);
1864 if ( dijit
. byId ( "helpDlg" ))
1865 dijit
. byId ( "helpDlg" ). destroyRecursive ();
1867 dialog
= new dijit
. Dialog ({
1870 style
: "width: 600px" ,
1877 exception_error ( "helpDialog" , e
);
1881 function htmlspecialchars_decode ( string
, quote_style
) {
1882 // http://kevin.vanzonneveld.net
1883 // + original by: Mirek Slugen
1884 // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
1885 // + bugfixed by: Mateusz "loonquawl" Zalega
1886 // + input by: ReverseSyntax
1887 // + input by: Slawomir Kaniecki
1888 // + input by: Scott Cariss
1889 // + input by: Francois
1890 // + bugfixed by: Onno Marsman
1891 // + revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
1892 // + bugfixed by: Brett Zamir (http://brett-zamir.me)
1893 // + input by: Ratheous
1894 // + input by: Mailfaker (http://www.weedem.fr/)
1895 // + reimplemented by: Brett Zamir (http://brett-zamir.me)
1896 // + bugfixed by: Brett Zamir (http://brett-zamir.me)
1897 // * example 1: htmlspecialchars_decode("<p>this -> "</p>", 'ENT_NOQUOTES');
1898 // * returns 1: '<p>this -> "</p>'
1899 // * example 2: htmlspecialchars_decode("&quot;");
1900 // * returns 2: '"'
1904 if ( typeof quote_style
=== 'undefined' ) {
1907 string
= string
. toString (). replace ( /</g , '<' ). replace ( />/g , '>' );
1910 'ENT_HTML_QUOTE_SINGLE' : 1 ,
1911 'ENT_HTML_QUOTE_DOUBLE' : 2 ,
1916 if ( quote_style
=== 0 ) {
1919 if ( typeof quote_style
!== 'number' ) { // Allow for a single string or an array of string flags
1920 quote_style
= []. concat ( quote_style
);
1921 for ( i
= 0 ; i
< quote_style
. length
; i
++) {
1922 // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
1923 if ( OPTS
[ quote_style
[ i
]] === 0 ) {
1925 } else if ( OPTS
[ quote_style
[ i
]]) {
1926 optTemp
= optTemp
| OPTS
[ quote_style
[ i
]];
1929 quote_style
= optTemp
;
1931 if ( quote_style
& OPTS
. ENT_HTML_QUOTE_SINGLE
) {
1932 string
= string
. replace ( /�*39;/g , "'" ); // PHP doesn't currently escape if more than one 0, but it should
1933 // string = string.replace(/'|�*27;/g, "'"); // This would also be useful here, but not a part of PHP
1936 string
= string
. replace ( /"/g , '"' );
1938 // Put this in last place to avoid escape being double-decoded
1939 string
= string
. replace ( /&/g , '&' );
1945 function label_to_feed_id ( label
) {
1946 return _label_base_index
- 1 - Math
. abs ( label
);
1949 function feed_to_label_id ( feed
) {
1950 return _label_base_index
- 1 + Math
. abs ( feed
);
1953 // http://stackoverflow.com/questions/6251937/how-to-get-selecteduser-highlighted-text-in-contenteditable-element-and-replac
1955 function getSelectionText () {
1958 if ( typeof window
. getSelection
!= "undefined" ) {
1959 var sel
= window
. getSelection ();
1960 if ( sel
. rangeCount
) {
1961 var container
= document
. createElement ( "div" );
1962 for ( var i
= 0 , len
= sel
. rangeCount
; i
< len
; ++ i
) {
1963 container
. appendChild ( sel
. getRangeAt ( i
). cloneContents ());
1965 text
= container
. innerHTML
;
1967 } else if ( typeof document
. selection
!= "undefined" ) {
1968 if ( document
. selection
. type
== "Text" ) {
1969 text
= document
. selection
. createRange (). textText
;
1973 return text
. stripTags ();