Skip to content

Instantly share code, notes, and snippets.

@nfeldman
Created July 6, 2011 00:48
Show Gist options
  • Save nfeldman/1066301 to your computer and use it in GitHub Desktop.
Save nfeldman/1066301 to your computer and use it in GitHub Desktop.
GreaseMonkey userscript to add a modal "front end" HTML editor to any node on a Drupal site
// ==UserScript==
// @name Front End Editor
// @namespace NSF
// @include http://the.devsite.com/*
// ==/UserScript==
/**
* @copyright 2011 Noah Feldman all rights reserved
* @author Noah Feldman
* @depends a good browser
* Was originally made to make it simpler to edit
* html on a Drupal site that had been populated with
* incomplete content and invalid markup by an incompetent
* vendor there was no point using a WYSIWYG editor
* and I wanted a one click way to get syntax highlighting
* requires codemirror available on a server
* along with a custom css file.
*/
var head = document.getElementsByTagName('head'),
body = document.body,
style, script,
NSF = unsafeWindow.NSF || {};
unsafeWindow.NSF = NSF;
if (body.className.match(/\bfront\b/)) {
style = document.createElement('style');
script = document.createElement('script');
style.type = 'text/css';
script.type = 'text/javascript';
script.src = 'http://example.com/cm/js/codemirror.min.js';
head[0].appendChild(style);
GM_xmlhttpRequest({
method: "GET",
url: "http://example.com/frontEndEditor.css",
onload: function(response) {
style.innerHTML = response.responseText;
}
});
}
(function (global, doc) {
// If logged in, inject front-end html editor for node body
if (body.className.match(/\slogged-in\b/) && body.className.match(/\bfront\b/)) {
var panelb = document.createElement('div'),
toArray = function (arg) {
return [].slice.call(arg);
},
editor, editLink;
panelb.id = "panel2";
panelb.className = "closed";
head[0].appendChild(script);
toArray(doc.links).forEach(function (el) {
if (el.href.match(/node\/([0-9]+)\/edit/)) return editLink = el.href;
}, this);
if (editLink) {
NSF.misc = {};
NSF.misc.editLink = editLink;
(function () {
var xmlhttp = new XMLHttpRequest(),
cm = doc.createElement('script'), form, edit, tmp, tgl;
xmlhttp.overrideMimeType('text/xml');
xmlhttp.open("GET", editLink, true);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState === 4) {
form = doc.adoptNode(xmlhttp.responseXML.getElementById('node-form'));
tgl = doc.createElement('div');
tgl.id = 'tgl';
tgl.innerHTML = "<span>+/-</span>";
tgl.addEventListener('click', function (e) {
var panel = document.getElementById('panel2');
panel.className.match(/closed/g) ? panel.className = '' : panel.className = 'closed';
}, false);
panelb.appendChild(tgl);
panelb.appendChild(form);
edit = doc.getElementById('edit-body');
cm.type = "text/javascript";
cm.innerHTML = ""+
"editor = CodeMirror.fromTextArea(\"edit-body\", {" +
"height: \"350px\"," +
"parserfile: \"http://example.com/cm/js/parsexml.js\"," +
"stylesheet: \"http://example.com/cm/css/xmlcolors1.css\"," +
"path: \"http://example.com/cm/js/\"," +
"continuousScanning: 250," +
"lineNumbers: false," +
"indentUnit: 4," +
"tabMode: 'spaces'," +
"reindentOnLoad: true," +
"iframeClass: 'cm-iframe'," +
"onChange: function() {" +
"var editBody = document.getElementById('edit-body');" +
"editBody.innerHTML = editor.getCode();" +
"}" +
"});" +
"editor.win.addEventListener('blur', function() {" +
"var editBody = document.getElementById('edit-body');" +
"editBody.innerHTML = editor.getCode();" +
"}, false);";
panelb.appendChild(cm);
return panelb;
}
}
xmlhttp.send(null);
}());
}
}
if (editLink) body.appendChild(panelb);
}(unsafeWindow, unsafeWindow.document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment