{"id":58336,"date":"2023-02-13T03:01:44","date_gmt":"2023-02-12T17:01:44","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=58336"},"modified":"2023-02-13T14:46:49","modified_gmt":"2023-02-13T04:46:49","slug":"wordpress-blog-code-element-clipboard-copy-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wordpress-blog-code-element-clipboard-copy-primer-tutorial\/","title":{"rendered":"WordPress Blog Code Element Clipboard Copy Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/code_copy.gif\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"WordPress Blog Code Element Clipboard Copy Primer Tutorial\" src=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/code_copy.gif\" title=\"WordPress Blog Code Element Clipboard Copy Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">WordPress Blog Code Element Clipboard Copy Primer Tutorial<\/p><\/div>\n<p>In the WordPress Blog you are reading, we have <a target=_blank title='HTML code element' href='https:\/\/www.w3schools.com\/tags\/tag_code.asp'>&#8220;code&#8221;<\/a> HTML elements that work well, except for the larger ones, and the user trying to copy into the clipboard, the entire &#8220;code&#8221; element content.<\/p>\n<p>We wanted to help out here, by overlaying a new &amp;#128203; clipboard &#128203; emoji button to the top right of any &#8220;code&#8221; element, that when you click we copy into the user&#8217;s clipboard an unadorned (ie. no colour coding) version of the &#8220;code&#8221; element content.<\/p>\n<p>In conjunction to a new body <font color=blue>onload event call<\/font> &#8230;<\/p>\n<p>&lt;?php echo &#8221;<br \/>\n<code><br \/>\n&lt;body onload=\" setTimeout(frnx,2000); if (onlok) { ahashlook(); setInterval(prehideIt, 3000); pp_ref(('' + document.URL), ('' + document.referrer)); if (1 == 1) { lookforbincode(); } lookforhighlight();  postcalendar(); changeasfordownload();  if (cafd == cafd) { cafd=0; } else { cafd=true; }  checkonl(); setTimeout(initpostedoncc, 3000); widgetcon(); precc(); courseCookies();  cookie_fonts(); is_mentioned_by(); calendar_pass(); prejustshow(); details_summary(0); pre_last_email_check(); maybevp(); setTimeout(last_email_check,15000); } if (navigator.userAgent.match(\/Android|BlackBerry|iPhone|iPod|Opera Mini|IEMobile\/i)) { setTimeout(bylinedo,5000); } else { bylinedo(); } setTimeout(newlpcheck, 3000); <font color=blue>codecopying('');<\/font> \" &lt;?php body_class(); ?&gt;&gt;<br \/>\n<\/code><br \/>\n&#8220;; ?&gt;<\/p>\n<p> &#8230; and new div element &#8230;<\/p>\n<p>&lt;?php echo &#8221;<br \/>\n<code><br \/>\n&lt;div id=dcode&gt;&lt;\/div&gt;<br \/>\n<\/code><br \/>\n&#8220;; ?&gt;<\/p>\n<p> &#8230; we have new TwentyTen theme header.php Javascript logic &#8230;<\/p>\n<p>&lt;?php echo &#8221;<br \/>\n<code><br \/>\nfunction fallbackCopyTextToClipboard(text) { \/\/ thanks to <a target=_blank title='https:\/\/stackoverflow.com\/questions\/400212\/how-do-i-copy-to-the-clipboard-in-javascript' href='https:\/\/stackoverflow.com\/questions\/400212\/how-do-i-copy-to-the-clipboard-in-javascript'>https:\/\/stackoverflow.com\/questions\/400212\/how-do-i-copy-to-the-clipboard-in-javascript<\/a><br \/>\n  var textArea = document.createElement(\"textarea\");<br \/>\n  \/\/alert(text);<br \/>\n  textArea.value = text;<br \/>\n<br \/>\n  \/\/ Avoid scrolling to bottom<br \/>\n  textArea.style.top = \"0\";<br \/>\n  textArea.style.left = \"0\";<br \/>\n  textArea.style.position = \"fixed\";<br \/>\n<br \/>\n  document.body.appendChild(textArea);<br \/>\n  textArea.focus();<br \/>\n  textArea.select();<br \/>\n<br \/>\n  try {<br \/>\n    var successful = document.execCommand('copy');<br \/>\n    var msg = successful ? 'successful' : 'unsuccessful';<br \/>\n    console.log('Fallback: Copying text command was ' + msg);<br \/>\n  } catch (err) {<br \/>\n    console.error('Fallback: Oops, unable to copy', err);<br \/>\n  }<br \/>\n<br \/>\n  document.body.removeChild(textArea);<br \/>\n}<br \/>\n<br \/>\nfunction copyTextToClipboard(text) { \/\/ thanks to <a target=_blank title='https:\/\/stackoverflow.com\/questions\/400212\/how-do-i-copy-to-the-clipboard-in-javascript' href='https:\/\/stackoverflow.com\/questions\/400212\/how-do-i-copy-to-the-clipboard-in-javascript'>https:\/\/stackoverflow.com\/questions\/400212\/how-do-i-copy-to-the-clipboard-in-javascript<\/a><br \/>\n  \/\/alert('text=' + text);<br \/>\n  if (!navigator.clipboard) {<br \/>\n    fallbackCopyTextToClipboard(text);<br \/>\n    return;<br \/>\n  }<br \/>\n  navigator.clipboard.writeText(text).then(function() {<br \/>\n    console.log('Async: Copying to clipboard was successful!');<br \/>\n  }, function(err) {<br \/>\n    console.error('Async: Could not copy text: ', err);<br \/>\n  });<br \/>\n}<br \/>\n<br \/>\nfunction ifotherthanfont(cii) {<br \/>\n  var newcii=cii, allokay=true, ncs=[], incs=0;<br \/>\n  if (cii.indexOf('&lt;\/font&gt;') != -1 || cii.indexOf('&lt;\/FONT&gt;') != -1) {<br \/>\n    ncs=cii.split('&lt;\/');<br \/>\n    for (incs=1; incs&lt;ncs.length; incs++) {<br \/>\n      if (ncs[incs].toLowerCase().indexOf('font') != 0) { allokay=false;  }<br \/>\n    }<br \/>\n    if (!allokay || 1 == 1) {<br \/>\n    ncs=cii.split('&lt;font');<br \/>\n    for (incs=1; incs&lt;ncs.length; incs++) {<br \/>\n      newcii=newcii.replace('&lt;font' + ncs[incs].split('&gt;')[0] + '&gt;', '');<br \/>\n      newcii=newcii.replace('&lt;\/font&gt;', '');<br \/>\n    }<br \/>\n    ncs=cii.split('&lt;FONT');<br \/>\n    for (incs=1; incs&lt;ncs.length; incs++) {<br \/>\n      newcii=newcii.replace('&lt;FONT' + ncs[incs].split('&gt;')[0] + '&gt;', '');<br \/>\n      newcii=newcii.replace('&lt;\/FONT&gt;', '');<br \/>\n    }<br \/>\n    }<br \/>\n  }<br \/>\n  return newcii;<br \/>\n}<br \/>\n<br \/>\nfunction codecopying(mode) {<br \/>\n  var rectcos=null, icn=0, okpast=false;<br \/>\n  var cos=document.getElementsByTagName('code');<br \/>\n  for (var ico=0; ico&lt;cos.length; ico++) {<br \/>\n    if (mode == '') {<br \/>\n     rectcos=cos[ico].getBoundingClientRect();<br \/>\n     document.getElementById('dcode').innerHTML+='&lt;span data-fword=\"' + encodeURIComponent(ifotherthanfont(cos[ico].innerHTML).substring(0,100)) + '\" onclick=\"codecopying(this.id);\" title=\"Copy unadorned code to clipboard buffer\" id=scd' + ico + ' style=\"position:absolute;z-index:56;left:' + eval(-50 + eval('' + rectcos.right)) + 'px;top:' + eval(10 + eval('' + rectcos.top)) + 'px;font-size:24px;\"&gt;&#128203;&lt;\/span&gt;';<br \/>\n    } else if (okpast && encodeURIComponent(ifotherthanfont(cos[ico].innerHTML).substring(0,100)) == document.getElementById(mode).getAttribute('data-fword')) {<br \/>\n      document.getElementById(mode).style.border='1px dashed pink';<br \/>\n      copyTextToClipboard(ifotherthanfont(cos[ico].innerHTML).replace(\/\\&amp\\;nbsp\\;\/g, '&nbsp;').replace(\/\\&amp\\;\\#\/g, '&#').replace(\/ \\&amp\\;\\;\/g, ' &;').replace(\/\\&lt;br\\&gt;\/g, '').replace(\/\\&lt\\;\/g, '&lt;').replace(\/\\&gt\\;\/g, '&gt;').trim());<br \/>\n      okpast=false;<br \/>\n    } else if (mode == ('scd' + ico)) { \/\/ Thanks to https:\/\/www.freecodecamp.org\/news\/copy-text-to-clipboard-javascript\/<br \/>\n     if (encodeURIComponent(ifotherthanfont(cos[ico].innerHTML).substring(0,100)) != document.getElementById(mode).getAttribute('data-fword')) {<br \/>\n      okpast=true;<br \/>\n     } else {<br \/>\n      document.getElementById(mode).style.border='1px dashed pink';<br \/>\n      copyTextToClipboard(ifotherthanfont(cos[ico].innerHTML).replace(\/\\&amp\\;nbsp\\;\/g, '&nbsp;').replace(\/\\&amp\\;\\#\/g, '&#').replace(\/ \\&amp\\;\\;\/g, ' &;').replace(\/\\&lt;br\\&gt;\/g, '').replace(\/\\&lt\\;\/g, '&lt;').replace(\/\\&gt\\;\/g, '&gt;').trim());<br \/>\n     }<br \/>\n    }<br \/>\n  }<br \/>\n}<br \/>\n<\/code><br \/>\n&#8220;; ?&gt;<\/p>\n<p> &#8230; you might want to try just above, here.<\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d58336' onclick='var dv=document.getElementById(\"d58336\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/emoji\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d58336' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In the WordPress Blog you are reading, we have &#8220;code&#8221; HTML elements that work well, except for the larger ones, and the user trying to copy into the clipboard, the entire &#8220;code&#8221; element content. We wanted to help out here, &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/wordpress-blog-code-element-clipboard-copy-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,14,37],"tags":[151,174,214,385,3606,2761,652,2189,861,870,894,932,997,1319,1324,1325,1456],"class_list":["post-58336","post","type-post","status-publish","format-standard","hentry","category-elearning","category-event-driven-programming","category-tutorials","tag-blog","tag-button","tag-clipboard","tag-emoji","tag-emoji-button","tag-header-php","tag-javascript","tag-navigator","tag-onclick","tag-onload","tag-overlay","tag-php","tag-programming","tag-tutorial","tag-twentyten","tag-twentyten-theme","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/58336"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=58336"}],"version-history":[{"count":7,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/58336\/revisions"}],"predecessor-version":[{"id":58349,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/58336\/revisions\/58349"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=58336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=58336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=58336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}