Skip to content

Instantly share code, notes, and snippets.

@Griever
Created May 12, 2011 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Griever/968254 to your computer and use it in GitHub Desktop.
Save Griever/968254 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name copySelector
// @description Stylish から拝借
// @namespace http://d.hatena.ne.jp/Griever/
// @author Griever
// @include chrome://inspector/content/inspector.xul
// @compatibility Firefox 4
// @version 0.0.1
// ==/UserScript==
setTimeout(function() {
var pane = document.getElementById("bxDocPanel");
var iframe = pane.mIFrameEl;
var win = iframe.contentWindow;
var doc = iframe.contentDocument;
if (win.stylishDomi) return;
win.stylishDomi = {
generateSelectors: function(event) {
var node = win.viewer.selectedNode;
if (!(node instanceof Element)) {
return;
}
var popup = event.target;
var idmenu = doc.getElementById("copy-selector-id");
var classmenu = doc.getElementById("copy-selector-class");
// id selector
if (node.hasAttribute("id")) {
idmenu.setAttribute("label", "#" + node.getAttribute("id"));
idmenu.setAttribute("hidden", "false");
} else {
idmenu.setAttribute("hidden", "true");
}
//class selector
if (node.hasAttribute("class")) {
classmenu.setAttribute("label", "." + node.getAttribute("class").split(/\s+/).join("."));
classmenu.setAttribute("hidden", "false");
} else {
classmenu.setAttribute("hidden", "true");
}
},
copySelectorToClipboard: function(event) {
Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper).copyString(event.target.getAttribute("label"));
}
}
var mnEditPasteMenu = doc.querySelector("#mnEditPasteMenu + menuseparator");
var m = doc.createElement("menuitem");
m.setAttribute("id", "copy-selector-id");
m.setAttribute("hidden", "true");
m.setAttribute("oncommand", "stylishDomi.copySelectorToClipboard(event);");
mnEditPasteMenu.parentNode.insertBefore(m, mnEditPasteMenu);
var m = doc.createElement("menuitem");
m.setAttribute("id", "copy-selector-class");
m.setAttribute("hidden", "true");
m.setAttribute("oncommand", "stylishDomi.copySelectorToClipboard(event);");
mnEditPasteMenu.parentNode.insertBefore(m, mnEditPasteMenu);
doc.getElementById('ppDOMContext').addEventListener('popupshowing', win.stylishDomi.generateSelectors, false);
win.addEventListener('unload', function(event){
doc.getElementById('ppDOMContext').removeEventListener('popupshowing', win.stylishDomi.generateSelectors, false);
}, false);
}, 500);
(function() {
var Cc = Components.classes;
var Ci = Components.interfaces;
function getDatasourceURI() {
var file = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties).get("ProfD", Ci.nsIFile);
file.append("stylish.rdf");
if (!file.exists()) throw "stylish.rdf is Not Found.";
var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
return ioService.newFileURI(file).spec;
}
var rdfService = Cc["@mozilla.org/rdf/rdf-service;1"].getService(Ci.nsIRDFService);
var ds = rdfService.GetDataSourceBlocking(getDatasourceURI());
var rdfContainerUtils = Cc["@mozilla.org/rdf/container-utils;1"].getService(Ci.nsIRDFContainerUtils);
var container = rdfContainerUtils.MakeSeq(ds, rdfService.GetResource("urn:stylish:userstyles"));
var children = container.GetElements();
var enabledPredicate = rdfService.GetResource("urn:stylish#enabled");
var codePredicate = rdfService.GetResource("urn:stylish#code");
var descriptionPredicate = rdfService.GetResource("urn:stylish#description");
var namespacePredicate = rdfService.GetResource("urn:stylish#namespace");
var xulns = [];
var htmlns = [];
var exns = [];
while (children.hasMoreElements()) {
var current = children.getNext();
var nsNode = ds.GetTarget(current, namespacePredicate, true);
var namespace = nsNode? nsNode.QueryInterface(Ci.nsIRDFLiteral).Value : "";
var description = ds.GetTarget(current, descriptionPredicate, true).QueryInterface(Ci.nsIRDFLiteral).Value;
var enabled = ds.GetTarget(current, enabledPredicate, true).QueryInterface(Ci.nsIRDFLiteral).Value;
var css = ds.GetTarget(current, codePredicate, true).QueryInterface(Ci.nsIRDFLiteral).Value;
var code =
"/*\n" +
" * @description " + description + "\n" +
" * @enabled " + enabled + "\n" +
" */\n\n" + css + "\n\n\n\n";
if (namespace == "http://www.w3.org/1999/xhtml")
htmlns.push(code);
else if (namespace == "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul")
xulns.push(code);
else
exns.push(code);
}
var url = "data:text/css;charset=UTF-8," + encodeURIComponent(
htmlns.join('\n\n\n\n') + "\n\n\n\n\n\n" +
xulns.join('\n\n\n\n') + "\n\n\n\n\n\n" +
exns.join('\n\n\n\n') + "\n\n\n\n\n\n");
var wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
var win = wm.getMostRecentWindow("navigator:browser");
if (win) {
win.openNewTabWith(url);
} else {
window.openDialog("chrome://browser/content/", "_blank", "chrome,all,dialog=no", url);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment