Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active May 20, 2021 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unitycoder/8eed5fcfe3d186bf762d5fa152ce4b4b to your computer and use it in GitHub Desktop.
Save unitycoder/8eed5fcfe3d186bf762d5fa152ce4b4b to your computer and use it in GitHub Desktop.
UnityDocsCopyScript (GreaseMonkey)
// ==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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment