MyBB Community Forums

Full Version: select all text in custom mycode
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a mycode to create a syntax higlhighter codebox.
some examples here


These boxes are custom mycodes 
regex:
\[python\](.*?)\[/python\]
replacement:
<pre class="brush: python">$1</pre>


As is you can select all text inside by double clicking the text. But i would like a link to select all text in addition. There is a plugin to do this exact thing via javascript.....but its for mybb default codeboxes here

Its a single plugin file, quite simple, i just cant figure out how to convert this to to my mycode instead of the default codebox
<?php
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$plugins->add_hook("showthread_start","SAICAP");

function SelectAllinCode_info()
{
	return array(
		"name"			=> "Select All In Code's",
		"description"	=> "Select all in Code and PHP",
		"website"		=> "http://mybb-es.com",
		"author"		=> "Edson Ordaz",
		"authorsite"	=> "mailto:[email protected]",
		"version"		=> "1.0",
		"compatibility" => "16*",
		"guid"			=> "608cb4086667cdd6d0d3ba103991c309"
	);
}

function SelectAllinCode_activate()
{
}

function SelectAllinCode_deactivate()
{
}

function SAICAP()
{
	global $headerinclude;
	$headerinclude .= "<script type=\"text/javascript\">
function selectCode(a)
{
   var e = a.parentNode.parentNode.getElementsByTagName('CODE')[0];
   if (window.getSelection)
   {
      var s = window.getSelection();
       if (s.setBaseAndExtent)
      {
         s.setBaseAndExtent(e, 0, e, e.innerText.length - 1);
      }
      else
      {
         var r = document.createRange();
         r.selectNodeContents(e);
         s.removeAllRanges();
         s.addRange(r);
      }
   }
   else if (document.getSelection)
   {
      var s = document.getSelection();
      var r = document.createRange();
      r.selectNodeContents(e);
      s.removeAllRanges();
      s.addRange(r);
   }
   else if (document.selection)
   {
      var r = document.body.createTextRange();
      r.moveToElementText(e);
      r.select();
   }
}
</script>";
	global $lang;
	$lang->load("global", false, true);
	$lang->php_code .= " <a href=# onclick=\"selectCode(this); return false;\">(Select All)</a>";
	$lang->code .= " <a href=# onclick=\"selectCode(this); return false;\">(Select All)</a>";
}
?>

I tried changing "CODE" to the pre tag that i am using 
   var e = a.parentNode.parentNode.getElementsByTagName('PRE')[0];

but that doesnt seem to work. It seems like this plugin is small enough that even someone that doesnt know javascript would be able to finagle it to work the way they wanted....hopefully.

EDIT:
I tried changing my replacement for mycode to this instead in hope that it would work. Teh script is in my header inlcudes as i installed that script. 
<b>Code:</b> <a href=# onclick="selectCode(this); return false;">(Select All)</a>

<pre class="brush: python">$1</pre>
EDIT2:
Then i tried adding a php to echo $lang->code but it keeps showing code}; ?> 
<?php define("IN_MYBB",1);include("global.php");echo {$lang->code}; ?>
<b>Code:</b> <a href=# onclick="selectCode(this); return false;">(Select All)</a>
<pre class="brush: python">$1</pre>