Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:49:15+00:00 2026-06-08T20:49:15+00:00

I am writing a script to append attribution linking to a user’s clipboard —

  • 0

I am writing a script to append attribution linking to a user’s clipboard — similar to the functionality that you see from Tynt.

Functionally, the script below is working fine, except that it is removing line breaks and formatting. Is there anyway to preserve — at the very least — the line breaks?

function addLinktoCopy() {

// Define tracking parameter
var trackingparam = "source=copypaste";

// Set document body as body_element variable
var body_element = document.getElementsByTagName('body')[0];

// Create a selection variable to store copied value
var selection;

// Populate selection variable with user's selected copy
selection = window.getSelection();

// Create a variable to store the selection length
var selectionlength;

// Convert the selection to a string and get the string's length
selectionlength = selection.toString().length

// If the selectionlength is >34, then append tracking
if ( selectionlength > 34 ){

    // Set the current page's URL as a variable
    var page = document.location.href;

    // Create a seperator variable
    var seperator;

    // Create a locationURL variable to store the URL and associated tracking parameters
    var locationURL;

    // Check to see if the URL already contains the tracking paramater
    // If the URL doesn't contain the tracking code, append it to the end
    if ( page.indexOf(trackingparam) == -1 ) {

        // Check to see if the URL already includes a ? in it
        if ( page.indexOf("?") != -1 ) {

            // If ? is present in the URL string, set seperator variable to &
            seperator = "&";        

        }

        else {

            // If ? is not present in the URL string, set seperator variable to ?
            seperator = "?";

        }

        // Set locationURL variable with URL + tracking code
        locationURL = document.location.href + seperator + trackingparam;

    }

    // Othwerise, the tracking code already exists in the URL string, so append nothing
    else {

        // Set the locationURL variable with the URL as is      
        locationURL = document.location.href;

    }

    // Build link to page with editorial copy, URL, seperator, and URL tracking parameters
    var pagelink = "<br/><br/>Read more at: "+ locationURL;

    // Create new variable with user's selection and page link
    var copytext = selection + pagelink;

    // Create a new div and add associated styling and hide it off the page
    var newdiv = document.createElement('div');

    // Append new div to document
    body_element.appendChild(newdiv);

    // Select text from the new (hidden) div
    newdiv.innerHTML = copytext;

    // Replace clipboard values with new selection + link value
    selection.selectAllChildren(newdiv);    

}

// Otherwise, selectionlength <= 34, so do nothing
// We are not appending any copy or URLs to the user's selection

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:49:17+00:00Added an answer on June 8, 2026 at 8:49 pm

    After a bit of research, I figured out what was going on here, and made a fix to preserve the line breaks. It looks like when copying the selection, line breaks were being represented as \n. So, I added some logic to replace all \n instances with an HTML break
    , and this resolved the issue. Here’s the updated script in its entirety.

    function addLinktoCopy() {
    
    // Define tracking parameter
    var trackingparam = "source=copypaste";
    
    // Set document body as body_element variable
    var body_element = document.getElementsByTagName('body')[0];
    
    // Create a selection variable to store copied value
    var selection;
    
    // Create a selection to store the selection as a text string
    var selectedText;
    
    // Populate selection variable with user's selected copy
    selection = window.getSelection();
    
    // Populate selectedText variable with the user's selected copy stored as a string
    selectedText = selection.toString(); 
    
    // Create a variable to store the selection length
    var selectionlength;
    
    // Convert the selection to a string and get the string's length
    selectionlength = selection.toString().length
    
    // If the selectionlength is >34 and doesn't start with "http://", then append tracking
    // If the selection starts with "http://", it's likely a URL, which could provide for a bad experience when pasting into an address bar
    if ( (selectionlength > 34) && (selectedText.substring(0,7) != "http://") ){
    
        // Set the current page's URL as a variable
        var page = document.location.href;
    
        // Create a seperator variable
        var seperator;
    
        // Create a locationURL variable to store the URL and associated tracking parameters
        var locationURL;
    
        // Check to see if the URL already contains the tracking paramater
        // If the URL doesn't contain the tracking code, append it to the end
        if ( page.indexOf(trackingparam) == -1 ) {
    
            // Check to see if the URL already includes a ? in it
            if ( page.indexOf("?") != -1 ) {
    
                // If ? is present in the URL string, set seperator variable to &
                seperator = "&";        
    
            }
    
            else {
    
                // If ? is not present in the URL string, set seperator variable to ?
                seperator = "?";
    
            }
    
            // Set locationURL variable with URL + tracking code
            locationURL = document.location.href + seperator + trackingparam;
    
        }
    
        // Othwerise, the tracking code already exists in the URL string, so append nothing
        else {
    
            // Set the locationURL variable with the URL as is      
            locationURL = document.location.href;
    
        }
    
        // Build link to page with editorial copy, URL, seperator, and URL tracking parameters
        var pagelink = "<br/><br/>Read more at: "+ locationURL;
    
        // Create new variable with user's selection and page link
        var copytext = selection + pagelink;
    
        // Replace line breaks /n with HTML break tags to quasi-preserve formatting on paste
        var copytext = copytext.replace(/\n/g, '<br />');
    
        // Create a new div and add associated styling and hide it off the page
        var newdiv = document.createElement("div");
    
        // Append new div to document
        body_element.appendChild(newdiv);
    
        // Select text from the new (hidden) div
        newdiv.innerHTML = copytext;
    
        // Replace clipboard values with new selection + link value
        selection.selectAllChildren(newdiv);
    
    }
    
    // Otherwise, selectionlength <= 34, so do nothing
    // We are not appending any copy or URLs to the user's selection
    
    }
    
    // Execute addLinktoCopy function when user copies text from page
    document.oncopy = addLinktoCopy;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Powershell script that will extract a set of data files from
im writing a script to get services from local and remote machines. I've had
I am currently writing a script which parses the ServiceTage, Computername and Username from
I'm writing a script that will constantly scan iTunes for new dialog boxes and
Writing a bash script, and I want to get user input. Awesome, read -p
I'm writing an import script that processes a file that has potentially hundreds of
I am writing a script that reads multiple twitter feeds and writes them to
I am writing a script that will allow me to search torrent sites. On
I'm writing a script in Python that will spit out some data organized as
I am writing a python script that will parse through a file quickly by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.