Navigation


 Online publications


 Go back to the forum

Free Codes   

Share post link

System32 | Published on the thu Dec 01, 2022 11:38 pm | 45 Views

Howdy, 

In case you want to share your post to some other website or in another topic, now you can do that with a simple quick on share icon! Stay with me, it's an easy job. 

Preview:

First, go to CP > Display > Templates > General > viewtopic_body. Find:

<li  >
{postrow.displayed.REPORT_IMG_NEW}
</li>

Just under that li element add another one with this code:

<li  >
<i  >share</i>
</li> 

Now, go to  CP > Display > CSS and add this style:

.share_post {
 cursor:pointer;
}

Last thing we need to do is to add javascript. Let's go to:
CP > Modules > Javascript and create a new script. Name it whatever you want ( I named it "Get post link" ) and then paste this code:

/*
Author: System32
Description: Script is made to get post URL, copy to clipboard and let user use it to share link
More tutorials at: https://freecodes.forumotion.me/publi?list=all
 
COPYRIGHT notice - Code can be shared and used only if Author is mentioned. Breaking of this rule will be reported
*/
 
 
$(document).ready(function() {
    $(".share_post").click(function(event) {
 
        var websiteUrl = window.location.hostname; // Get website URL
 
        var post = $(this).parent().parent().parent().parent().html().toString(); // Get full HTML of parent
        var start = post.indexOf("<a href"); // Get first occurrence of link
        var uneditedLink = post.substring(start, start + 50); // Get 50 more chars after start of the link
        var startLink = uneditedLink.indexOf("/"); // Get index of / position before taking post URL
        var endLink = uneditedLink.lastIndexOf('"'); // Get index of last occurrence of double quote
 
        var postLink = uneditedLink.substring(startLink, endLink); // Get post link
        var completeLink = websiteUrl + postLink; // Complete link to the post
   
        // Add text to clipboard (copy post link)
        navigator.clipboard.writeText(completeLink).then(() => {
 
       // Show text "Copied" once share link is clicked then fade in 1 second
            $("<span>" + "Copied" + "</span>").appendTo($(this)).fadeTo(1000, 1, function() {
                $(this).fadeTo(1000, 0, function() {
                    $(this).remove()
                });
            });
        });
        event.preventDefault();
    });
});


Cheers ;)

About the author