Skip to content

Instantly share code, notes, and snippets.

@Ceda
Last active August 19, 2020 06:53
Show Gist options
  • Save Ceda/8189516 to your computer and use it in GitHub Desktop.
Save Ceda/8189516 to your computer and use it in GitHub Desktop.
Greasemonkey cms textarea code highlight
// ==UserScript==
// @name Blueberry CMS
// @namespace http://blueberry.cz/user/ceda
// @description Highlights the HTML syntax for Textareas in block editor
// @include http://cms.blueberry.cz/*
// @require http://codemirror.net/lib/codemirror.js
// @require http://codemirror.net/addon/fold/foldcode.js
// @require http://codemirror.net/addon/fold/foldgutter.js
// @require http://codemirror.net/addon/fold/xml-fold.js
// @require http://codemirror.net/mode/xml/xml.js
// @require http://codemirror.net/mode/htmlmixed/htmlmixed.js
// @require http://codemirror.net/addon/selection/active-line.js
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js
// @version 0.1
// @grant GM_addStyle
// @grant GM_getResourceText
// ==/UserScript==
/**
* This function improves the GM_AddStyle function, enabling to add external CSS stylesheets, like the CodeMirror one.
* Modified version of kaskus-code-mirror UserScript http://userscripts.org/scripts/show/156308 (removed the gvar variable).
**/
var GM_addGlobalStyle = function (a, b, c) {
//alert("entering add global style");
var d, e;
if (a.match(/^http?:\/\/.+/)) {
d = createEl("link", { type: "text/css", rel:'stylesheet', href:a });
//alert("element = " + d.toString());
}else{
d = createEl("style", { type: "text/css" });
d.appendChild(createTextEl(a));
}
if (isDefined(b) && isString(b)) d.setAttribute("id", b);
if (isDefined(c) && c) {
document.body.insertBefore(d, document.body.firstChild)
} else {
e = document.getElementsByTagName("head");
//alert("element = " + e.toString());
/*if (isDefined(e[0]) && e[0].nodeName == "HEAD") gvar.$w.setTimeout(function () {
e[0].appendChild(d)
}, 100);
else */
document.body.insertBefore(d, document.body.firstChild)
}
return d
};
//=== mini-functions
//Taken from kaskus-code-mirror UserScript http://userscripts.org/scripts/show/80409
// static routine
function isDefined(x) { return !(x == null && x !== null); }
function isUndefined(x) { return x == null && x !== null; }
function isString(x) { return (typeof(x)!='object' && typeof(x)!='function'); }
function trimStr(x) { return (typeof(x)=='string' && x ? x.replace(/^\s+|\s+$/g,"") : '') };
function createTextEl(a) {
return document.createTextNode(a)
}
function createEl(a, b, c) {
var d = document.createElement(a);
for (var e in b) if (b.hasOwnProperty(e)) d.setAttribute(e, b[e]);
if (c) d.innerHTML = c;
return d
}
/**
* Waits for the page to load before launch the script.
**/
document.onreadystatechange = function () {
if (document.readyState == "complete") {
initStyles();
addCustomScripts();
HashChange.init();
}
}
var HashChange = {
init: function() {
setInterval(HashChange.checkHashChange, 100);
},
// Check if url hash has changed and fire hashchange event if it has.
checkHashChange: function() {
var hash_is_new = window.location.hash && window.currentHash != window.location.hash;
if (hash_is_new) {
window.currentHash = window.location.hash;
setTimeout(function(){
loadCodeMirror();
},1000);
}
}
};
function addCustomScripts() {
var script = document.createElement("script");
script.type = "text/javascript";
script.text = "$('.ui-sortable').sortable('disable');";
document.body.appendChild(script);
}
function loadCodeMirror() {
$("textarea[id^=page_]").each(function(){
var editor_html = CodeMirror.fromTextArea($(this)[0], {
mode: "text/html",
lineNumbers: true,
height: "200px",
width: "90%",
lineWrapping:true,
styleActiveLine: true,
theme:"monokai",
foldGutter: true,
gutters: ["CodeMirror-foldgutter"]
});
});
}
function initStyles() {
GM_addGlobalStyle('http://codemirror.net/lib/codemirror.css');
GM_addGlobalStyle('http://codemirror.net/addon/fold/foldgutter.css');
GM_addGlobalStyle('http://codemirror.net/theme/monokai.css');
GM_addGlobalStyle(''
+'.CodeMirror {height: auto!important; width: auto!important; line-height:18px !important; font-family:"Monaco" !important; }'
+'.ui-sortable li {margin:5px; }'
,'additional-css', 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment