Jul
13
2017

Browser plugin: Add Copy-button to unity scripting docs

Using GreaseMonkey (Firefox) the script below will add Copy-to-clipboard-button into scripts at unity documentation website.
– Supports multiple scripts per page
– New: Adds copy button to forum code snippets also

*Best used with this unity editor plugin (so its 1 click to copy from docs, 1 click to paste into file in unity)
https://unitycoder.com/blog/2017/07/12/editor-plugin-paste-script-to-file/

GreaseMonkey Script Source


// ==UserScript==
// @name UnityDocsCopyScript
// @namespace https://unitycoder.com
// @description Adds copy-button to unity Docs script examples > https://unitycoder.com/blog/2017/07/13/browser-plugin-add-copy-button-to-unity-scripting-docs/
// @include https://docs.unity3d.com/ScriptReference/*
// @include https://forum.unity3d.com/threads/*
// @include https://forum.unity.com/threads/*
// @version 3
// @grant none
// ==/UserScript==
// add buttons to scripting docs
CreateCopyButtons("codeExampleCS");
// add buttons to forum code tags
CreateCopyButtons("code");
// button creation
function CreateCopyButtons(className)
{
var codeDivs = document.getElementsByClassName(className);
for(var d=0;d<codeDivs.length;d++)
{
var btn = document.createElement("BUTTON");
btn.innerHTML="&#x1f4cb;"; // unicode clipboard icon
btn.id = "::xcopyButton"+d;
btn.style.cssText = 'width:auto;height:26px;';
btn.targetDiv = codeDivs[d];
btn.addEventListener("click", CustomCopy);
btn.onclick = function(e){
CustomCopy(e);
return false;
};
codeDivs[d].parentNode.insertBefore(btn, codeDivs[d].parentNode.childNodes[0]);
}
}
// https://stackoverflow.com/a/30810322/5452781
function CustomCopy(e)
{
CustomSelectElementContents(e.target.targetDiv);
try
{
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
return false;
}
// https://stackoverflow.com/a/8024509/5452781
function CustomSelectElementContents(el) {
if (window.getSelection && document.createRange) {
// IE 9 and non-IE
var range = document.createRange();
range.selectNodeContents(el);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
} else if (document.body.createTextRange) {
// IE < 9
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.select();
}
}


// ==UserScript==
// @name UnityDocsCopyScript
// @namespace https://unitycoder.com
// @description Adds copy-button to unity Docs script examples > https://unitycoder.com/blog/2017/07/13/browser-plugin-add-copy-button-to-unity-scripting-docs/
// @include https://docs.unity3d.com/ScriptReference/*
// @include https://forum.unity3d.com/threads/*
// @include https://forum.unity.com/threads/*
// @include https://forum.unity.com/conversations/*
// @version 4
// @grant none
// ==/UserScript==
var addUrl=true; // adds current page url into clipboard text beginning
// add buttons to scripting docs
CreateCopyButtons("codeExampleCS");
// add buttons to forum code tags
CreateCopyButtons("code");
// button creation
function CreateCopyButtons(className)
{
var codeDivs = document.getElementsByClassName(className);
for(var d=0;d<codeDivs.length;d++)
{
var btn = document.createElement("BUTTON");
btn.innerHTML="&#x1f4cb;"; // unicode clipboard icon
btn.id = "::xcopyButton"+d;
btn.style.cssText = 'width:auto;height:26px;';
btn.targetDiv = codeDivs[d];
//btn.addEventListener("click", CustomCopy);
btn.setAttribute('type', 'button'); // disables Post behaviour from button
btn.onclick = function(e)
{
//CustomCopy(e); // old
CustomCopy2(e);
return false;
};
codeDivs[d].parentNode.insertBefore(btn, codeDivs[d].parentNode.childNodes[0]);
}
}
// new clipboard method, with trim
function CustomCopy2(e)
{
if (window.getSelection && document.createRange)
{
var range = document.createRange();
range.selectNodeContents(e.target.targetDiv);
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
// trim leading/trailing spaces from copied string
var src = sel.anchorNode.innerText.trim();
// if should include url
if (addUrl) src = "// "+window.location.href+"\n"+src;
// manually set clipboard data
navigator.clipboard.writeText(src);
}
}
// https://stackoverflow.com/a/30810322/5452781
function CustomCopy(e)
{
CustomSelectElementContents(e.target.targetDiv);
try
{
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('[UnityCopyCodeHelper] Copying text command was ' + msg);
} catch (err) {
console.log('[UnityCopyCodeHelper] Oops, unable to copy');
}
return false;
}
// https://stackoverflow.com/a/8024509/5452781
function CustomSelectElementContents(el)
{
if (window.getSelection && document.createRange)
{
// IE 9 and non-IE
var range = document.createRange();
range.selectNodeContents(el);
//var sel = window.getSelection().anchorNode.data.trim();
var sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
// TODO trim copied string!
//console.log(sel.toString());
//console.log(sel.anchorNode.innerText);
//window.getSelection().anchorNode.data.replace(/some pattern/, 'replace value');
} else if (document.body.createTextRange) {
// IE < 9
var textRange = document.body.createTextRange();
textRange.moveToElementText(el);
textRange.select();
}
}


1 Comment + Add Comment

Leave a comment

Connect

Twitter View LinkedIn profile Youtube Youtube Join Discord Twitch Instagram

UnityLauncherPro

Get UnityLauncherPRO and work faster with Unity Projects!
*free unity hub alternative

@unitycoder_com

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.