Bookmarklets: Difference between revisions
From Rest of What I Know
No edit summary |
No edit summary |
||
Line 4: | Line 4: | ||
<syntaxhighlight lang=js> | <syntaxhighlight lang=js> | ||
javascript:void(function(){const e=document.querySelector('article[tabindex="-1"]');if(!e){console.log('No tweet found on this page');return}const t=e.querySelector('[data-testid="User-Name"]'),n=t.getElementsByTagName('span'),a=n[0]?.textContent||%27%27,userLink=e.querySelector(%27a[href^="/"]:not([href*="status"])%27),r=userLink?userLink.getAttribute(%27href%27).replace(%27/%27,%27%27)||%27%27:t.querySelector(%27div[dir="ltr"]%27)?.textContent.replace(%27@%27,%27%27)||%27%27,l=Array.from(e.querySelectorAll(%27[data-testid="tweetText"]%27)),s=l.map(e=>e.textContent).join(%27\n%27).trim(),i=window.location.pathname.split(%27/%27).pop(),o=e.querySelector(%27time%27),d=o?new Date(o.getAttribute(%27datetime%27)):null,c=d?d.toLocaleDateString(%27en-US%27,{month:%27short%27,day:%27numeric%27,year:%27numeric%27}):%27%27;let m=%27{{Tweet\n| name = %27+a+%27\n| username = %27+r+%27\n| text = %27+s+%27\n| date = %27+c+%27\n| ID = %27+i+%27\n| ref-name = Tweet_%27+i+%27\n}}%27;console.log(m)})(); | javascript:void(function(){const e=document.querySelector('article[tabindex="-1"]');if(!e){console.log('No tweet found on this page');return}const t=e.querySelector('[data-testid="User-Name"]'),n=t.getElementsByTagName('span'),a=n[0]?.textContent||%27%27,userLink=e.querySelector(%27a[href^="/"]:not([href*="status"])%27),r=userLink?userLink.getAttribute(%27href%27).replace(%27/%27,%27%27)||%27%27:t.querySelector(%27div[dir="ltr"]%27)?.textContent.replace(%27@%27,%27%27)||%27%27,l=Array.from(e.querySelectorAll(%27[data-testid="tweetText"]%27)),s=l.map(e=>e.textContent).join(%27\n%27).trim(),i=window.location.pathname.split(%27/%27).pop(),o=e.querySelector(%27time%27),d=o?new Date(o.getAttribute(%27datetime%27)):null,c=d?d.toLocaleDateString(%27en-US%27,{month:%27short%27,day:%27numeric%27,year:%27numeric%27}):%27%27;let m=%27{{Tweet\n| name = %27+a+%27\n| username = %27+r+%27\n| text = %27+s+%27\n| date = %27+c+%27\n| ID = %27+i+%27\n| ref-name = Tweet_%27+i+%27\n}}%27;console.log(m);const modal=document.createElement(%27div%27);modal.style.cssText=%27position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.7);z-index:9999;display:flex;align-items:center;justify-content:center;%27;const content=document.createElement(%27div%27);content.style.cssText=%27background:white;padding:20px;border-radius:5px;width:80%;max-width:600px;max-height:80%;overflow:auto;%27;const textarea=document.createElement(%27textarea%27);textarea.value=m;textarea.style.cssText=%27width:100%;height:200px;margin-bottom:10px;font-family:monospace;%27;textarea.onclick=function(){this.select()};const closeBtn=document.createElement(%27button%27);closeBtn.textContent=%27Close%27;closeBtn.style.cssText=%27padding:5px 10px;%27;closeBtn.onclick=function(){document.body.removeChild(modal)};const copyBtn=document.createElement(%27button%27);copyBtn.textContent=%27Copy to Clipboard%27;copyBtn.style.cssText=%27padding:5px 10px;margin-right:10px;%27;copyBtn.onclick=function(){textarea.select();document.execCommand(%27copy%27);this.textContent=%27Copied!%27;setTimeout(()=>{this.textContent=%27Copy to Clipboard%27},2000)};content.appendChild(textarea);content.appendChild(copyBtn);content.appendChild(closeBtn);modal.appendChild(content);document.body.appendChild(modal)})(); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 04:38, 22 February 2025
Bookmarklets in general are a bit out of fashion, but they're still quite useful. I'll add ones I like here.
Convert a Tweet into Wikitext Template[edit]
javascript:void(function(){const e=document.querySelector('article[tabindex="-1"]');if(!e){console.log('No tweet found on this page');return}const t=e.querySelector('[data-testid="User-Name"]'),n=t.getElementsByTagName('span'),a=n[0]?.textContent||%27%27,userLink=e.querySelector(%27a[href^="/"]:not([href*="status"])%27),r=userLink?userLink.getAttribute(%27href%27).replace(%27/%27,%27%27)||%27%27:t.querySelector(%27div[dir="ltr"]%27)?.textContent.replace(%27@%27,%27%27)||%27%27,l=Array.from(e.querySelectorAll(%27[data-testid="tweetText"]%27)),s=l.map(e=>e.textContent).join(%27\n%27).trim(),i=window.location.pathname.split(%27/%27).pop(),o=e.querySelector(%27time%27),d=o?new Date(o.getAttribute(%27datetime%27)):null,c=d?d.toLocaleDateString(%27en-US%27,{month:%27short%27,day:%27numeric%27,year:%27numeric%27}):%27%27;let m=%27{{Tweet\n| name = %27+a+%27\n| username = %27+r+%27\n| text = %27+s+%27\n| date = %27+c+%27\n| ID = %27+i+%27\n| ref-name = Tweet_%27+i+%27\n}}%27;console.log(m);const modal=document.createElement(%27div%27);modal.style.cssText=%27position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.7);z-index:9999;display:flex;align-items:center;justify-content:center;%27;const content=document.createElement(%27div%27);content.style.cssText=%27background:white;padding:20px;border-radius:5px;width:80%;max-width:600px;max-height:80%;overflow:auto;%27;const textarea=document.createElement(%27textarea%27);textarea.value=m;textarea.style.cssText=%27width:100%;height:200px;margin-bottom:10px;font-family:monospace;%27;textarea.onclick=function(){this.select()};const closeBtn=document.createElement(%27button%27);closeBtn.textContent=%27Close%27;closeBtn.style.cssText=%27padding:5px 10px;%27;closeBtn.onclick=function(){document.body.removeChild(modal)};const copyBtn=document.createElement(%27button%27);copyBtn.textContent=%27Copy to Clipboard%27;copyBtn.style.cssText=%27padding:5px 10px;margin-right:10px;%27;copyBtn.onclick=function(){textarea.select();document.execCommand(%27copy%27);this.textContent=%27Copied!%27;setTimeout(()=>{this.textContent=%27Copy to Clipboard%27},2000)};content.appendChild(textarea);content.appendChild(copyBtn);content.appendChild(closeBtn);modal.appendChild(content);document.body.appendChild(modal)})();
Using an LLM you can transform the code into something readable and then encode it back into a Bookmarklet if you'd like.