// snap selection function snapSelectionToWord() { var sel; // Check for existence of window.getSelection() and that it has a // modify() method. IE 9 has both selection APIs but no modify() method. if (window.getSelection && (sel = window.getSelection()).modify) { sel = window.getSelection(); if (!sel.isCollapsed) { // Detect if selection is backwards var range = document.createRange(); range.setStart(sel.anchorNode, sel.anchorOffset); range.setEnd(sel.focusNode, sel.focusOffset); var backwards = range.collapsed; range.detach(); // modify() works on the focus of the selection var endNode = sel.focusNode, endOffset = sel.focusOffset; sel.collapse(sel.anchorNode, sel.anchorOffset); var direction = []; if (backwards) { direction = ['backward', 'forward']; } else { direction = ['forward', 'backward']; } sel.modify("move", direction[0], "character"); sel.modify("move", direction[1], "word"); sel.extend(endNode, endOffset); sel.modify("extend", direction[1], "character"); sel.modify("extend", direction[0], "word"); } } else if ( (sel = document.selection) && sel.type != "Control") { var textRange = sel.createRange(); if (textRange.text) { textRange.expand("word"); // Move the end back to not include the word's trailing space(s), // if necessary while (/\s$/.test(textRange.text)) { textRange.moveEnd("character", -1); } textRange.select(); } } } // if touch screen do nothing function isMobile() { /* var match = window.matchMedia || window.msMatchMedia; if(match) { var mq = match("(pointer:coarse)"); return mq.matches; } return false; */ // if we want a more complete list use this: http://detectmobilebrowsers.com/ // str.test() is more efficent than str.match() // remember str.test is case sensitive var isMobile = (/iphone|ipod|android|ie|blackberry|fennec/).test (navigator.userAgent.toLowerCase()); return isMobile; } // return odd or even day function getOddEven() { var time = new Date().getDate(); var xtoday="even"; if (time % 2 != 0) { xtoday = "odd"; }; return xtoday; } //alert("OK"); // start main pop-up due to selection.. if (!window.x) { x = {}; } x.Selector = {}; x.Selector.getSelected = function() { var t = ''; if (window.getSelection) { t = window.getSelection(); } else if (document.getSelection) { t = document.getSelection(); } else if (document.selection) { t = document.selection.createRange().text; } return t; } //alert("OK"); // https://www.3schools.in/2021/11/how-to-create-button-in-javascript.html // if we store it, add the button if(localStorage.getItem("ERBookmark")!=null) { //alert("Stored!"); const demoDiv = document.getElementById("rbookmark"); const btn = document.createElement("BUTTON"); btn.id = "bookmark-id"; btn.class = "bookmark-class"; btn.type = "submit"; btn.name = "bookmark-name"; btn.innerHTML = "bookmark"; btn.onclick = function(){ //alert("button is clicked"); const URL= localStorage.getItem("ERBookmark"); window.location.href=URL; }; demoDiv.appendChild(btn); const btnClear = document.createElement("BUTTON"); btnClear.id = "bookmark-id-clear"; btnClear.type = "submit"; btnClear.name = "bookmark-name-clear"; btnClear.innerHTML = "☓"; btnClear.onclick = function(){ localStorage.removeItem("ERBookmark"); location.reload(); }; demoDiv.appendChild(btnClear); }; //snapSelectionToWord(); //let mydict = document.getElementById('mw-content-text'); let mydict = document.getElementsByClassName('container-fluid'); if (mydict == null) mydict=document.getElementsByClassName('container'); // is it title page? let htitle_page = document.getElementById("ERTitle"); // remove the line below to enable tip of the day //let htitle_page=true; // initial empty button! if(htitle_page ==null && localStorage.getItem("ERBookmark") == null && isMobile() == false) { //alert("Stored!"); const demoDiv = document.getElementById("rbookmark"); const btn = document.createElement("BUTTON"); btn.id = "bookmark-id-empty"; btn.class = "bookmark-class-empty"; btn.type = "submit"; btn.name = "bookmark-name"; btn.innerHTML = "bookmark"; btn.onclick = function(){ //alert("button is clicked"); //const URL= localStorage.getItem("ERBookmark"); //window.location.href=URL; let URL=window.location.href; localStorage.setItem("ERBookmark", URL); const time2show=3000; const message=" Bookmark to this page is created on the top menu."; Swal.fire({ position: 'bottom-end', html: message, showConfirmButton: false, showCloseButton: true, padding: '4px', backdrop: false, returnFocus: true, width: 400, background: '#e6f1f0', timer: time2show, }); setTimeout(function(){ window.location.reload(); }, 3000); }; demoDiv.appendChild(btn); }; if (mydict != null && isMobile() == false) { for (var i = 0 ; i < mydict.length; i++) { // dictionary selection mydict[i].addEventListener('click', function(e) { // left button. dictionary selection if (e.button == 0) { snapSelectionToWord(); // alert("OK"); var mytext = x.Selector.getSelected(); mytext = mytext.toString(); mytext = mytext.trim(); mytext = mytext.replace(".", ""); mytext = mytext.replace(",", ""); mytext = mytext.replace("!", ""); mytext = mytext.replace("?", ""); mytext = mytext.trim(); // clear the, a, an, of const regex = /(?:(the|a|an|of|in|on|by) +)/g; const subst = ` `; // The substituted value will be contained in the result variable mytext = mytext.replace(regex, subst); mytext = mytext.trim(); //alert(mytext); if (mytext.length > 1 && mytext.length<60) { var xmlhttp; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { //alert(xmlhttp.responseText); var textLength=(xmlhttp.responseText).length; var time2stay=6000; // default if ( textLength > 800) time2stay=15000; if ( textLength > 1600) time2stay=30000; if ( textLength > 3200) time2stay=50000; if ( textLength > 10000) time2stay=100000; // alert(textLength); // deselect text if (window.getSelection) {window.getSelection().removeAllRanges();} else if (document.selection) {document.selection.empty();} //alert(xmlhttp.responseText); Swal.fire({ position: 'top-end', html: xmlhttp.responseText, showConfirmButton: false, showCloseButton: true, padding: '2px', backdrop: false, returnFocus: true, width: 350, timer: time2stay, background: 'white', }); } } //var surl="/dictsimple.php?query="+mytext+"&lang=auto"; var surl="/lookup/simple.php?query="+mytext+"&lang=auto"; xmlhttp.open("GET", surl, true); xmlhttp.send(); } // end if } }); }; // end loop }; // if not null /* =============== Message of the day ================== */ // show only one time if ( htitle_page == null && isMobile() == false ) { var xtoday=getOddEven(); if(localStorage.getItem("firstTimeER"+xtoday)==null){ var xmlhttp2; if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp2 = new XMLHttpRequest(); } else { // code for IE6, IE5 xmlhttp2 = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp2.onreadystatechange = function() { if (xmlhttp2.readyState == 4 && xmlhttp2.status == 200) { //alert(xmlhttp2.responseText); if ( (xmlhttp2.responseText).length>20) { Swal.fire({ position: 'bottom-end', html: xmlhttp2.responseText, showConfirmButton: false, showCloseButton: true, padding: '4px', backdrop: false, returnFocus: true, background: '#fff1c7', width: 340, timer: 6000, }); }; // end length }} xmlhttp2.open("GET", "/DAY_MESSAGE_"+xtoday+".html", true); xmlhttp2.send(); // done localStorage.setItem("firstTimeER"+xtoday,"done"); } } // end no mobile