The forum of the forums
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Select content from codebox [phpBB3, PunBB]

3 posters

Go down

Select content from codebox [phpBB3, PunBB] Empty Select content from codebox [phpBB3, PunBB]

Post by Wizzard November 9th 2024, 9:28 am

As you might know this, by default all codeboxes do not have select all feature so if you want to select full content of it, you need to drag down which is tedious job. With this script you can add a button and select all of the content inside it.

Before click (follow the arrow where to click):
Select content from codebox [phpBB3, PunBB] Screen47

After the click:
Select content from codebox [phpBB3, PunBB] Screen48

How to implement this?

Go to AP > Modules > Javascript management and make a new script. Name it "Select all code in codebox" and for placement choose "In the topics".

Paste this code there and save:

phpBB3
Code:
$(document).ready(function() {
  // Iterate over all .codebox elements to ensure each has its own "Select all" functionality
  $("dl.codebox").each(function() {
    var dtElement = $(this).find("dt");
    
    // Create an anchor element for "Select all"
    var selectAllLink = $("<a>", {
      href: "#",
      text: "Select all",
      css: {
        "margin-left": "10px", // Add some space between "Code:" and "Select all"
        "color": "#a0947e",     // Set the text color
        "float": "right"        // Float the link to the right
      }
    });
    
    // Append the link to the <dt> element
    dtElement.append(selectAllLink);
    
    // Add event listener to handle the "Select all" functionality
    selectAllLink.on("click", function(e) {
      e.preventDefault(); // Prevent default link behavior
      
      // Select the content inside <code> tag within the current <dd>
      var codeElement = $(this).closest("dl.codebox").find("dd code").get(0); // Get the specific <code> tag in this .codebox
      
      // Create a range object
      var range = document.createRange();
      range.selectNodeContents(codeElement);
      
      // Get the current selection and clear any existing selections
      var selection = window.getSelection();
      selection.removeAllRanges();
      
      // Add the new range to the selection
      selection.addRange(range);
    });
  });
});

PunBB

Code:
// Function to select the code content
function selectCode(e) {  
    var doc = document,
        text = $(e).closest("dl").find(".cont_code, code").get(0),
        range, selection;

    if (doc.body.createTextRange) {
        range = document.body.createTextRange();
        range.moveToElementText(text);
        range.select();
    } else if (window.getSelection) {
        selection = window.getSelection();
        range = document.createRange();
        range.selectNodeContents(text);
        selection.removeAllRanges();
        selection.addRange(range);
    }
};

// Function to initialize the select button
$(function() {
    $("dl.codebox:not(.spoiler, .hidecode) > dd.code, dl.codebox:not(.spoiler, .hidecode) > dd > code")
        .closest("dl")
        .find('dt')
        .append('<span onClick="selectCode(this)" class="selectCode">Select Content</span>');
    
    // Add CSS for the .selectCode span
    const style = document.createElement('style');
    style.innerHTML = `
        span.selectCode {
            float: right;       /* Float to the right */
            margin-right: 20px; /* Space from the right */
            cursor: pointer;    /* Pointer cursor for the clickable span */
        }
    `;
    document.head.appendChild(style);
});

:yeahhh:
Wizzard
Wizzard
Forumember

Male Posts : 128
Reputation : 24
Language : English

https://net-cafe.forumotion.com/

Back to top Go down

Select content from codebox [phpBB3, PunBB] Empty Re: Select content from codebox [phpBB3, PunBB]

Post by Niko November 9th 2024, 12:44 pm

Niko
Niko
Helper
Helper

Male Posts : 3334
Reputation : 257
Language : English, Italian, French
Location : Italy

https://www.fmcodes.net/

skouliki, TonnyKamper, Jucarese and Wizzard like this post

Back to top Go down

Select content from codebox [phpBB3, PunBB] Empty Re: Select content from codebox [phpBB3, PunBB]

Post by Wizzard November 9th 2024, 2:26 pm

Niko wrote:There is already a tutorial for that :rose:

https://help.forumotion.com/t93456-select-content-button

Oh, I didn't know. Thanks.
Wizzard
Wizzard
Forumember

Male Posts : 128
Reputation : 24
Language : English

https://net-cafe.forumotion.com/

Back to top Go down

Select content from codebox [phpBB3, PunBB] Empty Re: Select content from codebox [phpBB3, PunBB]

Post by Ape November 9th 2024, 8:40 pm

As this is already a thing we will lock and trash this topic.
Topic moved to the garbage.
Please read our forum rules: ESF General Rules


Select content from codebox [phpBB3, PunBB] Left1212Select content from codebox [phpBB3, PunBB] Center11Select content from codebox [phpBB3, PunBB] Right112
Select content from codebox [phpBB3, PunBB] Ape_b110
Select content from codebox [phpBB3, PunBB] Ape1010
Ape
Ape
Administrator
Administrator

Male Posts : 19479
Reputation : 2013
Language : fluent in dork / mumbojumbo & English haha

http://chatworld.forumotion.co.uk/

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum