IMDBAge

Adds the age and other various info onto IMDB pages.

  1. /* IMDBAge v2.14 - Greasemonkey script to add actors ages to IMDB pages
  2. Copyright (C) 2005-2020 Thomas Stewart <thomas@stewarts.org.uk>
  3.  
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8.  
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16.  
  17. Inspired in 2001, Created on 24/03/2005, Last Changed 28/12/2020
  18. Major bug fixes and improvements by Christopher J. Madsen
  19.  
  20. This is a Greasemonkey user script, see http://www.greasespot.net/ and
  21. https://addons.mozilla.org/firefox/addon/748
  22.  
  23. New versions can be found on the following sites:
  24. https://stewarts.org.uk/project/imdbage/
  25. http://userscripts.org/scripts/show/1060 (dead)
  26. https://greasyfork.org/scripts/2798-imdbage
  27. https://monkeyguts.com/code.php?id=268 (dead)
  28. https://openuserjs.org/scripts/thomas_d_stewart/IMDBAge
  29. https://github.com/thomasdstewart/IMDBAge
  30.  
  31. This script adds the age and other various info onto IMDB pages.
  32. Specifically it adds some details to actor or actresses pages. It adds
  33. their age, their Tropical Zodiac Sign and their Chinese Zodiac Sign. As
  34. well as adding how many years ago and how old they were when they made the
  35. listed films. It also adds how long a go a film was made on a film page.
  36.  
  37. This script is not abandoned, email thomas@stewarts.org.uk if it breaks.
  38.  
  39. Changelog
  40. * 2.14 fixed icon, improved getNameDates, new style fixes, reformating
  41. * 2.13 added https urls, removed scriptvals, fixed title pages
  42. * 2.12 fixed adding ages to individual films and fixed old style
  43. * 2.11 fixed date grabbing again
  44. * 2.10 fixed date grabbing
  45. * 2.9 fixed adding year to title with many years
  46. * 2.8 old style working, fixed death day for new style, improved year grabbing
  47. * 2.7 added persistent config, changed namespace
  48. * 2.6 fixed star signs and added unicode symbols
  49. * 2.5 fixed imdb updates
  50. * 2.4 fixed imdb updates
  51. * 2.3 improved year grabbing
  52. * 2.2 updated imdb text info, formatting, added ages to individual films
  53. * 2.1 Major changes, added signs, added config
  54. * 1.6 Added improvement ideas from Christopher J. Madsen, Added first imdb text files search, reformatting
  55. * 1.5 Removed function enclosing while script
  56. * 1.3 First public version
  57. */
  58.  
  59. var doNameAge = true;
  60. var doNameAges = true;
  61. var doSigns = true;
  62. var doFilmAge = true;
  63.  
  64. // ==UserScript==
  65. // @name IMDBAge
  66. // @description Adds the age and other various info onto IMDB pages.
  67. // @version 2.14
  68. // @author Thomas Stewart
  69. // @namespace http://www.stewarts.org.uk
  70. // @include http*://*imdb.com/name/*
  71. // @include http*://*imdb.com/title/*
  72. // @homepageURL https://stewarts.org.uk/project/imdbage/
  73. // @icon https://stewarts.org.uk/project/imdbage/icon.png
  74. // @license GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
  75. // ==/UserScript==
  76.  
  77. /*
  78. (https://secure.imdb.com/register-imdb/siteprefs)
  79. Title Test Cases:
  80. plain year https://www.imdb.com/title/tt0056172/
  81. year range https://www.imdb.com/title/tt0108757/
  82. year range still open https://www.imdb.com/title/tt0944947/
  83. year with version https://www.imdb.com/title/tt1008690/ (dead)
  84.  
  85. Name Test Cases: (In full for completeness)
  86. Born 18C -> Died 18C https://www.imdb.com/name/nm1038177/ (Laurence Sterne 54)
  87. Born 18C -> Died 19C https://www.imdb.com/name/nm0308075/ (Almeida Garrett 55)
  88. Born 18C -> Died 20C None
  89. Born 18C -> Died 21C None
  90. Born 18C -> Alive None
  91.  
  92. Born 19C -> Died 19C https://www.imdb.com/name/nm0786564/ (Anna Sewell 58)
  93. Born 19C -> Died 20C https://www.imdb.com/name/nm0186440/ (Ward Crane 38)
  94. Born 19C -> Died 21C https://www.imdb.com/name/nm0041807/ (Germaine Auger 112)
  95. Born 19C -> Alive https://www.imdb.com/name/nm0008724/ (Dawlad Abiad 118)
  96.  
  97. Born 20C -> Died 20C https://www.imdb.com/name/nm0001006/ (John Candy 43)
  98. Born 20C -> Died 21C https://www.imdb.com/name/nm0670239/ (John Peel 65)
  99. Born 20C -> Alive https://www.imdb.com/name/nm0088127/ (Alexis Bledel 36)
  100.  
  101. Born 21C -> Died 21C https://www.imdb.com/name/nm2548643/ (Tabea Block 1)
  102. Born 21C -> Alive https://www.imdb.com/name/nm1468628/ (Ben Want 15)
  103.  
  104. Born 31 Dec 1969 https://www.imdb.com/name/nm1009503/ (Taylor McCall 48)
  105. Died 31 Dec 1969 https://www.imdb.com/name/nm0862239/ (Carol Thurston 49)
  106. Born 1 Jan 1970 https://www.imdb.com/name/nm0231191/ (Fiona Dolman 48)
  107. Died 1 Jan 1970 https://www.imdb.com/name/nm0902025/ (Eduard von Borsody 71)
  108.  
  109. http://us.imdb.com/date/{month}-{day}
  110. http://us.imdb.com/search/name?birth_year={year}
  111.  
  112. $ curl https://datasets.imdbws.com/name.basics.tsv.gz | gunzip > name.basics.tsv
  113. $ for c in 17 18 19 20; do awk -F'\t' '{print $3 " " $1}' name.basics.tsv | grep ^$c | awk '{print $2}' > b.$c; awk -F'\t' '{print $4 " " $1}' name.basics.tsv | grep ^$c | awk '{print $2}' > d.$c; done
  114. $ cat b.17 d.17 | sort | uniq -d #list of people born and died in 18C
  115. $ wc -l b.17 d.17 b.18 d.18 b.19 d.19 b.20 d.20
  116. 353 b.17
  117. 137 d.17
  118. 37757 b.18
  119. 921 d.18
  120. 471993 b.19
  121. 96672 d.19
  122. 4516 b.20
  123. 86201 d.20
  124. 698550 total
  125. $
  126.  
  127. */
  128.  
  129. /*
  130. TODO: add ages to individual ages of actors to a film page, very hard,
  131. http req for each one, and then a xpath on the whole result
  132. TODO: add script updater support
  133. */
  134.  
  135. /* calculates tropical zodiac sign see http://en.wikipedia.org/wiki/Signs_of_the_Zodiac
  136. input: month and day
  137. returns: tropical zodiac sign as string */
  138. function tropicalZodiac(month, day) {
  139. if (month == 3 && day >= 21 ||
  140. month == 4 && day <= 19) { return "Aries - ♈"; }
  141. else if(month == 4 && day >= 20 ||
  142. month == 5 && day <= 20) { return "Taurus - ♉"; }
  143. else if(month == 5 && day >= 21 ||
  144. month == 6 && day <= 20) { return "Gemini - ♊"; }
  145. else if(month == 6 && day >= 21 ||
  146. month == 7 && day <= 22) { return "Cancer - ♋"; }
  147. else if(month == 7 && day >= 23 ||
  148. month == 8 && day <= 22) { return "Leo - ♌"; }
  149. else if(month == 8 && day >= 23 ||
  150. month == 9 && day <= 22) { return "Virgo - ♍"; }
  151. else if(month == 9 && day >= 23 ||
  152. month == 10 && day <= 22) { return "Libra - ♎"; }
  153. else if(month == 10 && day >= 23 ||
  154. month == 11 && day <= 21) { return "Scorpio - ♏"; }
  155. else if(month == 11 && day >= 22 ||
  156. month == 12 && day <= 21) { return "Sagittarius - ♐"; }
  157. else if(month == 12 && day >= 22 ||
  158. month == 1 && day <= 19) { return "Capricorn - ♑"; }
  159. else if(month == 1 && day >= 20 ||
  160. month == 2 && day <= 18) { return "Aquarius - ♒"; }
  161. else if(month == 2 && day >= 19 ||
  162. month == 3 && day <= 20) { return "Pisces - ♓"; }
  163. else { return ""; }
  164. }
  165.  
  166. /* calculates chinese zodiac sign see http://en.wikipedia.org/wiki/Chinese_astrology
  167. input: full year
  168. returns: chinese zodiac sign as string */
  169. function chineseZodiac(year) {
  170. /* no idea how to work out signs before 20C */
  171. if (year < 1900) { return ""; }
  172.  
  173. /* there are 12 signs that go round in a rotation */
  174. /* find years since 1900, find modulus of that, get rid of the */
  175. /* sign(sic) and round it */
  176. var nsign = Math.round(Math.abs((year - 1900) % 12));
  177.  
  178. if (nsign == 0) { return "Rat (Metal)"; }
  179. else if (nsign == 1) { return "Ox (Metal)"; }
  180. else if (nsign == 2) { return "Tiger (Water)"; }
  181. else if (nsign == 3) { return "Rabbit/Cat (Water)"; }
  182. else if (nsign == 4) { return "Dragon (wood)"; }
  183. else if (nsign == 5) { return "Snake (Wood)"; }
  184. else if (nsign == 6) { return "Horse (Fire)"; }
  185. else if (nsign == 7) { return "Goat (Fire)"; }
  186. else if (nsign == 8) { return "Monkey (Earth)"; }
  187. else if (nsign == 9) { return "Rooster (Earth)"; }
  188. else if (nsign == 10) { return "Dog (Metal)"; }
  189. else if (nsign == 11) { return "Pig/Wild Boar (Metal)"; }
  190. else { return ""; }
  191. }
  192.  
  193. /* get dates from a name page
  194. input: born and died called by ref, they are filled with dates from the page
  195. returns: whether they are dead or alive */
  196. function getNameDates(born, died) {
  197. var alive = true;
  198.  
  199. /* if new style */
  200. if (newStyle()) {
  201. var nodes = document.evaluate("//script[@type='application/ld+json']",
  202. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  203.  
  204. if (nodes.snapshotLength == 1) {
  205. jsond = JSON.parse(nodes.snapshotItem(0).firstChild.textContent);
  206.  
  207. /* find the birth date */
  208. date = jsond.birthDate.split("-");
  209. born.setFullYear(date[0]);
  210. born.setMonth(date[1] - 1);
  211. born.setDate(date[2]);
  212.  
  213. /* find the death date */
  214. date = jsond.deathDate;
  215. if(date) {
  216. date = date.split("-")
  217. died.setFullYear(date[0]);
  218. died.setMonth(date[1] - 1);
  219. died.setDate(date[2]);
  220. alive = false;
  221. }
  222.  
  223. }
  224. /* else old style */
  225. } else {
  226. /* find the birth date */
  227. var nodes = document.evaluate(
  228. "//div[contains(@class,'info-content')]/a[contains(@href,'birth')]",
  229. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  230. null);
  231. if (nodes.snapshotLength == 3) {
  232. monthday = nodes.snapshotItem(0).getAttribute("href")
  233. month = monthday.substring(28, 30)
  234. day = monthday.substring(31, 33)
  235.  
  236. year = nodes.snapshotItem(1).getAttribute("href")
  237. year = year.substring(24, 28);
  238.  
  239. born.setFullYear(year);
  240. born.setMonth(month - 1);
  241. born.setDate(day);
  242. alive = true
  243. }
  244.  
  245. /* find the death date */
  246. var nodes = document.evaluate(
  247. "//div[contains(@class,'info-content')]/a[contains(@href,'death')]",
  248. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
  249. null);
  250. if (nodes.snapshotLength == 2) {
  251. monthday = nodes.snapshotItem(0).getAttribute("href")
  252. month = monthday.substring(6, 8)
  253. day = monthday.substring(09,11)
  254.  
  255. year = nodes.snapshotItem(1).getAttribute("href")
  256. year = year.substring(24, 28);
  257.  
  258. died.setFullYear(year);
  259. died.setMonth(month - 1);
  260. died.setDate(day);
  261. alive = false
  262. }
  263. }
  264.  
  265. //alert("Born: " + born + "\nDied: " + died + "\nAlive: " + alive);
  266. return alive;
  267. }
  268.  
  269. /* get dates from a title page
  270. input: none
  271. returns: date of title */
  272. function getTitleDates() {
  273. var nodes = document.evaluate("//h4[text()='Release Date:']",
  274. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  275. if (nodes.snapshotLength == 1) {
  276. date = nodes.snapshotItem(0).nextSibling.textContent;
  277. }
  278.  
  279. var nodes = document.evaluate("//a[text()='Release date']",
  280. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  281. if (nodes.snapshotLength == 1) {
  282. date = nodes.snapshotItem(0).nextSibling.textContent;
  283. }
  284.  
  285. titledate = new Date(Date.parse(date));
  286.  
  287. //alert("Year: " + titledate.getFullYear())
  288. return titledate;
  289. }
  290.  
  291. /* add age of person to page
  292. input: alive status, and dates
  293. returns: none */
  294. function addAge(alive, born, died) {
  295. var justyear;
  296.  
  297. /* find the difference between two times */
  298. var age;
  299. if (died == undefined) {
  300. age = new Date() - born.getTime();
  301. } else {
  302. age = died.getTime() - born.getTime();
  303. }
  304.  
  305. //alert("Born: " + born + "\nDied: " + died + "\nAlive: " + alive);
  306.  
  307. /* convert difference into years */
  308. age = age / (1000 * 60 * 60 * 24 * 365.242199);
  309.  
  310. /* get nice values */
  311. var years = Math.floor( age );
  312. var months = Math.floor( (age - years) * 12 );
  313.  
  314. var nodes = document.evaluate("//a[contains(@href,'/date')]",
  315. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  316.  
  317. /* only print the year if there aren't dates */
  318. if ((alive == true && nodes.snapshotLength == 1) ||
  319. (alive == false && nodes.snapshotLength == 2)) {
  320. justyear = false;
  321. } else {
  322. justyear = true;
  323. }
  324.  
  325. /* loop over all the a tags involving dates */
  326. var nodes = document.evaluate(
  327. "//a[contains(@href,'birth_year')] | //a[contains(@href,'death_date')]",
  328. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  329.  
  330. /* only count months if we found month & day info */
  331. var container = document.createTextNode(
  332. " (Age: " + years + " year" + (years == 1 ? '' : 's') +
  333. (!justyear ? ", " + months + " month" + (months == 1 ? '' : 's') : '') + ")");
  334.  
  335. /* loop over all dates */
  336. if (alive == true) {
  337. node = nodes.snapshotItem(0);
  338. node.parentNode.insertBefore(container, node.nextSibling);
  339. } else {
  340. node = nodes.snapshotItem(1);
  341.  
  342. //only add death age on old layout, as new layout has it!
  343. if (!newStyle()) {
  344. node.parentNode.insertBefore(container, node.nextSibling);
  345. }
  346. }
  347. }
  348.  
  349. /* add age of film and the age of the actor when they were in the film
  350. input: date
  351. returns: none */
  352. function addAges(born) {
  353. //find all the films, this in includes things like producer and writer
  354. var nodes = document.evaluate(
  355. /* new style and old style */
  356. "//span[contains(@class,'year_column')]|//div[@id='tn15content']/div/ol/li",
  357. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  358.  
  359. //loop round each film
  360. for (var i = 0; i < nodes.snapshotLength; i++) {
  361. var node = nodes.snapshotItem(i);
  362. //extract the year of the film depending on style
  363. if (newStyle()) {
  364. yearindex = node.innerHTML.search("[1-2][0-9]{3}")
  365. //if we don't find a year, continue with for loop
  366. if (yearindex < 0) {
  367. continue;
  368. }
  369. } else {
  370. yearindex = node.innerHTML.search(
  371. "[1-2][0-9]{3}[/I]{0,2}[)]")
  372. }
  373. var filmborn = node.innerHTML.substring(yearindex,
  374. yearindex + 4);
  375. //alert(filmborn);
  376.  
  377. //calculate ages
  378. var filmage = new Date().getFullYear() - filmborn;
  379. var age = filmborn - born;
  380. age = new String(age +
  381. " year" + (age == 1 ? '' : 's') + " old");
  382.  
  383. //get them in a nice format
  384. if (filmage < 0) {
  385. var agetxt = new String(
  386. "in " +
  387. Math.abs(filmage) + " year" +
  388. (Math.abs(filmage) == 1 ? '' : 's') +
  389. " will be " + age);
  390. }
  391. if (filmage == 0) {
  392. var agetxt = new String(
  393. "this year while " + age);
  394. }
  395. if (filmage > 0) {
  396. var agetxt = new String(
  397. Math.abs(filmage) + " year" +
  398. (Math.abs(filmage) == 1 ? '' : 's') +
  399. " ago while " + age);
  400. }
  401.  
  402. //if(i == 4) { alert(agetxt); }
  403. /* add in age text */
  404. node.innerHTML = node.innerHTML.substring(0,yearindex)
  405. + agetxt + ", " + node.innerHTML.substring(yearindex)
  406. }
  407. }
  408.  
  409. /* adds signs to page
  410. input: date person is born
  411. returns: none */
  412. function addSigns(born) {
  413. /* find place to stick the info */
  414. var nodes = document.evaluate( "//a[contains(@href,'birth_year')]",
  415. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  416.  
  417. /* make a node with info in */
  418. var container = document.createTextNode(
  419. ", Tropical Zodiac Sign: " + tropicalZodiac(born.getMonth() + 1, born.getDate()) +
  420. ", Chinese Zodiac Sign: " + chineseZodiac(born.getFullYear())
  421. );
  422.  
  423. /* should be the first occurance of the latter */
  424. var node = nodes.snapshotItem(0);
  425.  
  426. /* attach it */
  427. if (nodes.snapshotLength > 0) {
  428. node.parentNode.insertBefore(container, node.nextSibling);
  429. }
  430. }
  431.  
  432. /* add the age of the film to the page
  433. input: date of film
  434. returns: none */
  435. function addFilmAge(filmAge) {
  436. /* calc age */
  437. var age = new Date().getFullYear() - filmAge.getFullYear();
  438.  
  439. /* only print if age is 1 or over */
  440. if (age >= 1) {
  441. /* make a node with info in */
  442. var container = document.createTextNode(", " +
  443. age +
  444. " year" + (age == 1 ? '' : 's') +
  445. " ago");
  446. }
  447. if (age == 0) {
  448. var container = document.createTextNode(", This year");
  449. }
  450. if (age <= -1) {
  451. var container = document.createTextNode(", in " +
  452. Math.abs(age) +
  453. " year" + (Math.abs(age) == 1 ? '' : 's'));
  454. }
  455.  
  456. /* find place to stick the info */
  457. var nodes = document.evaluate(
  458. /* old style and new style */
  459. "//div[contains(@id,'tn15title')]//a[contains(@href,'year')]|//div[@class='subtext']/a[@title='See more release dates']|//div[contains(@class,'TitleBlock__TitleMetaDataContainer')]/ul/li",
  460. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  461.  
  462. /* create new span with formatting to match */
  463. var span = document.createElement('span');
  464. span.style.fontSize = "11px";
  465. span.appendChild(container);
  466.  
  467. /* should be the first occurrence of the latter */
  468. var node = nodes.snapshotItem(0);
  469. /* attach it */
  470. node.parentNode.insertBefore(span, node.nextSibling);
  471. }
  472.  
  473. /* find out if we are using the newstyle on not, works on name and title pages
  474. input: none
  475. returns: true if using the new style */
  476. function newStyle() {
  477. var nodes = document.evaluate( "//div[@id='tn15content']", document,
  478. null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  479. if (nodes.snapshotLength == 1) {
  480. return false
  481. } else {
  482. return true
  483. }
  484. }
  485.  
  486. /* code starts, two options, either it is a name page ... */
  487. if (window.location.href.indexOf('name') != -1) {
  488. born = new Date();
  489. died = new Date();
  490. /* get needed dates */
  491. var alive = getNameDates(born, died);
  492.  
  493. /* add wanted bits */
  494. if(doSigns == true) {
  495. addSigns(born);
  496. }
  497. if(doNameAge == true) {
  498. addAge(alive, born, died);
  499. }
  500. if(doNameAges == true) {
  501. addAges(born.getFullYear());
  502. }
  503.  
  504. /* ... or it is a title page */
  505. } else if (window.location.href.indexOf('title') != -1) {
  506. /* get needed dates */
  507. filmAge = getTitleDates();
  508.  
  509. /* add wanted bits */
  510. if(doFilmAge == true && typeof(filmAge) == "object") {
  511. addFilmAge(filmAge);
  512. }
  513. }