# Javascript notes ###### tags: `Javascript` ## Disable rigth click context menu https://stackoverflow.com/questions/737022/how-do-i-disable-right-click-on-my-web-page ```javascript document.addEventListener('contextmenu', event => event.preventDefault()); ``` ## Modify copyed data https://stackoverflow.com/questions/2026335/how-to-add-extra-info-to-copied-web-text ```javascript document.addEventListener('copy', (event) => { const pagelink = `\n\nRead more at: ${document.location.href}`; event.clipboardData.setData('text/plain', document.getSelection() + pagelink); event.preventDefault(); }); ``` ## Detect ad block [example](https://www.ruanyifeng.com/blog/2023/01/weekly-issue-238.html) ```javascript function checker() { if (/mobile|android/i.test(navigator.userAgent)) { return; } window.comments && window.comments.setAttribute("style", "!important"); var img = document.querySelector('a > img[src*="wangbase.com/blogimg/asset/"]'); if ((img && window.getComputedStyle(img).display === 'none') || (img && window.getComputedStyle(img.parentElement).display === 'none') ){ var sponsor = document.querySelector('#main-content'); var prompt = document.createElement('div'); prompt.style = 'border: 1px solid #c6c6c6;border-radius: 4px;background-color: #f5f2f0;padding: 15px; font-size: 14px;'; prompt.innerHTML = '<p>您使用了,导致本站内容无法显示。</p><p>请将 www.ruanyifeng.com 加入白名单,解除广告屏蔽后,刷新页面。谢谢。</p>'; sponsor.parentNode.replaceChild(prompt, sponsor); } } ``` ## Not selectable [Example](https://blog.csdn.net/vivo01/article/details/126343115) ```html <style> #content_views pre{ -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } #content_views pre code{ -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } </style> ``` ## TS: enum usage https://www.totaltypescript.com/why-i-dont-like-typescript-enums ```lang=ts enum PackStatus { Draft = 0, Approved = 1, Shipped = 2, } // [0, "Draft", 1, "Approved", 2, "Shipped"] console.log(Object.keys(PackStatus)); enum PackStatus2 { Draft = "Draft", Approved = "Approved", Shipped = "Shipped", } // ["Draft", "Approved", "Shipped"] console.log(Object.keys(PackStatus2)); ``` [TS 5.8](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-8.html) introduce erasableSyntaxOnly flag Nodejs support limited ts from v22.6.0 => Can not use `enum` & `namespace` and so on. Do not understand.