Extension:CopyLink

From mediawiki.org
MediaWiki extensions manual
CopyLink
Release status: unmaintained
Implementation Parser extension , Tag
Description Adds a <copylink [text="string"]> tag to copy text to clipboard
Author(s) (SmNtalk)
Latest version 1.0 (2012-04-24)
MediaWiki
Database changes No
License Creative Commons Attribution NonCommercial Share Alike 2.5
Download See below on this page
Example <copylink [text="string"]> some text </copylink>
  • <copylink>

The CopyLink extension provides a new tag <copylink> that allows user to click on text and have it copied to the system clipboard.

Tested on Internet Explorer and Firefox.

Usage[edit]

<copylink> some text </copylink>

or

<copylink [text="string"]> some text </copylink>

Installation[edit]

  • Copy the code into a file and place the file(s) in a directory called CopyLink in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    require_once "$IP/extensions/CopyLink/CopyLink.php";
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

CopyLink
<?php
/**
 * MediaWiki copylink extension
 * <copylink [text="string"]> some text </copylink>
 *
 * Based on the work of http://www.mediawiki.org/wiki/Extension:JSpoiler
 *
 * (C) Copyright 2012, SmN
 * This work is licensed under a Creative Commons Attribution-Noncommercial-Share
 * Alike 2.5 License.  Some rights reserved.
 * http://creativecommons.org/licenses/by-nc-sa/2.5/
 */


$wgExtensionFunctions[] = "wfCopylinkExtension";
$wgHooks['OutputPageBeforeHTML'][] = 'CopylinkParserHook' ;
$CopyLinkVersion = '1.0';
$wgExtensionCredits['parserhook'][] = array(
        'name'=>'CopyLink',
        'version'=>$CopyLinkVersion,
        'author'=>'sn',
        'url'=>'https://www.mediawiki.org/w/index.php?title=Extension:CopyLink',
        'description' => htmlentities('Adds a <copylink [text="string"]> tag')
);

function wfCopylinkExtension() {
        global $wgParser;
        // register the extension with the WikiText parser
        $wgParser->setHook( "copylink", "renderCopylink" );
}

function wfCopylinkJavaScript() {
        return  "<script language=\"JavaScript\">\n" .
                "function copy_clip(mytext)\n" .
                "{\n" .
                " if (window.clipboardData) \n" .
                "   {\n" .
                "   \n" .
                "   window.clipboardData.setData(\"Text\", mytext);\n" .
                "   }\n" .
                "   else if (window.netscape) \n" .
                "   { \n" .
                "   \n" .
                "   // you have to sign the code to enable this\n" .
                "   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');\n" .
                "   \n" .
                "   var clip = Components.classes['@mozilla.org/widget/clipboard;1']\n" .
                "                 .createInstance(Components.interfaces.nsIClipboard);\n" .
                "   if (!clip) return;\n" .
                "   \n" .
                "   var trans = Components.classes['@mozilla.org/widget/transferable;1']\n" .
                "                  .createInstance(Components.interfaces.nsITransferable);\n" .
                "   if (!trans) return;\n" .
                "   \n" .
                "   trans.addDataFlavor('text/unicode');\n" .
                "   \n" .
                "   var str = new Object();\n" .
                "   var len = new Object();\n" .
                "   \n" .
                "   var str = Components.classes[\"@mozilla.org/supports-string;1\"]\n" .
                "                .createInstance(Components.interfaces.nsISupportsString);\n" .
                "   \n" .
                "   var copytext=mytext;\n" .
                "   \n" .
                "   str.data=copytext;\n" .
                "   \n" .
                "   trans.setTransferData(\"text/unicode\",str,copytext.length*2);\n" .
                "   \n" .
                "   var clipid=Components.interfaces.nsIClipboard;\n" .
                "   \n" .
                "   if (!clip) return false;\n" .
                "   \n" .
                "   clip.setData(trans,null,clipid.kGlobalClipboard);\n" .
                "   \n" .
                "   }\n" .
                "   return false;\n" .
                "}\n" .
                "</script>\n";
}

function CopylinkParserHook( &$parser , &$text ) {
        $text = wfCopylinkJavaScript() . $text;
        return true;
}


// The callback function for converting the input text to HTML output
function renderCopylink( $input, $argv, $parser ) {
//        $outputObj = $parser->recursiveTagParse( $input, false );
        $output  = "<a href=\"#\" style='color:#CC0000;' onclick=\"return copy_clip('";

        if ( $argv["text"] <> "") {
          $output .= htmlspecialchars(str_replace("\\","\\\\", $argv["text"]));
        }
        else {
          $output .= htmlspecialchars(str_replace("\\","\\\\", $input));
          
        }
        $output .= "');\" onMouseOut=\"window.status='';return false;\" onMouseOver=\"window.status='Click to copy'; return true;\" >";
        $output .= htmlspecialchars($input) . "</a>";

        return $output;
}

See also[edit]