1const Selection = (function() { 2 function copyTextToClipboard(text) { 3 let textArea = document.createElement('textarea'); 4 textArea.style.position = 'fixed'; 5 textArea.style.top = 0; 6 textArea.style.left = 0; 7 textArea.style.width = '2em'; 8 textArea.style.height = '2em'; 9 textArea.style.padding = 0; 10 textArea.style.border = 'none'; 11 textArea.style.outline = 'none'; 12 textArea.style.boxShadow = 'none'; 13 textArea.style.background = 'transparent'; 14 textArea.value = text; 15 16 document.body.appendChild(textArea); 17 textArea.focus(); 18 textArea.select(); 19 20 try { 21 let successful = document.execCommand('copy'); 22 let msg = successful ? 'successful' : 'unsuccessful'; 23 } catch (err) { 24 } 25 26 document.body.removeChild(textArea); 27 } 28 29 function popupwindow(url, title, w, h) { 30 let left = screen.width / 2 - w / 2; 31 let top = screen.height / 2 - h / 2; 32 return window.open( 33 url, 34 title, 35 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + 36 w + 37 ', height=' + 38 h + 39 ', top=' + 40 top + 41 ', left=' + 42 left 43 ); 44 } 45 46 function getBrowserLanguage(){ 47 let language = navigator.language || navigator.userLanguage || function (){ 48 const languages = navigator.languages; 49 if (navigator.languages.length > 0){ 50 return navigator.languages[0]; 51 } 52 }() || 'en'; 53 return language.split('-')[0]; 54 } 55 56 function _selection() { 57 const menu = { 58 twitter: true, 59 facebook: true, 60 search: true, 61 copy: true, 62 speak: true, 63 translate: true, 64 disable: false 65 }; 66 const twitterConfig = { 67 url: 'https://twitter.com/intent/tweet?text=', 68 icon: 69 '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" class="selection__icon"><path d="M8.2,20.2c6.5,0,11.7-5.2,11.8-11.6c0-0.1,0-0.1,0-0.2c0-0.2,0-0.4,0-0.5c0.8-0.6,1.5-1.3,2.1-2.2c-0.8,0.3-1.6,0.6-2.4,0.7c0.9-0.5,1.5-1.3,1.8-2.3c-0.8,0.5-1.7,0.8-2.6,1c-1.6-1.7-4.2-1.7-5.9-0.1c-1.1,1-1.5,2.5-1.2,3.9C8.5,8.7,5.4,7.1,3.3,4.6c-1.1,1.9-0.6,4.3,1.3,5.5c-0.7,0-1.3-0.2-1.9-0.5l0,0c0,2,1.4,3.7,3.3,4.1c-0.6,0.2-1.2,0.2-1.9,0.1c0.5,1.7,2.1,2.8,3.9,2.9c-1.7,1.4-3.9,2-6.1,1.7C3.8,19.5,6,20.2,8.2,20.2"/></svg>' 70 }; 71 const facebookConfig = { 72 url: 'https://www.facebook.com/sharer/sharer.php?quote=', 73 icon: 74 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" enable-background="new 0 0 24 24" width="24" height="24" class="selection__icon"><path d="M20,2H4C2.9,2,2,2.9,2,4v16c0,1.1,0.9,2,2,2h16c1.1,0,2-0.9,2-2V4C22,2.9,21.1,2,20,2z M18.4,7.4H17c-0.9,0-1,0.3-1,1l0,1.3 h2.1L18,12h-1.9v7h-3.2v-7h-1.2V9.6h1.2V8.1c0-2,0.8-3.1,3.1-3.1h2.4V7.4z"/></svg>' 75 }; 76 const searchConfig = { 77 url: 'https://www.google.co.in/search?q=', 78 icon: 79 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" enable-background="new 0 0 24 24" width="24" height="24" class="selection__icon"><path d="M23.111 20.058l-4.977-4.977c.965-1.52 1.523-3.322 1.523-5.251 0-5.42-4.409-9.83-9.829-9.83-5.42 0-9.828 4.41-9.828 9.83s4.408 9.83 9.829 9.83c1.834 0 3.552-.505 5.022-1.383l5.021 5.021c2.144 2.141 5.384-1.096 3.239-3.24zm-20.064-10.228c0-3.739 3.043-6.782 6.782-6.782s6.782 3.042 6.782 6.782-3.043 6.782-6.782 6.782-6.782-3.043-6.782-6.782zm2.01-1.764c1.984-4.599 8.664-4.066 9.922.749-2.534-2.974-6.993-3.294-9.922-.749z"/></svg>' 80 }; 81 const copyConfig = { 82 icon: 83 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" enable-background="new 0 0 24 24" width="24" height="24" class="selection__icon"><path d="M18 6v-6h-18v18h6v6h18v-18h-6zm-12 10h-4v-14h14v4h-10v10zm16 6h-14v-14h14v14z"/></svg>' 84 }; 85 const speakConfig = { 86 icon: 87 '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" enable-background="new 0 0 24 24" width="24" height="24" class="selection__icon"><path d="M16 11c0 2.209-1.791 4-4 4s-4-1.791-4-4v-7c0-2.209 1.791-4 4-4s4 1.791 4 4v7zm4-2v2c0 4.418-3.582 8-8 8s-8-3.582-8-8v-2h2v2c0 3.309 2.691 6 6 6s6-2.691 6-6v-2h2zm-7 13v-2h-2v2h-4v2h10v-2h-4z"/></svg>' 88 }; 89 const translateConfig = { 90 url:'https://translate.google.com/#auto/', 91 icon:'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" enable-background="new 0 0 24 24" width="24" height="24" class="selection__icon">'+ 92 '<path id="svg_3" d="m17,20l-14.5,0c-1.378,0 -2.5,-1.122 -2.5,-2.5l0,-15c0,-1.378 1.122,-2.5 2.5,-2.5l8,0c0.214,0 0.404,0.136 0.473,0.338l6.5,19c0.052,0.152 0.027,0.321 -0.066,0.452c-0.094,0.132 -0.245,0.21 -0.407,0.21zm-14.5,-19c-0.827,0 -1.5,0.673 -1.5,1.5l0,15c0,0.827 0.673,1.5 1.5,1.5l13.8,0l-6.157,-18l-7.643,0z"/>'+ 93 '<path id="svg_5" d="m21.5,24l-8,0c-0.208,0 -0.395,-0.129 -0.468,-0.324l-1.5,-4c-0.097,-0.259 0.034,-0.547 0.292,-0.644c0.259,-0.096 0.547,0.034 0.644,0.292l1.379,3.676l7.653,0c0.827,0 1.5,-0.673 1.5,-1.5l0,-15c0,-0.827 -0.673,-1.5 -1.5,-1.5l-9.5,0c-0.276,0 -0.5,-0.224 -0.5,-0.5s0.224,-0.5 0.5,-0.5l9.5,0c1.378,0 2.5,1.122 2.5,2.5l0,15c0,1.378 -1.122,2.5 -2.5,2.5z"/>'+
94 '<path id="svg_7" d="m13.5,24c-0.117,0 -0.234,-0.041 -0.329,-0.124c-0.208,-0.182 -0.229,-0.498 -0.047,-0.706l3.5,-4c0.182,-0.209 0.498,-0.229 0.706,-0.047c0.208,0.182 0.229,0.498 0.047,0.706l-3.5,4c-0.1,0.113 -0.238,0.171 -0.377,0.171z"/>'+ 95 '<path id="svg_9" d="m9.5,14c-0.206,0 -0.398,-0.127 -0.471,-0.332l-2.029,-5.681l-2.029,5.681c-0.093,0.26 -0.38,0.396 -0.639,0.303c-0.26,-0.093 -0.396,-0.379 -0.303,-0.639l2.5,-7c0.142,-0.398 0.8,-0.398 0.941,0l2.5,7c0.093,0.26 -0.042,0.546 -0.303,0.639c-0.054,0.02 -0.111,0.029 -0.167,0.029z"/>'+ 96 '<path id="svg_11" d="m8,11l-2,0c-0.276,0 -0.5,-0.224 -0.5,-0.5s0.224,-0.5 0.5,-0.5l2,0c0.276,0 0.5,0.224 0.5,0.5s-0.224,0.5 -0.5,0.5z"/>'+ 97 '<path id="svg_13" d="m21.5,11l-7,0c-0.276,0 -0.5,-0.224 -0.5,-0.5s0.224,-0.5 0.5,-0.5l7,0c0.276,0 0.5,0.224 0.5,0.5s-0.224,0.5 -0.5,0.5z"/>'+ 98 '<path id="svg_15" d="m17.5,11c-0.276,0 -0.5,-0.224 -0.5,-0.5l0,-1c0,-0.276 0.224,-0.5 0.5,-0.5s0.5,0.224 0.5,0.5l0,1c0,0.276 -0.224,0.5 -0.5,0.5z"/>'+ 99 '<path id="svg_17" d="m16,17c-0.157,0 -0.311,-0.073 -0.408,-0.21c-0.16,-0.225 -0.107,-0.537 0.118,-0.697c2.189,-1.555 3.79,-4.727 3.79,-5.592c0,-0.276 0.224,-0.5 0.5,-0.5s0.5,0.224 0.5,0.5c0,1.318 -1.927,4.785 -4.21,6.408c-0.088,0.061 -0.189,0.091 -0.29,0.091z"/>'+ 100 '<path id="svg_19" d="m20,18c-0.121,0 -0.242,-0.043 -0.337,-0.131c-0.363,-0.332 -3.558,-3.283 -4.126,-4.681c-0.104,-0.256 0.02,-0.547 0.275,-0.651c0.253,-0.103 0.547,0.019 0.651,0.275c0.409,1.007 2.936,3.459 3.875,4.319c0.204,0.187 0.217,0.502 0.031,0.707c-0.099,0.107 -0.234,0.162 -0.369,0.162z"/>'+ 101 '</svg>' 102 }; 103 104 let selection = ''; 105 let text = ''; 106 let bgcolor = 'crimson'; 107 let iconcolor = '#fff'; 108 109 let _icons = {}; 110 let arrowsize = 5; 111 let buttonmargin = 7 * 2; 112 let iconsize = 24 + buttonmargin; 113 let top = 0; 114 let left = 0; 115 116 function facebookButton() { 117 const fbbtn = new Button(facebookConfig.icon, function() { 118 let sharelink = window.location.href; 119 let finalurl = facebookConfig.url; 120 if (sharelink.substring(0, 4) !== 'http') { 121 sharelink = 'http://www.demourl.com'; 122 } 123 finalurl += text + '&u=' + sharelink; 124 popupwindow(finalurl, 'Share', 600, 500); 125 }); 126 return fbbtn; 127 } 128 129 function twitterButton() { 130 const url = window.location.href; 131 const twbtn = new Button(twitterConfig.icon, function() { 132 popupwindow(twitterConfig.url + encodeURIComponent(text) + ' ' + url, 'Share', 550, 295); 133 return false; 134 }); 135 136 return twbtn; 137 } 138 139 function searchButton() { 140 const searchbtn = new Button(searchConfig.icon, function() { 141 popupwindow(searchConfig.url + encodeURIComponent(text), 'Search', 900, 540); 142 return false; 143 }); 144 145 return searchbtn; 146 } 147 148 function copyButton() { 149 const cbtn = new Button(copyConfig.icon, function() { 150 copyTextToClipboard(text); 151 }); 152 return cbtn; 153 } 154 155 function speakButton() { 156 const spbtn = new Button(speakConfig.icon, function() { 157 let speech = new SpeechSynthesisUtterance(text); 158 window.speechSynthesis.speak(speech); 159 }); 160 return spbtn; 161 } 162 163 function translateButton() { 164 const tsbtn = new Button(translateConfig.icon, function() { 165 popupwindow(translateConfig.url + getBrowserLanguage() + '/' + text, 'Translate', 900, 540); 166 return false; 167 }); 168 return tsbtn; 169 } 170 171 function IconStyle() { 172 const style = document.createElement('style'); 173 style.innerHTML = `.selection__icon{fill:${iconcolor};}`; 174 document.body.appendChild(style); 175 } 176 177 function appendIcons() { 178 const myitems=[{feature:'twitter',call:twitterButton()},{feature:'facebook',call:facebookButton()},{feature:'search',call:searchButton()},{feature:'translate',call:translateButton()}, 179 {feature:'copy',call:copyButton()},{feature:'speak',call:speakButton()}] 180 const div = document.createElement('div'); 181 let count = 0;
182 myitems.forEach((item)=>{ 183 if(menu[item.feature]){ 184 div.appendChild(item.call); 185 count++; 186 } 187 }) 188 return { 189 icons: div, 190 length: count 191 }; 192 } 193 194 function setTooltipPosition() { 195 const position = selection.getRangeAt(0).getBoundingClientRect(); 196 const DOCUMENT_SCROLL_TOP = 197 window.pageXOffset || document.documentElement.scrollTop || document.body.scrollTop; 198 top = position.top + DOCUMENT_SCROLL_TOP - iconsize - arrowsize; 199 left = position.left + (position.width - iconsize * _icons.length) / 2; 200 } 201 202 function moveTooltip() { 203 setTooltipPosition(); 204 let tooltip = document.querySelector('.selection'); 205 tooltip.style.top = `${top}px`; 206 tooltip.style.left = `${left}px`; 207 } 208 209 function drawTooltip() { 210 _icons = appendIcons(); 211 setTooltipPosition(); 212 213 const div = document.createElement('div'); 214 div.className = 'selection'; 215 div.style = 216 'line-height:0;' + 217 'position:absolute;' + 218 'background-color:' + 219 bgcolor + 220 ';' + 221 'border-radius:20px;' + 222 'top:' + 223 top + 224 'px;' + 225 'left:' + 226 left + 227 'px;' + 228 'transition:all .2s ease-in-out;' + 229 'box-shadow: 0 14px 28px rgba(0,0,0,0.25), 0 10px 10px rgba(0,0,0,0.22);' + 230 'z-index:99999;'; 231 232 div.appendChild(_icons.icons); 233 234 const arrow = document.createElement('div'); 235 arrow.style = 236 'position:absolute;' + 237 'border-left:' + 238 arrowsize + 239 'px solid transparent;' + 240 'border-right:' + 241 arrowsize + 242 'px solid transparent;' + 243 'border-top:' + 244 arrowsize + 245 'px solid ' + 246 bgcolor + 247 ';' + 248 'bottom:-' + 249 (arrowsize - 1) + 250 'px;' + 251 'left:' + 252 (iconsize * _icons.length / 2 - arrowsize) + 253 'px;' + 254 'width:0;' + 255 'height:0;'; 256 257 if (!menu.disable) { 258 div.appendChild(arrow); 259 } 260 261 document.body.appendChild(div); 262 } 263 264 function attachEvents() { 265 function hasSelection() { 266 return !!window.getSelection().toString(); 267 } 268 269 function hasTooltipDrawn() { 270 return !!document.querySelector('.selection'); 271 } 272 273 window.addEventListener( 274 'mouseup', 275 listener, 276 false 277 ); 278 279 function listener() { 280 if (localStorage.getItem("voice_menu") == 0){ 281 window.removeEventListener( 282 'mouseup', 283 listener, 284 false 285 ); 286 }else{ 287 setTimeout(function mouseTimeout() { 288 if (hasTooltipDrawn()) { 289 if (hasSelection()) { 290 selection = window.getSelection(); 291 text = selection.toString(); 292 moveTooltip(); 293 return; 294 } else { 295 document.querySelector('.selection').remove(); 296 } 297 } 298 if (hasSelection()) { 299 selection = window.getSelection(); 300 text = selection.toString(); 301 drawTooltip(); 302 // if (localStorage.getItem("voice_menu") == 1){ 303 // drawTooltip(); 304 // } 305 306 } 307 }, 10); 308 } 309 } 310 } 311 312 function config(options) { 313 menu.twitter = options.twitter === undefined ? menu.twitter : options.twitter; 314 menu.facebook = options.facebook === undefined ? menu.facebook : options.facebook; 315 menu.search = options.search === undefined ? menu.search : options.search; 316 menu.translate = options.translate === undefined ? menu.translate : options.translate; 317 menu.copy = options.copy === undefined ? menu.copy : options.copy; 318 menu.speak = options.speak === undefined ? menu.speak : options.speak; 319 menu.disable = options.disable === undefined ? menu.disable : options.disable; 320 321 bgcolor = options.backgroundColor || '#333'; 322 iconcolor = options.iconColor || '#fff'; 323 return this; 324 } 325
326 function init() { 327 IconStyle(); 328 attachEvents(); 329 return this; 330 } 331 332 return { 333 config: config, 334 init: init 335 }; 336 } 337 338 function Button(icon, clickFn) { 339 const btn = document.createElement('div'); 340 btn.style = 'display:inline-block;' + 'margin:7px;' + 'cursor:pointer;' + 'transition:all .2s ease-in-out;'; 341 btn.innerHTML = icon; 342 btn.onclick = clickFn; 343 btn.onmouseover = function() { 344 this.style.transform = 'scale(1.2)'; 345 }; 346 btn.onmouseout = function() { 347 this.style.transform = 'scale(1)'; 348 }; 349 return btn; 350 } 351 352 return _selection; 353 })(); 354 355
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.