Jump to content
Wikimedia Meta-Wiki

User:Suyash.dwivedi/userscripts/CopytheshortUrlLink.js

From Meta, a Wikimedia project coordination wiki
This is an archived version of this page, as edited by Suyash.dwivedi (talk | contribs) at 19:40, 21 February 2025 (Created page with "/* This is a userscript to generate a short URL from MetaWiki and provide a link in front of the title on the local wiki. @Author User:Jayprakash12345, User:Suyash.dwivedi @OwnBy meta:Indic-TechCom ## Features: - Generates a **short URL** for the current wiki page using MetaWiki. - Adds a **Copy Button** next to the short URL. - Clicking the **Copy Button** automatically copies the short URL to the clipboard. - Displays a..."). It may differ significantly from the current version .
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
 /*
  This is a userscript to generate a short URL from MetaWiki
  and provide a link in front of the title on the local wiki.

  @Author [[User:Jayprakash12345]], [[User:Suyash.dwivedi]]
  @OwnBy [[meta:Indic-TechCom]]

  ## Features:
  - Generates a **short URL** for the current wiki page using MetaWiki.
  - Adds a **Copy Button** next to the short URL.
  - Clicking the **Copy Button** automatically copies the short URL to the clipboard.
  - Displays a **"Copied!"** message for confirmation.
 */

 $(function(){
 if(mw.config.get("wgArticleId")===0||mw.config.get("wgAction")!=="view"){
 return;
 }

 $.when(mw.loader.using(['mediawiki.util','mediawiki.ForeignApi']),$.ready).then(function(){
 newmw.ForeignApi('https://meta.wikimedia.org/w/api.php').post({
 action:'shortenurl',
 url:location.href
 }).done(function(data){
 varshortUrl=data.shortenurl.shorturl;

 // Create the tooltip container
 varurlContainer=$("<span>").css({
 "position":"absolute",
 "display":"inline-block",
 "z-index":"11111",
 "min-width":"max-content",
 "background-color":"black",
 "color":"#fff",
 "text-align":"center",
 "padding":"5px",
 "border-radius":"6px",
 "top":"-40%",
 "font-size":"small",
 "left":"90%"
 }).text("Short URL: "+shortUrl);

 // Create copy button
 varcopyButton=$("<button>").text("Copy").css({
 "margin-left":"5px",
 "cursor":"pointer",
 "background":"#007bff",
 "border":"none",
 "color":"white",
 "padding":"2px 6px",
 "border-radius":"4px",
 "font-size":"small"
 });

 // Copy functionality
 copyButton.click(function(){
 navigator.clipboard.writeText(shortUrl).then(function(){
 copyButton.text("Copied!");
 setTimeout(function(){
 copyButton.text("Copy");
 },2000);
 });
 });

 // Append copy button
 urlContainer.append(copyButton);

 // Create share icon
 varicon=$("<span>").addClass("shareIcon").append(urlContainer.hide());

 mw.util.addCSS('.shareIcon { display: inline-block; width: 25px; height: 18px; background-repeat: no-repeat; background-size: 18px;position: relative;background-position: left; margin: 0 0 0 6px; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAwCAQAAAD9CzEMAAAABGdBTUEAALGPC/xhBQAAACBjSFJNAAB6JgAAgIQAAPoAAACA6AAAdTAAAOpgAAA6mAAAF3CculE8AAAAAmJLR0QA/4ePzL8AAAAHdElNRQfhCAkVKSrZpFFqAAABu0lEQVRYw+2XTS8DURSGnxIzTSTWBEsWYqtd4AdgbcvOd4sEa3/BVpoOvwG/ANXSxEfSRG0kEqykaNMIaceCNHOndxJxT4PEmd3N5Hkn57zn7S3819+vMMtkKVMmyxJhaXw3F7ie55wu2a9X8S4uZ9hyAot1eBeXmJzAsVYgLSdQ0goUva80GQm8aU+rUgJRFVWrnERzOtiiqm2Qy5wp3GKN5wC4SxbLDD/GVQ1WIelzUpYOE3gvex7YEQNAC9Mc8EiBfWZo+T68lXVeavA7pgwdqFSICe5r8Fc2aPs+zCZOmhIljpjHAqJkPI3ZocfkWzs5U4Z2otjxkhGzVtg+vPd5YsXUgBAPgFdI0i4xzIwW/0BEyi1FrUDBBCno5a8I5ALiV6xFsUYP2ea0sTaFTp+Eumh500X7SPsFUhQpkWIWC4go9t01i4qgsBvnRirsfiSu9T846U/7DpMgRYoEwxIio+QV+zqKKRyJi6PFKk+BdnZkmtWOE3htGZKaSIRrrcCmVNgdc6s975dL02btqSsnkGvc3ZTPDdDNYFBy9Zw6fFJ2t22fRNK/aCEBkSEm6QNybHP4/7/+99U7rhtw2QbeFFoAAAAldEVYdGRhdGU6Y3JlYXRlADIwMTctMDgtMDlUMjE6NDE6NDIrMDA6MDC7TGiTAAAAJXRFWHRkYXRlOm1vZGlmeQAyMDE3LTA4LTA5VDIxOjQxOjQyKzAwOjAwyhHQLwAAAABJRU5ErkJggg==); }');

 // Append the element in DOM
 varcurrentSkin=mw.config.get("skin");
 if(currentSkin==="vector"){
 $('#firstHeading').append(icon);
 }elseif(currentSkin==="minerva"){
 $('#section_0').append(icon);
 }

 // Show on hover
 icon.mouseover(function(){
 urlContainer.show();
 });

 icon.mouseleave(function(){
 urlContainer.fadeOut(3000);
 });
 });
 });
 });

AltStyle によって変換されたページ (->オリジナル) /