Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.59 KB | None | 0 0
  1. // ==UserScript==
  2. // @author Jamie22
  3. // @name FamilySearch Relationship Name
  4. // @version 0.1
  5. // @description Script for FamilySearch.org that describes the relationship between you and another person on your family tree when you click "View My Relationship", ie. Grandmother, 3 Times Great Grandfather, 4th Cousin Twice Removed, etc.
  6. // @match https://familysearch.org/tree/*
  7. // @copyright 2017 James Shorrock
  8. // @require http://code.jquery.com/jquery-latest.js
  9. // @require https://gist.github.com/raw/2625891/waitForKeyElements.js
  10. // @grant GM_addStyle
  11. // @license GPL3; https://www.gnu.org/licenses/gpl-3.0.en.html
  12. // ==/UserScript==
  13.  
  14. // This will be used to display the relationship name on the page
  15. var rlNameSpan = null;
  16.  
  17. waitForKeyElements("#show", addShowListener);
  18.  
  19. function addShowListener()
  20. {
  21. document.getElementById("show").addEventListener("click", getFamilyJSON);
  22. }
  23.  
  24. function getFamilyJSON()
  25. {
  26. var ancestorId = window.location.pathname.split('/')[3];
  27.  
  28. $.getJSON('https://familysearch.org/tree-data/my-relatives/tree-graph-relation/' + window.eval('loggedInPersonId') + '/' + ancestorId, nameRelationship);
  29. }
  30.  
  31. function grand(rel, numgen)
  32. {
  33. if (numgen >= 2)
  34. rel = "Grand" + rel;
  35.  
  36. if (numgen >= 3)
  37. rel = "Great " + rel;
  38.  
  39. if (numgen == 4)
  40. rel = "Great " + rel;
  41.  
  42. if (numgen >= 5)
  43. rel = (numgen - 2) + ' Times ' + rel;
  44.  
  45. return rel;
  46. }
  47.  
  48. function ordinalSuffixOf(i)
  49. {
  50. var j = i % 10,
  51. k = i % 100;
  52.  
  53. if (j == 1 && k != 11)
  54. return i + "st";
  55.  
  56. if (j == 2 && k != 12)
  57. return i + "nd";
  58.  
  59. if (j == 3 && k != 13)
  60. return i + "rd";
  61.  
  62. return i + "th";
  63. }
  64.  
  65. function nameRelationship(relChart)
  66. {
  67. var isDiffBranch = false;
  68. var isInLaw = false;
  69. // 1 for up, -1 for down
  70. var direction = 0;
  71. var numGenerationsUp = 0;
  72. var numGenerationsDown = 0;
  73. var persons = relChart.data.persons;
  74. var ancestorGender = persons[persons.length-1].gender;
  75.  
  76. // Travese the people in the relationship chart
  77. for(var i = 0; i < persons.length; i++)
  78. {
  79. if (!persons[i].relationshipToPrevious)
  80. continue;
  81.  
  82. if (persons[i].relationshipToPrevious == "FATHER" || persons[i].relationshipToPrevious == "MOTHER")
  83. {
  84. direction = 1;
  85. numGenerationsUp++;
  86. }
  87.  
  88. if (persons[i].relationshipToPrevious == "SON" || persons[i].relationshipToPrevious == "DAUGHTER")
  89. {
  90. if (direction == 1)
  91. isDiffBranch = true;
  92.  
  93. direction = -1;
  94. numGenerationsDown++;
  95. }
  96.  
  97. if (persons[i].relationshipToPrevious == "MAN" || persons[i].relationshipToPrevious == "WOMAN")
  98. isInLaw = true;
  99. }
  100.  
  101. var relationship = '';
  102. var numGenerations = 0;
  103.  
  104. // Direct ancestor or descendant
  105. if (!isDiffBranch)
  106. {
  107. if (direction == 1)
  108. {
  109. if (ancestorGender == "FEMALE")
  110. relationship = "mother";
  111. else
  112. relationship = "father";
  113.  
  114. numGenerations = numGenerationsUp;
  115. }
  116. else
  117. {
  118. if (ancestorGender == "FEMALE")
  119. relationship = "daughter";
  120. else
  121. relationship = "son";
  122.  
  123. numGenerations = numGenerationsDown;
  124. }
  125.  
  126. relationship = grand(relationship, numGenerations);
  127. }
  128. else
  129. {
  130. // Sibling
  131. if (numGenerationsUp == 1 && numGenerationsDown == 1)
  132. {
  133. if (ancestorGender == "FEMALE")
  134. relationship = "Sister";
  135. else
  136. relationship = "Brother";
  137. }
  138.  
  139. // Niece or nephew
  140. if (numGenerationsUp == 1 && numGenerationsDown >= 2)
  141. {
  142. if (ancestorGender == "FEMALE")
  143. relationship = "niece";
  144. else
  145. relationship = "nephew";
  146.  
  147. relationship = grand(relationship, numGenerationsDown-1);
  148. }
  149.  
  150. // Uncle or aunt
  151. if (numGenerationsUp >= 2 && numGenerationsDown == 1)
  152. {
  153. if (ancestorGender == "FEMALE")
  154. relationship = "aunt";
  155. else
  156. relationship = "uncle";
  157.  
  158. relationship = grand(relationship, numGenerationsUp-1);
  159. }
  160.  
  161. // Cousin
  162. if (numGenerationsUp >= 2 && numGenerationsDown >= 2)
  163. {
  164. var cousinNum = Math.min(numGenerationsUp, numGenerationsDown)-1;
  165. var removedNum = Math.abs(numGenerationsUp - numGenerationsDown);
  166.  
  167. relationship = ordinalSuffixOf(cousinNum) + ' Cousin';
  168.  
  169. if (removedNum == 1)
  170. relationship += ' Once Removed';
  171.  
  172. if (removedNum == 2)
  173. relationship += ' Twice Removed';
  174.  
  175. if (removedNum >= 3)
  176. relationship += ' ' + removedNum + ' Times Removed';
  177. }
  178.  
  179. if (isInLaw)
  180. relationship += ' In-Law';
  181. }
  182.  
  183. // Capitalize first letter
  184. relationship = relationship.charAt(0).toUpperCase() + relationship.slice(1);
  185.  
  186. // Display the relationship name at the bottom of the relationship chart popup
  187. if(rlNameSpan === null)
  188. {
  189. // Works in Firefox
  190. var infoIcon = document.getElementById("info-icon");
  191.  
  192. if(infoIcon === null)
  193. // Works in Chrome
  194. infoIcon = document.getElementsByTagName("birch-relationship-calc")[0].shadowRoot.querySelector("#info-icon");
  195.  
  196. rlNameSpan = document.createElement('span');
  197. rlNameSpan.style.fontSize = "18px";
  198. rlNameSpan.style.paddingLeft = "100px";
  199. infoIcon.parentNode.insertBefore(rlNameSpan, infoIcon.nextSibling);
  200. }
  201.  
  202. rlNameSpan.innerHTML = relationship;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement