UNPKG

my-snip

Version:

This tool allows you to quickly prototype and develop a bookmarklet, aggregate snippets, and write user-flow scripts.

1 lines 8.62 kB
(()=>{"use strict";function e(t,n,i){Object.keys(t).reduce(((n,o)=>("object"==typeof t[o]?e(t[o],n,i+o+"▷"):(i+o).includes("_")||n.push(i+o),n)),n)}function t(e){const t=document.getElementById(e);t&&t.parentNode.removeChild(t)}const n="inquirer-ui-menu";function i(i,o){const s=[];e(i,s,"");const c={name:"keyname",choices:s};t(n);const l=document.createElement("ul");document.body.appendChild(l);const r={};function u(e){const t=e.keyname.split("▷");let n=i;t.reduce(((e,t)=>{const i=e[t];return"function"==typeof i&&(n=e),i}),i).call(n,o)}l.setAttribute("id",n),l.setAttribute("style"," z-index: 2147483000; position:fixed; list-style-type:none; margin:0; padding: 0; font-family: monospace; font-size: 14px; cursor: pointer; top: 0; left: 0; background: rgba(255, 255, 255, 0.8); border-radius: 16px; box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1); backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px); border: 1px solid rgba(255, 255, 255, 0.3); ");let a=null,d=l;const h=function(e,i,o,s){const l=i.split("▷");a!==l[0]&&(a=null,d=e),l.length>1&&!a&&(a=l[0],d=function(e){const t=encodeURI(a).replace(/\%/g,"");let n=document.getElementById(t);if(n)return n;const i=document.createElement("li");return i.setAttribute("style"," padding: 10px; "),i.innerHTML=a,n=document.createElement("ul"),n.id=t,n.setAttribute("style"," display: none; "),i.appendChild(n),d.appendChild(i),i.onclick=e=>{e.stopPropagation(),"on"===n.dataset.open?(n.setAttribute("style"," display: none; "),n.dataset.open=""):(n.setAttribute("style"," display: block; "),n.dataset.open="on")},n}());const b=i.replace(a+"▷","");return b.includes("▷")?a=h(d,b,o,a):function(e,i){const o=document.createElement("li");o.innerHTML=e,o.setAttribute("style"," padding: 10px; "),o.setAttribute("onmouseover",'this.style.backgroundColor="rgba(81, 114, 201, 0.42)";'),o.setAttribute("onmouseout",'this.style.backgroundColor="rgba(255, 255, 255, 0.2)";'),o.addEventListener("click",(()=>{t(n),u&&(r[c.name]=i,u(r),u=null)}),!1),d.appendChild(o)}(b,o),s};c.choices.forEach((e=>{h(l,e,e)}))}function o(e,t){var n=document.createElement("script");return n.type="text/javascript",n.src=e,t&&(n.onreadystatechange=t,n.onload=t),n}function s(e){return new Promise((function(t,n){try{const n=String.fromCharCode(65+Math.floor(26*Math.random()))+ +new Date,i=-1!==e.indexOf("?")?"&":"?",s=o.bind(this,e+i+"callback=fun"+n)((function(){if(s.readyState&&"complete"!==s.readyState&&"loaded"!==s.readyState)return!1;s.parentNode.removeChild(s)}));window["fun"+n]=function(e){t(e),window["fun"+n]=null},document.getElementsByTagName("head")[0].appendChild(s)}catch(e){n(e)}}))}let c=null;function l(e){let t=[];const n=c[0];if("//"===e.slice(0,2)){const i=7;let o=document.evaluate(e,n||document,null,i,null);for(let e=0,n=o.snapshotLength;e<n;++e)t.push(o.snapshotItem(e))}else t=[...n.querySelectorAll(e)];return t}const r=function(e){const t=function(t,n,i){let o=4e3;return(i||{}).timeout&&(o=i.timeout),new Promise(((i,s)=>{let l=0,r=Date.now(),u=null;e.log("wait "+t),function s(){u=setTimeout((()=>{let a=n();const d=a.length>0;l>=o||d?(clearTimeout(u),d?(e.log("found "+t),c=a,i(c)):(c=[],i(c))):(l=Date.now()-r,s())}),0)}()}))};return{initEl:async e=>{c=[e]},then:e=>e(c),contains:(e,n,i)=>t("contains "+e+" "+n,(()=>l(e).filter((e=>{const t=(e.innerText||"").toLowerCase(),i=(n||"").toLowerCase();return t.indexOf(i)>-1}))),i),eq:async e=>{c=c.slice(e,e+1)},find:async e=>{const t=[...c[0].querySelectorAll(e)];t.length>0&&(c=t)},focused:async()=>{c=[document.activeElement]},get:(e,n)=>t("get "+e,(()=>l(e)),n),last:async()=>{c=[c[c.length-1]]},parent:async()=>{c=[c[0].parentElement]},siblings:async e=>{c=[c[0].parentElement],c=[...c[0].querySelectorAll(e)]},click:async()=>{const{left:e,bottom:t}=c[0].getBoundingClientRect();let n=new MouseEvent("click",{button:0,bubbles:!0,cancelable:!0,clientX:e,clientY:t});c[0].dispatchEvent(n)},rightclick:async()=>{const{left:e,bottom:t}=c[0].getBoundingClientRect();let n=new MouseEvent("contextmenu",{bubbles:!0,cancelable:!1,button:2,buttons:0,clientX:e,clientY:t});c[0].dispatchEvent(n)},select:async e=>{const t=c[0];[...t.options].forEach(((n,i)=>{const o=n.value==e,s=n.text==e;(o||s||i===e)&&(n.selected=!0,t.dispatchEvent(new CustomEvent("change",{bubbles:!0})))}))},trigger:async e=>{let t=new Event(e);0===e.indexOf("key")&&(t=new KeyboardEvent(e,{bubbles:!0})),0===e.indexOf("mouse")&&(t=new MouseEvent(e,{bubbles:!0})),0===e.indexOf("touch")&&(t=new TouchEvent(e,{bubbles:!0})),0===e.indexOf("pointer")&&(t=new PointerEvent(e,{bubbles:!0})),0===e.indexOf("wheel")&&(t=new WheelEvent(e,{bubbles:!0})),0===e.indexOf("drag")&&(t=new DragEvent(e,{bubbles:!0})),0===e.indexOf("clipboard")&&(t=new ClipboardEvent(e,{bubbles:!0})),0===e.indexOf("composition")&&(t=new CompositionEvent(e,{bubbles:!0})),0===e.indexOf("input")&&(t=new InputEvent(e,{bubbles:!0})),0===e.indexOf("animation")&&(t=new AnimationEvent(e,{bubbles:!0})),0===e.indexOf("transition")&&(t=new TransitionEvent(e,{bubbles:!0})),0===e.indexOf("message")&&(t=new MessageEvent(e,{bubbles:!0})),0===e.indexOf("storage")&&(t=new StorageEvent(e,{bubbles:!0})),c[0].dispatchEvent(t)},type:async e=>{const t=e.replace(/{backspace}|{del}|{downArrow}|{end}|{enter}|{esc}|{home}|{insert}|{leftArrow}|{moveToEnd}|{moveToStart}|{pageDown}|{pageUp}|{rightArrow}|{selectAll}|{upArrow}|{alt}|{ctrl}|{meta}|{shift}]/g,"");c[0].value=t,c[0].dispatchEvent(new CustomEvent("input",{bubbles:!0}))},focus:async()=>{c[0].dispatchEvent(new CustomEvent("focus",{bubbles:!0}))},wait:async e=>await new Promise((t=>setTimeout(t,e))),log:async t=>{e.log(t)}}};class u{constructor(){const e=this;return this._queue=[],this.isRun=!1,this.isAsynch=!1,this._queueAsynch=[],this.isRunAsynch=!1,{_add(t,n){this[t]=(...t)=>(e.runThroughQueue(n.bind(e,...t)),this)}}}runThroughQueue(e){this.isAsynch?(this._queueAsynch.push(e),this.runAsynch()):(this._queue.push(e),this.run())}runAsynch(){this._queueAsynch.length&&!this.isRunAsynch&&(this.isRunAsynch=!0,(this._queueAsynch.shift()()||(async()=>{})()).then((()=>{this.isRunAsynch=!1,this.runAsynch()})))}run(){if(this._queue.length&&!this.isRun){const e=this._queue.shift();this.isRun=!0,this.isAsynch="bound then"===e.name;const t=e();(t||{}).then&&t.then((()=>{this.isAsynch=!1,this.isRun=!1,this.run()}))}}}function a(e,t){const n=new u;for(let[e,i]of Object.entries(t))n._add(e,i);return new Proxy(n,{get:(t,n)=>(t.initEl(e),t[n])})}const d=function(e,t={log(){}}){return a(e,r(t))},h=function(e,t={log(){}}){const n=r(t);return a(e,{initEl:n.initEl,then:n.then,locator:e=>n.get(e),click:e=>"string"==typeof e?n.get(e).then((()=>n.click())):"right"===(e||{}).button?n.rightclick():n.click(),check:()=>n.click(),fill:(e,t)=>e&&t?n.get(e).then((()=>n.type(t))):n.type(e),type:e=>n.type(e),hover:()=>n.trigger("mouseover"),selectOption:e=>n.select(e),getByTestId:e=>n.get(`[data-testid="${e}"]`),getByLabel:async t=>{const i=await n.get(`[aria-label="${t}"]`,{timeout:10});if(i[0])return i;n.initEl(e);const o=await n.contains("label",t,{timeout:10}),s=o[0]&&o[0].getAttribute("for");return s?(n.initEl(e),n.get(`#${s}`)):void 0},getByRole:(e,t)=>{if(t.name)return n.contains(e,t.name)}})},b={log(){console.log(...arguments)}};let p={};const f={},y=function(e,t){return e?(void 0!==t&&(p[e]=t),"object"==typeof e&&(p={...p,...e}),"string"==typeof e&&void 0===t?p[e]:void 0):p};function g(e){import(e).then((e=>{i(e.default,f)}))}f.env=y,f.cy=d(window.document,b),f.Cypress={env:y},f.page=h(window.document,b),window.snip=function(e){e.url&&g(e.url),e.id&&function(e){(function(e,t,n){return e("https://gist.github.com/"+n+".json").then((e=>{var n={},i=t.createElement("div");i.innerHTML=e.div;for(var o=i.querySelectorAll(".gist-file .gist-data .file"),s=0;s<o.length;s++)n[e.files[s]]={html:o[s].outerHTML,text:o[s].textContent.replace("This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters","")};return n}))})(s,document,e.id).then((t=>{const n=t[e.file].text;g(URL.createObjectURL(new Blob([n],{type:"text/javascript"})))}))}(e),e.env&&y(e.env),e.file&&!e.id&&e.file.then((e=>{i(e.default,f)}))}})();