Extension:PreToClip

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
PreToClip
Release status: beta
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
Hooks used
BeforePageDisplay
Translate the PreToClip extension if it is available at translatewiki.net
Check usage and version matrix.

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/

Requirements[edit]

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

Installation[edit]

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

<?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;
		}
		//print_r($inhaltende);
		//die();		
		if ($inhaltende != '') {
			$inhaltende = utf8_encode($inhaltende);
			$out->clearHTML();
			$out->addHTML($inhaltende);
		}
		$this->completed = true;
		return true;
	}
}

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

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

if extension AutoExtensionLoader is installed or

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