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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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
- [GreaseMonkey] Unity Forum Fixer
- UnityHub: Make Hub application background Translucent
- Customize SpriteShapeRenderer quality (but has issues)
- Editor tool: Copy selected gameobject’s names into clipboard as rows (for Excel)
- Editor tool: Replace string in selected gameobject’s names
- UnityHub: Enable built-in Login Dialog (no more browser login/logout issues!)
- Use TikTok-TTS in Unity (with WebRequest)
- Create Scene Thumbnail Image using OnSceneSaved & OnPreviewGUI
- webgl+javascript TTS
- Using Moonsharp (LUA) + Unity Webgl
- Using 3D gameobject prefabs with Unity Tilemap + NavMesh Surface
- Custom Unity Hub Project Template Preview Image/Video (using HTML+CSS in package description)
Recent Comments
- Vector3 maths for dummies! on
- UnityHub: Make Hub application background Translucent on
- UnityHub: Make Hub application background Translucent on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- Install Android SDK+JDK+NDK for Unity (without AndroidStudio or Unity Hub) on
- [Asset Store] Point Cloud Viewer & Tools on
- [Asset Store] Point Cloud Viewer & Tools on
- ffmpeg stream raw video into Unity Texture2D on
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