<?php
/**
* MyBB 1.8
* Copy PHP CODE
* Author: JLP423
* http://mybb.vn
*
* Ma Ni - unicode fix
*
**/
global $mybb, $templatelist;
if (isset($templatelist)) {
$templatelist .= ',copyphpcode_js';
}
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.");
}
function copyphpcode_info()
{
return array(
"name" => "Copy PHP CODE",
"description" => "Adds the ability to copy text to clipboard with a tooltip for [PHP],[CODE], and [QUOTE] blocks.",
"website" => "https://mybb.vn",
"author" => "JLP423",
"authorsite" => "https://mybb.vn",
"version" => "2.1",
"codename" => "copyphpcode",
"compatibility" => "18*"
);
}
function copyphpcode_install()
{
global $db, $mybb;
//Build settings
$query = $db->simple_select("settinggroups", "COUNT(*) as counts");
$dorder = $db->fetch_field($query, 'counts') + 1;
$groupid = $db->insert_query('settinggroups', array(
'name' => 'copyphpcode',
'title' => 'Copy PHP CODE',
'description' => 'Adds the ability to copy text to clipboard with a tooltip for [PHP],[CODE], and [QUOTE] blocks.',
'disporder' => $dorder,
'isdefault' => '0'
));
$dorder_set = 0;
$new_setting[] = array(
'name' => 'copyphpcode_button_text',
'title' => 'Button Text',
'description' => 'The text you want for your copy button',
'optionscode' => 'text',
'value' => 'Copy',
'disporder' => ++$dorder_set,
'gid' => $groupid
);
$new_setting[] = array(
'name' => 'copyphpcode_button_style',
'title' => 'Button Style',
'description' => 'Stylize your copy button',
'optionscode' => 'textarea',
'value' => '
background-color: #333;
color: #0078d7;
padding: 6px 12px; /* Adjust padding to make the tooltip shorter */
font-size: 14px; /* Adjust font size */
border-radius: 4px;
max-width: 200px; /* Limit the width of the tooltip */;
',
'disporder' => ++$dorder_set,
'gid' => $groupid
);
$new_setting[] = array(
'name' => 'copyphpcode_tooltip_text',
'title' => 'Tooltip Text',
'description' => 'The text you want to show as tool tip',
'optionscode' => 'text',
'value' => 'Copied!',
'disporder' => ++$dorder_set,
'gid' => $groupid
);
$new_setting[] = array(
'name' => 'copyphpcode_tooltip_style',
'title' => 'Tooltip Style',
'description' => 'Stylize your tooltip',
'optionscode' => 'textarea',
'value' => '
position: fixed;
background-color: #333;
color: #ff00ff;
padding: 6px 12px; /* Adjust padding to make the tooltip shorter */
font-size: 14px; /* Adjust font size */
border-radius: 4px;
bottom: 20px;
margin-bottom: 3px;
left: 50%;
transform: translateX(-50%);
max-width: 200px; /* Limit the width of the tooltip */;
//Build templates
$template = array();
$template['copyphpcode_js'] = '<script type="text/javascript">document.addEventListener(\'DOMContentLoaded\', function() {
async function copyTextToClipboard(textToCopy) {
try {
// Bereinige den Text
const sanitizedText = textToCopy.replace(/\\u00a0/g, \' \').replace(/\\r?\\n/g, \'\\n\');
if (navigator?.clipboard?.writeText) {
// Use Clipboard API if available
await navigator.clipboard.writeText(sanitizedText);
console.log(\'Text copied to clipboard using Clipboard API:\', sanitizedText);
showTooltip(\'Text copied to clipboard\');
} else if (document.execCommand) {
// Fallback to document.execCommand(\'copy\') method
const textArea = document.createElement(\'textarea\');
textArea.value = sanitizedText;
document.body.appendChild(textArea);
textArea.select();
document.execCommand(\'copy\');
document.body.removeChild(textArea);
console.log(\'Text copied to clipboard using document.execCommand:\', sanitizedText);
showTooltip(\'Text copied to clipboard\');
} else {
throw new Error(\'Clipboard API and document.execCommand are not supported in this browser.\');
}
} catch (err) {
console.error(\'Failed to copy text: \', err);
showTooltip(\'Failed to copy text\');
}
}
document.body.appendChild(tooltip);
setTimeout(() => {
tooltip.remove();
}, 500); // Remove the tooltip after 0.5 seconds
}
// Get references to all elements with class "copyButton"
const copyButtons = document.querySelectorAll(\'.copyButton\');
const buttons = document.querySelectorAll(\'.copyButton\');
buttons.forEach(button => {
button.textContent = "{$button_text}";
});