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="📋"; // 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="📋"; // 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(); | |
| } | |
| } |
Related Posts
1 Comment + Add Comment
Leave a comment
Recent posts
- Adding Real World Location to Ice Fishing Prototype
- [web tool] Image Channel Inspector : AlphaScope
- Editor Tool: Smart Recompile (don’t recompile if edit comments)
- Editor Tool: Scene Size Analyzer
- LudumDare 60: Old Tech
- uBasic Scripting Language for Unity
- GreaseMonkey+Editor Plugin: 1-Click Copy script from ChatGPT (Web) to Unity Editor (as script)
- Editor Tool: Property Watcher
- .unityignore : Hide files from Project window
- PostBuild Script to Create Single exe Standalone File
- Game Loop Designer
- Favorites in PackageManager
Recent Comments
Coin:
CUgDSbRqFcAumDSAcdKDvuXsw26VdkJe8C8WGUQHBAGS
An article by












updated version, removes leading/trailing spaces from copied script, optionally adds current url as comment in the beginning of copy string
https://gist.github.com/unitycoder/8eed5fcfe3d186bf762d5fa152ce4b4b#file-unitydocscopyscript4-js