Skip to content

Instantly share code, notes, and snippets.

@kastner
Created September 5, 2008 17:58
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 kastner/8998 to your computer and use it in GitHub Desktop.
Save kastner/8998 to your computer and use it in GitHub Desktop.
function _parseHTML(html) {
var doc = context.focusedWindow.document;
var div = doc.createElement( "div" );
div.innerHTML = html;
return div;
}
CmdUtils.CreateCommand({
name:"toggle-these",
homepage: "http://metaatem.net/",
author: { name: "Erik Kastner", email: "kastner@gmail.com"},
license: "MPL",
takes: {"selection": noun_arb_text },
description: "Toggles check boxes in the selection",
execute: function(directObject) {
var boxes = jQuery(":checkbox", _parseHTML(directObject.html)).map(function() { return ({name: this.name, id: this.id}); });
jQuery(boxes).each(function(box) {
var document = context.focusedWindow.document;
var b = boxes[box];
var c;
if(b.id) { c = jQuery("#" + b.id, document); }
else if(b.name) { c = jQuery("input[name='" + b.name + "']", document); }
if (c) { c.click(); }
});
},
preview: function( pblock, directObject ) {
var num = jQuery(":checkbox", _parseHTML(directObject.html)).size();
pblock.innerHTML = "Toggling " + num + " checkboxes";
}
});
CmdUtils.CreateCommand({
name:"check-these",
homepage: "http://metaatem.net/",
author: { name: "Erik Kastner", email: "kastner@gmail.com"},
license: "MPL",
takes: {"selection": noun_arb_text },
description: "Checks all the check boxes in the selection",
execute: function(directObject) {
var boxes = jQuery(":checkbox", _parseHTML(directObject.html)).map(function() { return ({name: this.name, id: this.id}); });
jQuery(boxes).each(function(box) {
var document = context.focusedWindow.document;
var b = boxes[box];
var c;
if(b.id) { c = jQuery("#" + b.id, document); }
else if(b.name) { c = jQuery("input[name='" + b.name + "']", document); }
if (c) { jQuery(c).attr("checked", true); }
});
},
preview: function( pblock, directObject ) {
var num = jQuery(":checkbox", _parseHTML(directObject.html)).size();
pblock.innerHTML = "Checking " + num + " checkboxes";
}
});
CmdUtils.CreateCommand({
name:"uncheck-these",
homepage: "http://metaatem.net/",
author: { name: "Erik Kastner", email: "kastner@gmail.com"},
license: "MPL",
takes: {"selection": noun_arb_text },
description: "Unchecks all the check boxes in the selection",
execute: function(directObject) {
var boxes = jQuery(":checkbox", _parseHTML(directObject.html)).map(function() { return ({name: this.name, id: this.id}); });
jQuery(boxes).each(function(box) {
var document = context.focusedWindow.document;
var b = boxes[box];
var c;
if(b.id) { c = jQuery("#" + b.id, document); }
else if(b.name) { c = jQuery("input[name='" + b.name + "']", document); }
if (c) { jQuery(c).attr("checked", false); }
});
},
preview: function( pblock, directObject ) {
var num = jQuery(":checkbox", _parseHTML(directObject.html)).size();
pblock.innerHTML = "Unchecking " + num + " checkboxes";
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment