Extension:CopyLink

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
CopyLink

Release status: stable

Implementation Parser extension, Tag
Description Adds a <copylink [text="string"]> tag to copy text to clipboard
Author(s) (SmNtalk)
Latest version 1.0
License CC BY-NC-SA 2.5
Download No link
Example <copylink [text="string"]> some text </copylink>
Tags
<copylink>
Hooks used
OutputPageBeforeHTML

Translate the CopyLink extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

What can this extension do?[edit | edit source]

Creates a new tag <copylink> that allows user to click on text and have it copied to the system clipboard.
Tested on IE and FireFox

Usage[edit | edit source]

<copylink> some text </copylink>

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

Download instructions[edit | edit source]

Please copy and paste the code found below and place it in $IP/extensions/copylink.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit | edit source]

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/copylink.php");

Configuration parameters[edit | edit source]

There are no configuration parameters

Code[edit | edit source]

<?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'=>'http://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;
}