PI Search Helper Script

Keyword highlighting, keyboard shortcuts and autoselect options. Helper script for PI search in mturk. Disable script when not in use.

  1. // ==UserScript==
  2. // @name PI Search Helper Script
  3. // @namespace https://gf.qytechs.cn/en/users/10782
  4. // @version 0.4111
  5. // @description Keyword highlighting, keyboard shortcuts and autoselect options. Helper script for PI search in mturk. Disable script when not in use.
  6. // @author tismyname
  7. // @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
  8. // @include https://s3.amazonaws.com/*
  9. // ==/UserScript==
  10.  
  11. // Makes Instructions Hidable
  12. $(".panel-heading").before('<label id="toggle-label" style="color:#0000EE; "><input type="checkbox" id="toggler"><span>-Show Instructions-<span></input><br></label>')
  13. $(".panel-heading").hide();
  14. $(".panel-body").hide();
  15. var text = $('#toggle-label').text();
  16.  
  17. // Button to show or hide instructions
  18. $('#toggler').click(function() {
  19. $(".panel-heading").toggle();
  20. $(".panel-body").toggle();
  21. $('#toggle-label').text() == '-Show Instructions-' ? str = '-Hide Instructions-' : str = '-Show Instructions-';
  22. $('#toggle-label span').html(str);
  23. });
  24.  
  25. var ITEMS = 6;
  26. var currentQ = 1;
  27.  
  28.  
  29. // Auto Select First Option
  30. for(var i = 1; i <= ITEMS; i++) {
  31. $('#Q'+i+'_5').click();
  32. // Quick Fix for the Pi Search with Two Choices
  33. $('#Q'+currentQ+'_Y').click();
  34. }
  35.  
  36. // Auto focuses on first radio button
  37. $('#Q'+currentQ+'_5').focus();
  38.  
  39. // Sets current clicked element to match keyboard shortcuts
  40. $("input[type=radio]").click(function(){
  41. currentQ = this.id.charAt(1);
  42. });
  43.  
  44.  
  45. // Checks for keypresses
  46. $(document).keyup(function (event) {
  47. var key = toCharacter(event.keyCode);
  48. if (key=='1') {
  49. $('#Q'+currentQ+'_5').prop("checked", true);
  50. // Quick Fix for the Pi Search with Two Choices
  51. $('#Q'+currentQ+'_Y').prop("checked", true);
  52. currentQ++;
  53. }
  54. if (key=='2') {
  55. $('#Q'+currentQ+'_4').prop("checked", true);
  56. // Quick Fix for the Pi Search with Two Choices
  57. $('#Q'+currentQ+'_Y').prop("checked", true);
  58. currentQ++;
  59. }
  60. if (key=='3') {
  61. $('#Q'+currentQ+'_3').prop("checked", true);
  62. currentQ++;
  63. }
  64. if (key=='4') {
  65. $('#Q'+currentQ+'_2').prop("checked", true);
  66. currentQ++;
  67. }
  68. if (key=='5') {
  69. $('#Q'+currentQ+'_1').prop("checked", true);
  70. currentQ++;
  71. }
  72. if(key=='N' || key=='W') {
  73. currentQ++;
  74. }
  75. if(key=='B' || key=='Q') {
  76. currentQ--;
  77. }
  78. if(currentQ > ITEMS)
  79. {
  80. currentQ = 6;
  81. }
  82. if(currentQ < 1)
  83. {
  84. currentQ = 1;
  85. }
  86. if(key != 'ARROWKEY')
  87. {
  88. $('#Q'+currentQ+'_5').focus();
  89. $('#Q'+currentQ+'_Y').focus();
  90. }
  91. });
  92.  
  93. // keyboard logic from https://gf.qytechs.cn/en/scripts/5978-mturk-dave-cobb-hit-helper
  94. function toCharacter(keyCode) {
  95. // delta to convert num-pad key codes to QWERTY codes.
  96. var numPadToKeyPadDelta = 48;
  97.  
  98. // if a numeric key on the num pad was pressed.
  99. if (keyCode >= 96 && keyCode <= 105) {
  100. keyCode = keyCode - numPadToKeyPadDelta;
  101. return String.fromCharCode(keyCode);
  102. }
  103. if(keyCode >= 37 && keyCode <= 40)
  104. return "ARROWKEY";
  105. if (keyCode == 13)
  106. return "ENTER"; // not sure if I need to add code to hit the submit button
  107. return String.fromCharCode(keyCode);
  108. }
  109.  
  110.  
  111. /*
  112. SUPER AWESOME NOT MINE PLUGIN CODE STARTS HERE ====================================================
  113.  
  114. highlight v5
  115.  
  116. Highlights arbitrary terms.
  117.  
  118. <http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
  119.  
  120. MIT license.
  121.  
  122. Johann Burkard
  123. <http://johannburkard.de>
  124. <mailto:jb@eaio.com>
  125.  
  126. */
  127. jQuery.fn.highlight = function(pat) {
  128. function innerHighlight(node, pat) {
  129. var skip = 0;
  130. if (node.nodeType == 3) {
  131. var pos = node.data.toUpperCase().indexOf(pat);
  132. pos -= (node.data.substr(0, pos).toUpperCase().length - node.data.substr(0, pos).length);
  133. if (pos >= 0) {
  134. var spannode = document.createElement('span');
  135. spannode.className = 'highlight';
  136. var middlebit = node.splitText(pos);
  137. var endbit = middlebit.splitText(pat.length);
  138. var middleclone = middlebit.cloneNode(true);
  139. spannode.appendChild(middleclone);
  140. middlebit.parentNode.replaceChild(spannode, middlebit);
  141. skip = 1;
  142. }
  143. }
  144. else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
  145. for (var i = 0; i < node.childNodes.length; ++i) {
  146. i += innerHighlight(node.childNodes[i], pat);
  147. }
  148. }
  149. return skip;
  150. }
  151. return this.length && pat && pat.length ? this.each(function() {
  152. innerHighlight(this, pat.toUpperCase());
  153. }) : this;
  154. };
  155.  
  156. jQuery.fn.removeHighlight = function() {
  157. return this.find("span.highlight").each(function() {
  158. this.parentNode.firstChild.nodeName;
  159. with (this.parentNode) {
  160. replaceChild(this.firstChild, this);
  161. normalize();
  162. }
  163. }).end();
  164. };
  165. // PLUGIN CODE ENDS ===================================================
  166.  
  167. // Adds Styling Class for Highlighting Text
  168. var sheet = document.createElement('style')
  169. sheet.innerHTML = ".highlight { font-weight: bold; background-color: yellow; font-size: 110%;}";
  170. document.body.appendChild(sheet);
  171.  
  172. // Regex to find keywords to highlight
  173. var match = $('tbody').text().match(/searched for[: ](.*)/g);
  174.  
  175. // Loops through matched terms, cleans regex junk and then highlights them
  176. for(var i = 0; i < match.length; i++)
  177. {
  178. // Remove the noise in front used to find the area
  179. var cleanedString = match[i].toString().substring(13);
  180. // Replace any searches with '+' with spaces
  181. cleanedString = cleanedString.replace(/[+]/g, " ");
  182. var words = cleanedString.split(" ");
  183. for(var j = 0; j < words.length; j++)
  184. {
  185. var word = words[j];
  186.  
  187. // Highlights only first row of each table
  188. $('tr:eq('+i*2+')').highlight(word);
  189. // If word ends in s, highlight the case without it
  190. var patt = new RegExp("[s]$");
  191. if( patt.test(word) ) {
  192. $('tr:eq('+i*2+')').highlight(word.substring(0, word.length - 1));
  193. }
  194.  
  195. // Cases where word ends in 'es'
  196. patt = new RegExp("es$");
  197. if( patt.test(word) ) {
  198. $('tr:eq('+i*2+')').highlight(word.substring(0, word.length - 2));
  199. }
  200. }
  201. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址