BreadCrumbs: Greasemonkey
Greasemonkey
From Luke Jackson
(Difference between revisions)
Revision as of 02:18, 23 July 2016 (edit) Ljackson (Talk | contribs) (→Multiple Select Options with CSV) ← Previous diff |
Revision as of 02:30, 23 July 2016 (edit) Ljackson (Talk | contribs) (→Parse URLs with Xpath and Fetch Contents) Next diff → |
||
Line 238: | Line 238: | ||
//fileCSV.push(rowCSV.join(",")); | //fileCSV.push(rowCSV.join(",")); | ||
- | console.log(batchID + ": " + rowCSV.join(",")); | + | console.log(batchID + "," + rowCSV.join(",")); |
rowCSV = []; | rowCSV = []; | ||
} | } | ||
Line 248: | Line 248: | ||
}); | }); | ||
- | console.log(batchID); | + | //console.log(batchID); |
- | console.log('hello' + i); // your code here | + | //console.log('hello' + i); // your code here |
i++; // increment the counter | i++; // increment the counter | ||
- | if (i < 2) { // if the counter < 10, call the loop function | + | if (i < 3) { // if the counter < 10, call the loop function |
myLoop(); // .. again which will trigger another | myLoop(); // .. again which will trigger another | ||
} // .. setTimeout() | } // .. setTimeout() |
Revision as of 02:30, 23 July 2016
Contents |
Examples
Select Multiple Check Boxes with the Shift Key
Lets you check or uncheck a range of checkboxes by clicking the first checkbox and then Shift+clicking the last checkbox.
// ==UserScript== // @name Check Range // @namespace http://squarefree.com/userscripts // @description Lets you check or uncheck a range of checkboxes by clicking the first checkbox and then Shift+clicking the last checkbox. // @include * // @exclude http://gmail.google.com/* // @exclude https://gmail.google.com/* // ==/UserScript== /* Author: Jesse Ruderman - http://www.squarefree.com/ Suggested on http://69.90.152.144/collab/GreaseMonkeyUserScriptRequest at 2005-04-06 17:00:13 anonymously. Features: * Works with both mouse (shift+click) and keyboard (shift+space). * Use to select or deselect. * Use forwards or backwards. Tested with: * Hotmail, Yahoo! Mail, Google Personalized profile creation. * HTML loose, HTML strict, XHTML (with the XHTML mime type). */ (function() { var currentCheckbox = null; function NSResolver(prefix) { if (prefix == 'html') { return 'http://www.w3.org/1999/xhtml'; } else { //this shouldn't ever happen return null; } } function selectCheckboxRange(start, end) { var xpath, i, checkbox, last; if (document.documentElement.namespaceURI) // XML xpath = "//html:input[@type='checkbox']"; else // HTML xpath = "//input[@type='checkbox']"; var checkboxes = document.evaluate(xpath, document, NSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); for (i = 0; (checkbox = checkboxes.snapshotItem(i)); ++i) { if (checkbox == end) { last = start; break; } if (checkbox == start) { last = end; break; } } for (; (checkbox = checkboxes.snapshotItem(i)); ++i) { if (checkbox != start && checkbox != end && checkbox.checked != start.checked) { // Instead of modifying the checkbox's value directly, fire an onclick event. // This makes scripts that are part of Yahoo! Mail and Google Personalized pick up the change. // Doing it this way also triggers an onchange event, which is nice. var evt2 = document.createEvent("MouseEvents"); evt2.initEvent("click", true, false); checkbox.dispatchEvent(evt2); } if (checkbox == last) { break; } } } function handleChange(event) { var t = event.target; if (isCheckbox(t) && (event.button == 0 || event.keyCode == 32)) { if (event.shiftKey && currentCheckbox) { selectCheckboxRange(currentCheckbox, t); } currentCheckbox = t; } } function isCheckbox(elt) { // tagName requires toUpperCase because of HTML vs XHTML return (elt.tagName.toUpperCase() == "INPUT" && elt.type == "checkbox"); } // onchange always has event.shiftKey==true, so to tell whether // shift was held, we have to use onkeyup and onclick instead. document.documentElement.addEventListener("keyup", handleChange, true); document.documentElement.addEventListener("click", handleChange, true); })();
Multiple Select Options with CSV
Automatically populate (set to selected) multiple select box options based on comma separated values equal to the values of the desired selected options.
// NOTE : Once you are on a webpage, you can invoke // this script by pressing CTRL+SHIFT+F // ================================ Set data here ============================= // // Values for the fieldnames above, in the exact array order //select = [6, 7, 8, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 72, 73, 74, 75, 76, 77, 79, 81, 82, 83, 84, 85, 87, 88, 89, 90, 91, 92, 93, 94, 95, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 162, 163, 164, 166, 167, 168, 169, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202]; //select = new Array ( '6', '7', '8', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '40', '41', '42', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '72', '73', '74', '75', '76', '77', '79', '81', '82', '83', '84', '85', '87', '88', '89', '90', '91', '92', '93', '94', '95', '97', '98', '99', '100', '101', '102', '103', '104', '105', '106', '107', '108', '109', '110', '111', '112', '113', '114', '115', '116', '117', '118', '119', '120', '121', '122', '123', '124', '125', '126', '127', '128', '129', '130', '131', '134', '135', '136', '138', '139', '140', '141', '142', '143', '144', '145', '146', '147', '148', '149', '150', '151', '152', '153', '154', '155', '156', '162', '163', '164', '166', '167', '168', '169', '173', '174', '175', '176', '177', '178', '179', '180', '181', '182', '183', '184', '185', '186', '187', '188', '189', '190', '191', '192', '193', '194', '195', '196', '197', '198', '199', '200', '201', '202' ); select = [48, 109, 121, 144, 171, 175, 210, 221, 288, 347]; // ================================= No need to alter below ==================== // function doAutoFill() { var allFields, currentField, fieldName, splitFields, test, luke; allFields = document.getElementsByTagName('select'); //producttype_id[] //productcolor_id[] if(allFields.length > 1) { alert(allFields.length); alert(allFields[2].name); } //luke = allFields[3].name; for (var h = 0; h < allFields[2].options.length; h++) { for (var i = 0; i < select.length; i++) { if(allFields[2].options[h].value == select[i]) { allFields[2].options[h].selected = true; break; } } } } // ============================= // Track keypresses function keyDown(e) { if (e.shiftKey&&e.ctrlKey) { switch (e.keyCode) { case 70: // CTRL+SHIFT+F doAutoFill(); break; } } } // ============================= // Add a handler for the keyboard shortcut window.addEventListener("keydown", keyDown, true);
Parse URLs with Xpath and Fetch Contents
// ==UserScript== // @name Links // @namespace All // @version 1 // @grant none // ==/UserScript== var links = document.evaluate("//tr/@href", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null); var i = 0; // set your counter to 1 function myLoop () { // create a loop function setTimeout(function () { // call a 3s setTimeout when the loop is called var thisLink = links.snapshotItem(i); var lSplit = thisLink.value.split("/"); var batchID = lSplit.pop(); var rootURL = "https://server.com/"; var gURL = rootURL + thisLink.value; //your code to be executed after 1 second // Replace URL //thisLink.setAttribute("href", "https://server.com/" + thisLink.getAttribute("href")); var ReqText; $.get(gURL, function(ReqText) { var RMdoc = new DOMParser().parseFromString(ReqText,'text/html'); var lja = RMdoc.evaluate("//td/text()", RMdoc, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); var rowCSV = []; var fileCSV = []; for (var p=0; p < lja.snapshotLength; p++) { var thisTable = lja.snapshotItem(p); rowCSV.push(thisTable.nodeValue); if (rowCSV.length == 7) { //fileCSV.push(rowCSV.join(",")); console.log(batchID + "," + rowCSV.join(",")); rowCSV = []; } } //console.log(batchID + ": " + csvData.join(",")); }); //console.log(batchID); //console.log('hello' + i); // your code here i++; // increment the counter if (i < 3) { // if the counter < 10, call the loop function myLoop(); // .. again which will trigger another } // .. setTimeout() }, 2000) } //links.snapshotLength myLoop(); // start the loop