Skip to content

Instantly share code, notes, and snippets.

@xiaoxiaoflood
Last active June 5, 2020 16:21
Show Gist options
  • Save xiaoxiaoflood/729ed66051d022a07dae67838c53bc51 to your computer and use it in GitHub Desktop.
Save xiaoxiaoflood/729ed66051d022a07dae67838c53bc51 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name checkForStyleEditorErrors
// @include chrome://devtools/content/shared/sourceeditor/codemirror/cmiframe.html
// ==/UserScript==
const {Services} = ChromeUtils.import('resource://gre/modules/Services.jsm');
let sss = Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService);
sss.loadAndRegisterSheet(Services.io.newURI('data:text/css;charset=UTF-8,' + encodeURIComponent(`
@-moz-document url('chrome://devtools/content/styleeditor/index.xhtml') {
#errors label {
cursor: pointer;
}
}
`)), sss.AUTHOR_SHEET);
keyset = document.createXULElement('keyset');
document.documentElement.appendChild(keyset);
checkKey = document.createXULElement('key');
checkKey.setAttribute('id', 'xiao-check');
checkKey.setAttribute('modifiers', 'alt');
checkKey.setAttribute('key', 'C');
checkKey.setAttribute('oncommand', 'checkForErrors()');
keyset.appendChild(checkKey);
let doc = parent.document;
let errors = doc.getElementById('errors');
if (!errors) {
errors = doc.createElement('vbox');
errors.id = 'errors';
errors.className = 'theme-toolbar';
errors.style.display = 'none';
doc.documentElement.appendChild(errors);
}
errors.addEventListener('click', function (e) {
if (e.target == this)
this.style.display = 'none';
});
function goToLine (line, col) {
editor.focus();
editor.setCursor({line: line - 1, ch: col});
}
docShell.cssErrorReportingEnabled = true;
function checkForErrors() {
while (errors.hasChildNodes())
errors.removeChild(errors.lastChild);
let count = 0;
let errorListener = {
observe: function (message) {
if (!count)
errors.style.display = 'block';
let error = message.QueryInterface(Ci.nsIScriptError);
let newmessage = error.lineNumber + ':' + error.columnNumber + ' - ' + error.errorMessage;
let label = doc.createElement('label');
label.appendChild(doc.createTextNode(newmessage));
label.addEventListener('click', function () {
goToLine(error.lineNumber, error.columnNumber);
});
errors.appendChild(label);
errors.appendChild(doc.createElement('br'));
count++;
if (count == 10) {
errors.appendChild(doc.createTextNode('...'));
Services.console.unregisterListener(this);
}
}
}
Services.console.registerListener(errorListener);
let styleEl = document.createElement('style');
styleEl.appendChild(document.createTextNode(editor.getText()));
document.documentElement.appendChild(styleEl);
styleEl.remove();
// do this on a delay because of https://bugzilla.mozilla.org/show_bug.cgi?id=831428
setTimeout(() => {
if (count < 10)
Services.console.unregisterListener(errorListener);
});
editor.focus();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment