MediaWiki extensions manual
OOjs UI icon advanced-invert.svg
Release status: unmaintained
Implementation Parser extension , Skin
Description Adds a "Copy to clipboard"-Button to every <pre>-Tag
Author(s) Thomas Candrian (Thomascandriantalk)
Latest version 0.2 (2014-08-26)
MediaWiki 1.23.+
Database changes No
License Public domain
Download see below
Update for ZeroClipboard v2.1.6

The PreToClip extension adds a "Copy to clipboard"-Button to every <pre>-Tag even autogenerated ones. It works even with firefox.

This extension is using the excellent JavaScript and Flash from https://github.com/zeroclipboard/ZeroClipboard presented on https://zeroclipboard.github.io/


Needs ZeroClipboard from https://zeroclipboard.github.io/


1. Create Folder extensions/PreToClip/

2. Create Folder extensions/PreToClip/ZeroClipboard

3. Download ZeroClipboard from https://zeroclipboard.github.io/ and extract the content of the folder "dist" into the ZeroClipboard-Folder.

4. Store the file below to extensions/PreToClip/PreToClip.php

$wgExtensionCredits['other'][] = array (
	'name' => 'PreToClip',
	'version' => '0.1',
	'author' => 'Thomas Candrian, based on the work of Jon Rohan, James M. Greene',
        'url'    => 'https://www.mediawiki.org/wiki/Extension:PreToClip',
	'description' => htmlentities('Adds a copy to clipboard button to every <pre> tag')
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
$ZeroClipboardFilesDir = dirname($_SERVER["SCRIPT_NAME"]).'/extensions/PreToClip/ZeroClipboard';
$nlpObj = new PreToClip;
$wgHooks['BeforePageDisplay'][] = array ($nlpObj,'hPreToClip');
class PreToClip {
	var $completed;	
	function PreToClip() {
		$this->completed = false;
	function hPreToClip($out) {		
		if ($this->completed) {			
			return true;
		global $action;
		if ($action != 'view' and $action != '')
			return true;
		global $wgRequest;
		global $ZeroClipboardFilesDir;
		global $GLOBALS;
		$mBodytext = $out->mBodytext;
		$inhaltende = "";
		if (strpos($mBodytext, "<pre>") !== false) {			
			$inhaltende = utf8_decode($mBodytext);
			preg_match_all("/<pre>(.*)<\/pre>/siU", $inhaltende, $treffer);
			foreach ($treffer[0] as $key => $value) {
				$text1 = "<div style=\"text-align:right; margin-bottom:-37px;\"><button id=\"my-button".$key."\" data-clipboard-target=\"preid".$key."\" title=\"Click to copy to clipboard.\">Copy to Clipboard</button></div>\n";
				$text1 .= "<script language=\"JavaScript\">\n";
				$text1 .= "var clip".$key." = new ZeroClipboard( $('#my-button".$key."'));\n";
				$text1 .= "</script>\n";
				$text1 .= "<pre id=\"preid".$key."\">".$treffer[1][$key]."</pre>\n";
				$inhaltende = str_replace($treffer[0][$key], $text1, $inhaltende);
			$text2 = "<script type=\"text/JavaScript\" src=\"".$ZeroClipboardFilesDir."/ZeroClipboard.min.js\"></script>\n";
			$inhaltende = $text2 . $inhaltende;
		if ($inhaltende != '') {
			$inhaltende = utf8_encode($inhaltende);
		$this->completed = true;
		return true;

5. Add a call to it at the end of LocalSettings.php: