GRO Index Search Helper

Adds additional functionality to the UK General Register Office (GRO) BMD index search

当前为 2019-02-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GRO Index Search Helper
  3. // @description Adds additional functionality to the UK General Register Office (GRO) BMD index search
  4. // @namespace cuffie81.scripts
  5. // @include https://www.gro.gov.uk/gro/content/certificates/indexes_search.asp
  6. // @version 1.15
  7. // @grant GM_listValues
  8. // @grant GM_getValue
  9. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.0.10/handlebars.min.js
  11. // @require https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js
  12. // ==/UserScript==
  13.  
  14.  
  15. this.$ = this.jQuery = jQuery.noConflict(true);
  16.  
  17. $(function() {
  18. var resources, recordType, results;
  19. var main = function() {
  20. buildResources();
  21. recordType = getRecordType();
  22. //console.log("resources:\r\n%s", JSON.stringify(resources));
  23. // Load the general css
  24. $("body").append($(resources.baseStyle));
  25.  
  26. initialiseSearchForm();
  27. initialiseResultViews();
  28. // Scroll down to the form. Do this last as we may add/remove/change elements in the previous calls.
  29. $("h1:contains('Search the GRO Online Index')")[0].scrollIntoView();
  30. // Wire up accesskeys to clicks, to avoid having to use the full accesskey combo (eg ALT+SHFT+#)
  31. $(document).on("keypress", function(e) {
  32. if (!document.activeElement || document.activeElement.tagName.toLowerCase() !== "input")
  33. {
  34. var char = String.fromCharCode(e.which);
  35. //console.log("keypress: %s", char);
  36. if ($("*[id^='groish'][accesskey='" + char + "']").length)
  37. $("*[id^='groish'][accesskey='" + char + "']").click();
  38. else if (char == "{")
  39. adjustSearchYear(-10);
  40. else if (char == "}")
  41. adjustSearchYear(10);
  42. else if (char == "?")
  43. $("form[name='SearchIndexes'] input[type='submit']").click();
  44. else if (char == '@')
  45. switchRecordType();
  46. }
  47. });
  48. }
  49. var initialiseSearchForm = function() {
  50. // Hide superfluous spacing, text and buttons
  51. $("body > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(2)").hide();
  52. $("h1:contains('Search the GRO Online Index')").closest("tr").next().hide();
  53. $("strong:contains('Which index would you like to search?')").closest("tr").hide();
  54. $("table[summary*='contains the search form fields'] > tbody > tr:nth-of-type(2)").hide();
  55. $("table[summary*='contains the search form fields'] > tbody > tr:nth-of-type(3) td.main_text[colspan='5']").parent().hide();
  56. $("form[name='SearchIndexes'] input[type='submit'][value='Reset']").hide();
  57. $("form[name='SearchIndexes'] a.tooltip").hide();
  58. $("form[name='SearchIndexes'] span.main_text").has("i > a[href^='most_customers_want_to_know.asp']").hide();
  59. // Change text
  60. $("form[name='SearchIndexes'] td span.main_text:contains('year(s)')").text("yrs");
  61. $("form[name='SearchIndexes'] td.main_text:contains('Surname at Death:')").html("Surname:<span class='redStar'>*</span>");
  62. $("form[name='SearchIndexes'] td.main_text:contains('First Forename at Death:')").text("Forename 1:");
  63. $("form[name='SearchIndexes'] td.main_text:contains('Second Forename at Death:')").text("Forename 2:");
  64. $("form[name='SearchIndexes'] td.main_text:contains('District of Death:')").text("District:");
  65. $("form[name='SearchIndexes'] td.main_text:contains('Age at'):contains('Death'):contains('in years')").text("Age:");
  66. $("form[name='SearchIndexes'] td.main_text:contains('Surname at Birth:')").html("Surname:<span class='redStar'>*</span>");
  67. $("form[name='SearchIndexes'] td.main_text:contains('First Forename:')").text("Forename 1:");
  68. $("form[name='SearchIndexes'] td.main_text:contains('Second Forename:')").text("Forename 2:");
  69. $("form[name='SearchIndexes'] td.main_text:contains('Maiden Surname:')").text("Mother:");
  70. $("form[name='SearchIndexes'] td.main_text:contains('District of Birth:')").text("District:");
  71.  
  72. // Add gender and year navigation buttons, and style them
  73. var searchButton = $("form[name='SearchIndexes'] input[type='submit'][value='Search']");
  74. $(searchButton).attr("accesskey", "?");
  75. $(searchButton).parent().find("br").remove();
  76.  
  77. $("<input type='button' class='formButton' accesskey='#' id='groish_BtnToggleGender' value='Gender' />").insertBefore($(searchButton));
  78. $("<input type='button' class='formButton' accesskey='[' id='groish_BtnYearsPrev' value='&lt; Years' />").insertBefore($(searchButton));
  79. $("<input type='button' class='formButton' accesskey=']' id='groish_BtnYearsNext' value='Years &gt;' />").insertBefore($(searchButton));
  80. var buttonContainer = $("form[name='SearchIndexes'] input[type='submit'][value='Search']").closest("td").addClass("groish_ButtonContainer");
  81. // Add button event handlers
  82. $("input#groish_BtnYearsPrev").click(function() { navigateYears(false); });
  83. $("input#groish_BtnYearsNext").click(function() { navigateYears(true); });
  84. $("input#groish_BtnToggleGender").click(function() { toggleGender(); });
  85.  
  86. // Set encoding
  87. if (typeof $("form[name='SearchIndexes']").attr("accept-charset") === typeof undefined) {
  88. $("form[name='SearchIndexes']").attr("accept-charset", "UTF-8");
  89. }
  90.  
  91. }
  92. var initialiseResultViews = function() {
  93. // Move default results table into a view container
  94. var defaultTable = $("form[name='SearchIndexes'] h3:contains('Results:')").closest("table").css("width", "100%").addClass("groish_ResultsTable");
  95. $(defaultTable).before($("<div results-view='default' />"));
  96. var defaultView = $("div[results-view='default']");
  97. $(defaultView).append($("table.groish_ResultsTable"));
  98.  
  99. // Move header row to before default view
  100. $(defaultView).before($("<div class='groish_ResultsHeader' style='margin: 10px 0px; position: relative' />"));
  101. $(".groish_ResultsHeader").append($("table.groish_ResultsTable h3:contains('Results:')"));
  102.  
  103. // Move pager row contents to after default view
  104. $(defaultView).after($("table.groish_ResultsTable > tbody > tr:last table:first"));
  105. $("div[results-view='default'] + table").css("width", "100%").addClass("groish_ResultsInfo");
  106.  
  107. // Get results, sort them and populate views
  108. results = getResults(recordType);
  109. sortResults();
  110. populateAlternateViews();
  111. }
  112. var sortResults = function(reverse, sortFieldsCsv) {
  113. //console.log("sorting results, sort fields: %s", sortFieldsCsv);
  114. if (!results || !results.items)
  115. return;
  116. var defaultSortFields = "year,quarter";
  117. // Get the last sort fields and order for the record type
  118. var sortFieldsKey = recordType + "-sort-fields";
  119. var sortOrderKey = recordType + "-sort-order";
  120. var lastSortFields = sessionStorage.getItem(sortFieldsKey);
  121. var lastSortOrder = sessionStorage.getItem(sortOrderKey);
  122. // Cleanup values
  123. sortFieldsCsv = (sortFieldsCsv || "").replace(/\s\s+/g, ' ');
  124. lastSortFields = (lastSortFields || "").replace(/\s\s+/g, ' ');
  125. //console.log("last sort fields: %s; last sort order: %s", lastSortFields, lastSortOrder);
  126. var sortOrder = "asc";
  127. if (!sortFieldsCsv) {
  128. sortFieldsCsv = lastSortFields || defaultSortFields;
  129. sortOrder = lastSortOrder || "asc";
  130. }
  131. else if (sortFieldsCsv.localeCompare(lastSortFields) == 0 && sortOrder.localeCompare(lastSortOrder) == 0 && reverse) {
  132. sortOrder = "desc";
  133. }
  134. // Build sort fields and order arrays
  135. var sortFields = sortFieldsCsv.split(",");
  136. var sortOrders = Array.apply(null, Array(sortFields.length)).map(String.prototype.valueOf, sortOrder);
  137. // Append defaults if needed
  138. if (sortFieldsCsv.localeCompare(defaultSortFields) != 0) {
  139. sortFields.push("year");
  140. sortFields.push("quarter");
  141. sortOrders.push("asc");
  142. sortOrders.push("asc");
  143. }
  144. //console.log("sorting results by: %s (%s)", sortFields, sortOrders);
  145. results.items = _.orderBy(results.items, sortFields, sortOrders);
  146. sessionStorage.setItem(sortFieldsKey, sortFieldsCsv);
  147. sessionStorage.setItem(sortOrderKey, sortOrder);
  148. }
  149.  
  150. var populateAlternateViews = function() {
  151. // Add alternate view(s)
  152. if (recordType && resources && results && results.items && results.items.length > 0) {
  153. // Remove any existing views
  154. $("div[results-view][results-view!='default']").remove();
  155. // Add alternate views
  156. //console.log("Adding alternate views...");
  157. var viewPrefix = "view_" + recordType; // record type = EW_Birth, EW_Death
  158. for (var resourceName in resources) {
  159. var resourceNamePrefix = resourceName.substring(0, viewPrefix.length);
  160. if (resources.hasOwnProperty(resourceName) && viewPrefix.localeCompare(resourceNamePrefix) == 0) {
  161. var template = resources[resourceName].toString();
  162. var compiledTemplate = Handlebars.compile(template);
  163. var html = compiledTemplate(results);
  164. if (html) {
  165. $("div[results-view]").filter(":last").after($(html));
  166. //console.log("Added alternate view");
  167. }
  168. }
  169. }
  170. // Add view helpers and event handlers, if not already added
  171. if ($("div[results-view]").length > 1) {
  172. // Add event handler to hide/show actions row
  173. // TODO: Make adding view event handlers more dynamic, so they can be specific to the view
  174. $("div[results-view][results-view!='default'] tbody tr.rec")
  175. .off("click.groish")
  176. .on("click.groish", function(event) {
  177.  
  178. event.preventDefault();
  179. $(this).next("tr.rec-actions:not(:empty)").toggle();
  180. }
  181. );
  182.  
  183. // Add event handler for column sorting
  184. $("div[results-view][results-view!='default'] thead td[sort-fields]")
  185. .off("click.groish")
  186. .on("click.groish", function(event) {
  187.  
  188. event.preventDefault();
  189. //var defaultSortFields = ($(this).closet("div[results-view]").attr("default-sort-fields");
  190. var sortFields = ($(this).attr("sort-fields") ? $(this).attr("sort-fields") : $(this).text());
  191. sortResults(true, sortFields);
  192. populateAlternateViews();
  193. }
  194. );
  195.  
  196. // Add view switcher, if it doesn't already exist
  197. if ($("#groish_ViewSwitcher").length == 0) {
  198. $(".groish_ResultsHeader").append($("<a href='#' id='groish_ViewSwitcher' class='main_text' accesskey='~'>Switch view</a>"));
  199. $("#groish_ViewSwitcher").off("click.groish").on("click.groish", function() { switchResultsView(); return false; });
  200.  
  201.  
  202. // Add results copier (if supported)
  203. if (window.getSelection && document.createRange) {
  204. $(".groish_ResultsHeader").append($("<a href='#' id='groish_ResultsCopier' class='main_text' accesskey='|'>Copy results</a>"));
  205. $("#groish_ResultsCopier")
  206. .off("click.groish")
  207. .on("click.groish", function(event) {
  208.  
  209. event.preventDefault();
  210.  
  211. // Get most specific element containing results, typically a table body
  212. var resultsContent = $("div[results-view]:visible tbody");
  213.  
  214. if (resultsContent.length == 0)
  215. resultsContent = $("div[results-view]:visible");
  216. if (resultsContent.length > 0) {
  217. resultsContent = resultsContent[0];
  218. var selection = window.getSelection();
  219. var range = document.createRange();
  220. range.selectNodeContents(resultsContent);
  221. selection.removeAllRanges();
  222. selection.addRange(range);
  223.  
  224. try {
  225. if (document.execCommand("copy")) {
  226. selection.removeAllRanges();
  227. $(".groish_Message").text("Results copied to clipboard").show();
  228. setTimeout(function() { $(".groish_Message").fadeOut(); }, 3000);
  229. }
  230. }
  231. catch(e) { }
  232. }
  233.  
  234. return false;
  235. });
  236. }
  237. }
  238. }
  239.  
  240. // Show the last used view
  241. var viewName = sessionStorage.getItem("groish_view." + recordType);
  242. //console.log("initialising view: %s", viewName);
  243. if (viewName && $("div[results-view='" + viewName + "']:hidden").length == 1) {
  244. //console.log("setting active view: %s", viewName);
  245. $("div[results-view][results-view!='" + viewName + "']").hide();
  246. $("div[results-view][results-view='" + viewName + "']").show();
  247. }
  248. }
  249. }
  250.  
  251. var switchResultsView = function() {
  252. var views = $("div[results-view]");
  253. if (views.length > 1) {
  254. var curIndex = -1;
  255. $(views).each(function(index) {
  256. if ($(this).css("display") != "none")
  257. curIndex = index;
  258. });
  259.  
  260. //console.log("current view index: %s", curIndex);
  261. if (curIndex !== -1) {
  262. var newIndex = ((curIndex == (views.length-1)) ? 0 : curIndex+1);
  263. $(views).hide();
  264. $("div[results-view]:eq(" + newIndex + ")").show();
  265.  
  266. $(".groish_Message").hide();
  267.  
  268. // Get the name and save it
  269. var viewName = $("div[results-view]:eq(" + newIndex + ")").attr("results-view")
  270. sessionStorage.setItem("groish_view." + recordType, viewName); //save it
  271. //console.log("new view: %s", viewName);
  272. }
  273. }
  274. }
  275. var getResults = function(recordType) {
  276. var results = { "ageWarningThreshold": 24, "items": [], "failures": [] };
  277. // Lookup record type - birth or death
  278. if (recordType !== null && (recordType === "EW_Birth" || recordType === "EW_Death")) {
  279. var gender = $("form[name='SearchIndexes'] select#Gender").val();
  280. $("div[results-view='default'] > table > tbody > tr")
  281. .has("input[type='radio'][name='SearchResult']")
  282. .each(function(index) {
  283. try
  284. {
  285. //console.log("Parsing record (%d)...", index);
  286. // Get result id, contains year and record id
  287. var recordId = null;
  288. var resultId = $(this).find("input[type='radio'][name='SearchResult']:first").val();
  289. if (resultId && resultId.length > 5 && resultId.indexOf('.') == 4)
  290. recordId = resultId.substring(5);
  291. // Get names and reference
  292. var names = $(this).find("td:eq(1)").text().replace(/\u00a0/g, " ").replace(/\s\s+/g, ' ').trim();
  293. var ref = $(this).next().find("td:eq(0)").text();
  294.  
  295. // Clean up reference
  296. ref = ref.replace(/\u00a0/g, " ");
  297. ref = ref.replace(/\s\s+/g, ' ');
  298. ref = ref.replace(/GRO Reference: /g, "");
  299. ref = ref.replace(/M Quarter in/g, "Q1");
  300. ref = ref.replace(/J Quarter in/g, "Q2");
  301. ref = ref.replace(/S Quarter in/g, "Q3");
  302. ref = ref.replace(/D Quarter in/g, "Q4");
  303.  
  304. var age = 0;
  305. if (recordType === "EW_Death") {
  306. var ageArr = /^([0-9]{1,3})$/.exec($(this).find("td:eq(2)").text().replace(/\u00a0/g, " ").replace(/\s\s+/g, ' ').trim());
  307. if (ageArr)
  308. age = parseInt(ageArr[1], 10);
  309. }
  310.  
  311. var mother = null;
  312. if (recordType === "EW_Birth")
  313. mother = toTitleCase($(this).find("td:eq(2)").text().replace(/\u00a0/g, " ").replace(/\s\s+/g, ' ')).trim();
  314.  
  315.  
  316. // Parse forenames, surname, year, quarter, district, vol, page
  317. var namesArr = /([a-z' -]+),([a-z' -]*)/gi.exec(names);
  318. var refArr = /([0-9]{4}) Q([1-4]) ([a-z\.\-,\(\)0-9\&'/ ]*)Volume ([a-z0-9]+)( Page ([0-9a-z]+)|)( Occasional Copy: ([0-9a-z]+)|)/gi.exec(ref); // NB: the district may not be set in some cases
  319.  
  320. //console.log("index: %d, namesArr: %s, refArr: %s", index, namesArr, refArr);
  321.  
  322. var quarterNames = [ "Mar", "Jun", "Sep", "Dec" ];
  323.  
  324. var record =
  325. {
  326. "recordId": recordId,
  327. "gender": gender,
  328. "forenames": toTitleCase(namesArr[2]).trim(),
  329. "surname": toTitleCase(namesArr[1]).trim(),
  330. "age": age,
  331. "mother": mother,
  332. "year": parseInt(refArr[1], 10),
  333. "quarter": parseInt(refArr[2], 10),
  334. "district": toTitleCase(refArr[3]).trim(),
  335. "volume": refArr[4].toLowerCase(),
  336. "page": refArr[6],
  337. "copy": (refArr[8] ? refArr[8].trim() : ""),
  338. "actions": []
  339. };
  340. //console.log("resultId: %s, record.recordId: %s, record.year: %s, recordType: %s", resultId, record.recordId, record.year, recordType);
  341. // Determine what actions are supported for the record and add them
  342. if (record.recordId && record.year && recordType) {
  343. // Define possible actions
  344. var actions = [
  345. { "text": "Order Certificate", "url": null, "itemType": "Certificate", "pdfStatus": 0, "selector": "img[src$='order_certificate_button.gif']" },
  346. { "text": "Order PDF", "url": null, "itemType": "PDF", "pdfStatus": 5, "selector": "img[src$='order_pdf_button.gif']" }
  347. //{ "text": "Order MSF + Bundle", "url": null, "itemType": "MSFBundle", "pdfStatus": 0, "selector": "img[src$='order_certificate_button.gif']" }
  348. ];
  349. for (var i = 0; i < actions.length; i++) {
  350. if ($(this).next().find(actions[i].selector).length) {
  351. // Build order url
  352. var orderUrl = "https://www.gro.gov.uk/gro/content/certificates/indexes_order.asp?";
  353. orderUrl += "Index=" + recordType;
  354. orderUrl += "&Year=" + record.year;
  355. orderUrl += "&EntryID=" + record.recordId;
  356. orderUrl += "&ItemType=" + actions[i].itemType;
  357. if (actions[i].pdfStatus && actions[i].pdfStatus > 0)
  358. orderUrl += "&PDF=" + actions[i].pdfStatus;
  359. actions[i].url = orderUrl;
  360. record.actions.push(actions[i]);
  361. }
  362. //console.log("action '%s' (%s), url: %s", actions[i].itemType, actions[i].selector, actions[i].url);
  363. }
  364. }
  365.  
  366. record.noForenames = (!record.forenames || record.forenames == "-");
  367. record.ageWarning = (age != null && age > 0 && age <= results.ageWarningThreshold);
  368. record.birth = (age != null ? record.year - age : null);
  369. record.quarterName = ((record.quarter >=1 && record.quarter <= 4) ? quarterNames[record.quarter-1] : null);
  370. //console.log(record);
  371. results.items.push(record);
  372. }
  373. catch (e)
  374. {
  375. //console.log("Failed to parse record (%d): %s", index, e.message);
  376. results.failures.push({ "index": index, "ex": e });
  377. }
  378. });
  379. }
  380. return results;
  381. }
  382.  
  383.  
  384. var toTitleCase = function(str) {
  385. return str.replace(/([^\W_]+[^\s-]*) */g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
  386. }
  387. var switchRecordType = function() {
  388. var recordTypes = $("form[name='SearchIndexes'] input[type='Radio'][name='index']");
  389.  
  390. var curIndex = -1;
  391. for (var i = 0; i < recordTypes.length; i++) {
  392. if ($(recordTypes).eq(i).prop("checked")) {
  393. curIndex = i;
  394. break;
  395. }
  396. }
  397. //console.log("current record type: %d", curIndex);
  398.  
  399. if (curIndex >= 0) {
  400. var nextIndex = (curIndex == (recordTypes.length-1)) ? 0 : curIndex + 1;
  401.  
  402. if (nextIndex != curIndex)
  403. $(recordTypes).eq(nextIndex).prop("checked", true).click();
  404. //console.log("next record type: %d", nextIndex);
  405. }
  406. }
  407.  
  408. var toggleGender = function() {
  409. var curGender = $("form[name='SearchIndexes'] select#Gender").val();
  410. $("form[name='SearchIndexes'] select#Gender").val((curGender === "F" ? "M" : "F"));
  411. $("form[name='SearchIndexes'] input[type='submit'][value='Search']").click();
  412. }
  413. var adjustSearchYear = function(step) {
  414. var adjusted = false;
  415. // Get min and max years
  416. var minYear = parseInt($("form[name='SearchIndexes'] select#Year option:eq(2)").val(), 10);
  417. var maxYear = parseInt($("form[name='SearchIndexes'] select#Year option:last").val(), 10);
  418.  
  419. //console.log("Year range: %s - %s", minYear, maxYear);
  420.  
  421. if (!isNaN(step) && !isNaN(minYear) && !isNaN(maxYear)) {
  422. // Read current year and range
  423. var curYear = parseInt($("form[name='SearchIndexes'] select#Year").val(), 10);
  424. var curRange = parseInt($("form[name='SearchIndexes'] select#Range").val(), 10);
  425.  
  426. if (!isNaN(curRange)) {
  427. // Calculate the new year
  428. var newYear = (isNaN(curYear) ? minYear : curYear+step);
  429. newYear = Math.min(Math.max(newYear, minYear), maxYear);
  430. if (newYear != curYear) {
  431. $("form[name='SearchIndexes'] select#Year").val(newYear);
  432. adjusted = true;
  433. }
  434. }
  435.  
  436. //console.log("Current year: %d +-%d (%d-%d), New year: %d (%d-%d)", curYear, curRange, curYear-curRange, curYear+curRange, newYear, newYear-curRange, newYear+curRange);
  437. }
  438.  
  439. return adjusted;
  440. }
  441.  
  442. var navigateYears = function(forward) {
  443. var curRange = parseInt($("form[name='SearchIndexes'] select#Range").val(), 10);
  444. if (!isNaN(curRange)) {
  445. // Calculate the new year
  446. var step = (curRange * 2) + 1;
  447. if (!forward) step = -step;
  448. if (adjustSearchYear(step)) {
  449. $("form[name='SearchIndexes'] input[type='submit'][value='Search']").click();
  450. }
  451. }
  452. }
  453. var getRecordType = function() {
  454. return $("form[name='SearchIndexes'] input[type='radio'][name='index']:checked").val();
  455. }
  456.  
  457.  
  458. var buildResources = function() {
  459. resources = {
  460.  
  461. baseStyle: `
  462. <style type="text/css">
  463. body
  464. {
  465. min-height: 1200px;
  466. }
  467.  
  468. form[name="SearchIndexes"]
  469. {
  470. position: relative !important;
  471. }
  472. .groish_ButtonContainer
  473. {
  474. padding-bottom: 10px;
  475. }
  476. .groish_ButtonContainer input[type='submit'],
  477. .groish_ButtonContainer input[type='button']
  478. {
  479. margin-right: 20px;
  480. min-width: 100px;
  481. font-size: 13px;
  482. padding: 4px 10px;
  483. }
  484. .groish_ButtonContainer input[type='submit']
  485. {
  486. margin-right: 0px;
  487. }
  488. #groish_ResultsCopier,
  489. #groish_ViewSwitcher
  490. {
  491. display:inline-block;
  492. position: absolute;
  493. bottom: 0px;
  494. color: #993333;
  495. font-weight: bold;
  496. cursor: pointer;
  497. }
  498. #groish_ResultsCopier
  499. {
  500. right: 120px;
  501. }
  502. #groish_ViewSwitcher
  503. {
  504. right: 10px;
  505. }
  506. div[results-view] td[sort-fields]:hover
  507. {
  508. cursor: pointer;
  509. }
  510.  
  511. .groish_Message
  512. {
  513. position: absolute;
  514. bottom: -30px;
  515. left: 5px;
  516. }
  517.  
  518. </style>
  519. `,
  520.  
  521. view_EW_Birth_Table: `
  522. <style type="text/css">
  523. div[results-view='EW_Birth-Table'] td
  524. {
  525. padding: 5px 3px;
  526. font-size: 75%;
  527. color: #663333;
  528. vertical-align: top;
  529. }
  530. div[results-view='EW_Birth-Table'] thead td
  531. {
  532. font-weight: bold;
  533. }
  534. div[results-view='EW_Birth-Table'] tbody tr:nth-child(4n+1),
  535. div[results-view='EW_Birth-Table'] tbody tr:nth-child(4n+2)
  536. {
  537. background-color: #F9E8A5;
  538. }
  539. div[results-view='EW_Birth-Table'] tr.rec-actions a
  540. {
  541. padding: 0px 5px;
  542. font-size: 90%;
  543. color: #663333;
  544. text-decoration: none;
  545. }
  546. </style>
  547. <div results-view='EW_Birth-Table' style='display: none; margin-bottom: 25px' default-sort-fields='year,quarter'>
  548. <table style='width: 100%; border-collapse: collapse'>
  549. <thead>
  550. <tr>
  551. <td style='width: 12%' sort-fields='year,quarter'>Date</td>
  552. <td style='width: 30%' sort-fields='forenames,surname'>Name</td>
  553. <td style='width: 15%' sort-fields='mother'>Mother</td>
  554. <td style='width: 25%' sort-fields='district'>District</td>
  555. <td style='width: 6%' sort-fields='volume,district'>Vol</td>
  556. <td style='width: 6%' sort-fields='page,volume'>Page</td>
  557. <td style='width: 6%' sort-fields='copy,volume'>Copy</td>
  558. </tr>
  559. </thead>
  560. <tbody>
  561. {{#each items}}
  562. <tr class='rec'>
  563. <td>{{year}} Q{{quarter}}</td>
  564. <td><span class='forenames'>{{forenames}}</span> <span class='surname'>{{surname}}</span>{{#if noForenames}} ({{gender}}){{/if}}</td>
  565. <td>{{mother}}</td>
  566. <td>{{district}}</td>
  567. <td>{{volume}}</td>
  568. <td>{{page}}</td>
  569. <td>{{copy}}</td>
  570. </tr>
  571. <tr class='rec-actions' style='display: none'>
  572. <td colspan='7' style='text-align: right'>
  573. {{#actions}}
  574. <a href='{{url}}' {{#if title}}title='{{title}}'{{/if}}>{{text}}</a>
  575. {{/actions}}
  576. </td>
  577. </tr>
  578. {{/each}}
  579. </tbody>
  580. </table>
  581. {{#if failures}}
  582. <p class='main_text' style='color: Red'>WARNING: Failed to parse {{failures.length}} records. See default view for full list.</p>
  583. <!--
  584. {{#each failures}}record parse exception ({{index}}): exception: {{ex.message}}{{/each}}
  585. -->
  586. {{/if}}
  587. <p class='main_text groish_Message'></p>
  588. </div>`,
  589. view_EW_Birth_Delimited: `
  590. <style type="text/css">
  591. div[results-view='EW_Birth-Delimited'] td
  592. {
  593. padding: 5px 3px;
  594. font-size: 75%;
  595. color: #663333;
  596. vertical-align: top;
  597. }
  598. div[results-view='EW_Birth-Delimited'] thead td
  599. {
  600. font-weight: bold;
  601. }
  602. div[results-view='EW_Birth-Delimited'] tbody tr:nth-child(odd)
  603. {
  604. background-color: #F9E8A5;
  605. }
  606.  
  607. </style>
  608. <div results-view='EW_Birth-Delimited' style='display: none; margin-bottom: 25px' default-sort-fields='year,quarter'>
  609. <table style='width: 100%; border-collapse: collapse'>
  610. <thead>
  611. <tr>
  612. <td style='width: 100%' sort-fields='year,quarter'>Births</td>
  613. </tr>
  614. </thead>
  615. <tbody>
  616. {{#each items}}
  617. <tr class='rec'>
  618. <td>
  619. {{year}} Q{{quarter}} Birth -
  620. {{forenames}} {{surname}}{{#if noForenames}} ({{gender}}){{/if}}
  621. (mmn: {{mother}});
  622. {{district}}; {{volume}}; {{page}}; {{copy}}
  623. </tr>
  624. {{/each}}
  625. </tbody>
  626. </table>
  627. {{#if failures}}
  628. <p class='main_text' style='color: Red'>WARNING: Failed to parse {{failures.length}} records. See default view for full list.</p>
  629. <!--
  630. {{#each failures}}record parse exception ({{index}}): exception: {{ex.message}}{{/each}}
  631. -->
  632. {{/if}}
  633. <p class='main_text groish_Message'></p>
  634. </div>`,
  635.  
  636. view_EW_Death_Table: `
  637. <style type="text/css">
  638. div[results-view='EW_Death-Table'] td
  639. {
  640. padding: 5px 3px;
  641. font-size: 75%;
  642. color: #663333;
  643. vertical-align: top;
  644. }
  645. div[results-view='EW_Death-Table'] thead td
  646. {
  647. font-weight: bold;
  648. }
  649. div[results-view='EW_Death-Table'] tbody tr:nth-child(4n+1),
  650. div[results-view='EW_Death-Table'] tbody tr:nth-child(4n+2)
  651. {
  652. background-color: #F9E8A5;
  653. }
  654. div[results-view='EW_Death-Table'] tr.rec-actions a
  655. {
  656. padding: 0px 5px;
  657. font-size: 90%;
  658. color: #663333;
  659. text-decoration: none;
  660. }
  661. </style>
  662. <div results-view='EW_Death-Table' style='display: none; margin-bottom: 25px' default-sort-fields='year,quarter'>
  663. <table style='width: 100%; border-collapse: collapse'>
  664. <thead>
  665. <tr>
  666. <td style='width: 12%' sort-fields='year,quarter'>Date</td>
  667. <td style='width: 26%' sort-fields='forenames,surname'>Name</td>
  668. <td style='width: 8%' sort-fields='age'>Age{{#if ageCautionThreshold}}*{{/if}}</td>
  669. <td style='width: 8%' sort-fields='birth'>Birth</td>
  670. <td style='width: 28%' sort-fields='district'>District</td>
  671. <td style='width: 6%' sort-fields='volume,district'>Vol</td>
  672. <td style='width: 6%' sort-fields='page,volume'>Page</td>
  673. <td style='width: 6%' sort-fields='copy,volume'>Copy</td>
  674. </tr>
  675. </thead>
  676. <tbody>
  677. {{#each items}}
  678. <tr class='rec'>
  679. <td>{{year}} Q{{quarter}}</td>
  680. <td><span class='forenames'>{{forenames}}</span> <span class='surname'>{{surname}}</span>{{#if noForenames}} ({{gender}}){{/if}}</td>
  681. <td>{{age}}{{#if ageWarning}}*{{/if}}</td>
  682. <td>{{birth}}
  683. <td>{{district}}</td>
  684. <td>{{volume}}</td>
  685. <td>{{page}}</td>
  686. <td>{{copy}}</td>
  687. </tr>
  688. <tr class='rec-actions' style='display: none'>
  689. <td colspan='8' style='text-align: right'>
  690. {{#actions}}
  691. <a href='{{url}}' {{#if title}}title='{{title}}'{{/if}}>{{text}}</a>
  692. {{/actions}}
  693. </td>
  694. </tr>
  695. {{/each}}
  696. </tbody>
  697. </table>
  698. {{#if failures}}
  699. <p class='main_text' style='color: Red'>WARNING: Failed to parse {{failures.length}} records. See default view for full list.</p>
  700. <!--
  701. {{#each failures}}record parse exception ({{index}}): exception: {{ex.message}}{{/each}}
  702. -->
  703. {{/if}}
  704. <p class='main_text'>
  705. * Age is presumed to be years but <i>may</i> be months.
  706. {{#if ageWarningThreshold}}An age below {{ageWarningThreshold}} <i>may</i> be a child, treat with caution.{{/if}}
  707. An age of zero <i>may</i> have be used when a child was aged less than 12 months.
  708. </p>
  709. <p class='main_text groish_Message'></p>
  710. </div>`,
  711.  
  712. view_EW_Death_Delimited: `
  713. <style type="text/css">
  714. div[results-view='EW_Death-Delimited'] td
  715. {
  716. padding: 5px 3px;
  717. font-size: 75%;
  718. color: #663333;
  719. vertical-align: top;
  720. }
  721. div[results-view='EW_Death-Delimited'] thead td
  722. {
  723. font-weight: bold;
  724. }
  725. div[results-view='EW_Death-Delimited'] tbody tr:nth-child(odd)
  726. {
  727. background-color: #F9E8A5;
  728. }
  729. </style>
  730. <div results-view='EW_Death-Delimited' style='display: none; margin-bottom: 25px' default-sort-fields='year,quarter'>
  731. <table style='width: 100%; border-collapse: collapse'>
  732. <thead>
  733. <tr>
  734. <td style='width: 100%' sort-fields='year,quarter'>Deaths</td>
  735. </tr>
  736. </thead>
  737. <tbody>
  738. {{#each items}}
  739. <tr class='rec'>
  740. <td>
  741. {{year}} Q{{quarter}} Death -
  742. {{forenames}} {{surname}}{{#if noForenames}} ({{gender}}){{/if}};
  743. age {{age}}{{#if ageWarning}}*{{/if}} (b{{birth}});
  744. {{district}}; {{volume}}; {{page}}; {{copy}}
  745. </td>
  746. </tr>
  747. {{/each}}
  748. </tbody>
  749. </table>
  750. {{#if failures}}
  751. <p class='main_text' style='color: Red'>WARNING: Failed to parse {{failures.length}} records. See default view for full list.</p>
  752. <!--
  753. {{#each failures}}record parse exception ({{index}}): exception: {{ex.message}}{{/each}}
  754. -->
  755. {{/if}}
  756. <p class='main_text'>
  757. * Age is presumed to be years but <i>may</i> be months.
  758. {{#if ageWarningThreshold}}An age below {{ageWarningThreshold}} <i>may</i> be a child, treat with caution.{{/if}}
  759. An age of zero <i>may</i> have be used when a child was aged less than 12 months.
  760. </p>
  761. <p class='main_text groish_Message'></p>
  762. </div>`
  763.  
  764. };
  765.  
  766. // Add custom views
  767. // NB: Although GreaseMonkey has replaced the GM_* functions with functions on the GM object
  768. // both ViolentMonkey and TamperMonkey still support the GM_* functions so that's being used.
  769.  
  770. // Custom views are defined as GM values (one value per view). The value name must begin with
  771. // either view_EW_Birth or view_EW_Death. The view may contain CSS and HTML and the views are
  772. // Handlebars.js templates (see default views above for examples).
  773. //console.log("adding custom views");
  774. if (typeof GM_listValues === "function" && typeof GM_getValue === "function") {
  775. var valueKeys = GM_listValues();
  776. for(var i = 0; i < valueKeys.length; i++) {
  777. var valueKey = valueKeys[i];
  778. //console.log("value key: %", valueKey);
  779. if (valueKey && valueKey.length > 13 && (valueKey.startsWith("view_EW_Birth") || valueKey.startsWith("view_EW_Death"))) {
  780. // Check the key isn't already in use
  781. if (!resources.hasOwnProperty(valueKey)) {
  782. var viewContent = GM_getValue(valueKey, null);
  783. if (viewContent) {
  784. //console.log("adding view: %s", valueKey);
  785. resources[valueKey] = viewContent;
  786. }
  787. }
  788. }
  789. }
  790. }
  791. }
  792.  
  793. //Get the ball rolling...
  794. main();
  795. });