Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// ==UserScript==
// @name Geneweb person importer for rodovid
// @description Helps rodovid users to import ancestor data from genealogies published on Geneanet.
// @namespace http://tampermonkey.net/
// @version 2019-09-30T10:17
// @description try to take over the world!
// @author charles@nepote.org
// @match https://fr.rodovid.org/edit/Personne:New
// @match *.GED.txt
// @require http://code.jquery.com/jquery-latest.min.js
// @require http://code.jquery.com/ui/1.12.1/jquery-ui.min.js
// @require https://wzrd.in/standalone/parse-gedcom@latest
// @grant none
// ==/UserScript==
// Firefox: add it via Greamonkey or Tampermonkey extension: https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/
// Chrome (not tested): add it with Tampermonkey: https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo
// Features:
// * manages first name, last name, birth, christening, death and burial (dates and places)
// TODO:
// * manage gedcom dates: before, after, about...
// * find other examples; see
(function() {
'use strict';
var gedcomForm, genewebdate;
var giImportURLFieldDefault = "";
//var giImportURLFieldDefault = "https://gw.geneanet.org/nepote_w?lang=fr&p=alice+ernestine&n=grisel"; // For testing purposes
// URL to test:
// * [OK] Née, baptisée, décédée, inhumée :
// https://gw.geneanet.org/nepote_w?lang=fr&pz=rosalie+blanche+marie&nz=nepote&p=marthe&n=cuzel
// * [OK] Né, décédé :
// https://gw.geneanet.org/pierfit?lang=fr&p=jean+baptiste&n=apchin
// * [KO] http://roglo.eu/roglo?lang=fr;i=7614899
// * [KO] Né avant, décédé entre ...
// https://gw.geneanet.org/nepote_w?lang=fr&pz=rosalie+blanche+marie&nz=nepote&p=jean&n=cuzel
// * [KO] Née vers :
// https://gw.geneanet.org/nepote_w?lang=fr&pz=monique&nz=lanos&p=therese&n=linage
$(".firstHeading").append('<input type="input" name="giImportUrlField" value="'+giImportURLFieldDefault+'">'+
'<button id="giSubmit">import</button>');
$("#wpTextbox1") ? gedcomForm = true : gedcomForm = false;
$("#giSubmit").click(function() {
var giImportURL = $('input[name="giImportUrlField"]').val();
console.log(giImportURL);
importPerson(giImportURL);
});
function importPerson (url) {
// Force call to french (TODO: or english?) page
var langPattern = /lang=../g;
if(langPattern.exec(url)) {
url = url.replace(/lang=../gi, "lang=fr");
} else {
url = url + "&lang=fr";
}
var request = $.get("https://cors-anywhere.herokuapp.com/" + url, function(response) {
console.log( "https://cors-anywhere.herokuapp.com/" + url ); // success if printed
console.log(response);
/**/
var person = { };
person.firstname = $.trim($(response).find(".gw-individual-info-name-firstname").text());
person.lastname = $.trim($(response).find(".gw-individual-info-name-lastname").text());
person.SEX = $(response).find("#person-title div h1 img").attr("title");
// French date patterns:
// (Né|Baptisé|Décédé|Inhumé)e?&nbsp;(le|en)&nbsp;(\d?\d?(&nbsp;)?)(janvier|...
person.birth = { };
var birthPattern = /Née? (le|en)&nbsp;(.*?) - (.*?)<\/li>/g;
var birthData = (birthPattern.exec(response)||""); console.log("birthData: " + birthData);
if (birthData) {
person.birth.place = birthData[3];
genewebdate = parseGenewebDate(birthData[2]);
person.birth.day = genewebdate.day;
person.birth.month = genewebdate.month;
person.birth.year = genewebdate.year;
}
person.chr = { } ;
var chrPattern = /Baptisée? (le|en)&nbsp;(.*?) - (.*?)<\/li>/g;
//var chrPattern = /Born (in)? (.*?) - (.*?)<\/li>/g; // Ex. Born in&nbsp;June&nbsp;1653; Baptized 28&nbsp;June&nbsp;1653
var chrData = (chrPattern.exec(response)||""); console.log("chrData: " + chrData);
if (chrData) {
person.chr.place = chrData[3];
genewebdate = parseGenewebDate(chrData[2]);
person.chr.day = genewebdate.day;
person.chr.month = genewebdate.month;
person.chr.year = genewebdate.year;
}
/**/
person.deat = { };
var deatPattern = /<li>Décédée? (le|en)&nbsp;(.*?) - (.*?),/g; // <li>Décédée le&nbsp;7&nbsp;avril&nbsp;1920 - Canteleu,France, à l'âge de 34&nbsp;ans</li>
var deatData = (deatPattern.exec(response)||""); console.log("deatData: " + deatData);
if (deatData) {
person.deat.place = deatData[3];
genewebdate = parseGenewebDate(deatData[2]);
person.deat.day = genewebdate.day;
person.deat.month = genewebdate.month;
person.deat.year = genewebdate.year;
}
person.buri = { };
var buriPattern = /Inhumée? (le|en)&nbsp;(.*?) - (.*?)<\/li>/g;
var buriData = (buriPattern.exec(response)||""); console.log("buriData: " + buriData);
if (buriData) {
person.buri.place = buriData[3];
genewebdate = parseGenewebDate(buriData[2]);
person.buri.day = genewebdate.day;
person.buri.month = genewebdate.month;
person.buri.year = genewebdate.year;
}
console.log("------------------ person ------------------");
console.log(person);
if (gedcomForm === false) {
// First tab (names)
$('input[name="nameatbirth"]').val(person.firstname);
$('input[name="fnameatbirth"]').val(person.lastname);/**/
var sex = person.SEX.toLowerCase(); console.log("sex: " + sex);
//$('select[name="sex"]').val(sex).change(); // http://www.finalclap.com/faq/247-jquery-select-option
$('select[name="sex"] option[value="' + sex + '"]').prop('selected', true); // http://www.finalclap.com/faq/247-jquery-select-option
// Second tab (events)
$('input[name="place[0.001]"]').val(person.birth.place);
$('input[name="day[0.001]"]').val(person.birth.day);
/*$('select[name="month[0.001] option[value""' + sex + '"]').val(person.birth.month);*/
$('input[name="year[0.001]"]').val(person.birth.year);
}
if (gedcomForm === true) {
//
var gedcom = "0 @@ INDI \n" +
"1 NAME " + person.firstname + "/" + person.lastname + "/\n" +
"1 SEX " + person.SEX + "\n" +
(isEmpty(person.birth) ? "" : "1 BIRT \n") +
(isEmpty(person.birth) ? "" : "2 DATE " + person.birth.day + " " + person.birth.month + " " + person.birth.year + "\n") +
(isEmpty(person.birth.place) ? "" : "2 PLAC " + person.birth.place + "\n") +
(isEmpty(person.chr) ? "" : "1 CHR \n") +
(isEmpty(person.chr) ? "" : "2 DATE " + person.chr.day + " " + person.chr.month + " " + person.chr.year + "\n") +
(isEmpty(person.chr.place) ? "" : "2 PLAC " + person.chr.place + "\n") +
(isEmpty(person.deat) ? "" : "1 DEAT \n") +
(isEmpty(person.deat) ? "" : "2 DATE " + person.deat.day + " " + person.deat.month + " " + person.deat.year + "\n") +
(isEmpty(person.deat.place) ? "" : "2 PLAC " + person.deat.place + "\n") +
(isEmpty(person.buri) ? "" : "1 BURY \n") +
(isEmpty(person.buri) ? "" : "2 DATE " + person.buri.day + " " + person.buri.month + " " + person.buri.year + "\n") +
(isEmpty(person.buri.place) ? "" : "2 PLAC " + person.buri.place + "\n") +
"1 NOTE RDLINEAGE @@\n" +
"2 CONT Imported from " + url + "\n";
$("#wpTextbox1").val(gedcom); // write form
console.log(gedcom);
}
/**
console.log("----------");
// Edit URL here : <a id="edition-edit-person" href="/nepote_w?i=76&amp;lang=fr&amp;m=MOD_IND&amp;nz=lanos&amp;pz=monique">
// Edit URL: https://gw.geneanet.org/nepote_w?i=76&lang=fr&m=MOD_IND
var geneanetAccountName = "nepote";
var numberPattern = /"index":(\d+)/g;
var match = numberPattern.exec(response);
console.log(match[0] + " -- " + match[1]);
var editPageURL = "https://gw.geneanet.org" + "/" + geneanetAccountName + "_w?m=MOD_IND&i=" + match[1];
console.log(editPageURL);
importDataFromEditPage(editPageURL);// doesn't work
/**/
})
.fail(function() {
console.log( "error" );
})
.always(function() {
console.log( "finished" );
});
request.crossDomain = true;
}
/**
* parse Geneweb date such as 20 septembre 1785 or 7 août 1833
*
* @result date
*/
function parseGenewebDate(string) {
console.log("parseGenewebDate --> Analyzing date: " + string);
var datePattern = /(\d?\d?)(&nbsp;)?(.*?)(&nbsp;)?(\d\d\d\d)/;
var dateData = datePattern.exec(string); console.log("datePattern: " + datePattern);
var date = { };
date.day = dateData[1];
date.month = dateData[3];
date.year = dateData[5];
// convert décembre to DEC
const month = {
'janvier': 'JAN',
'février': 'FEB',
'mars': 'MAR',
'avril': 'APR',
'mai': 'MAY',
'juin': 'JUN',
'juillet': 'JUL',
'août': 'AUG',
'septembre': 'SEP',
'octobre': 'OCT',
'novembre': 'NOV',
'décembre': 'DEC'
};
const enMonth = {
'January': 'JAN',
'February': 'FEB',
'March': 'MAR',
'April': 'APR',
'May': 'MAY',
'June': 'JUN',
'July': 'JUL',
'August': 'AUG',
'September': 'SEP',
'October': 'OCT',
'November': 'NOV',
'December': 'DEC'
}
date.month = month[date.month];
console.log(date);
return date;
}
/**
* test if object is empty
*
* @param {object} obj - object; example: { person { birth { date: "21 JUN 1653" } } }
* @returns {Boolean} Return either true either false
**/
function isEmpty(obj) {
for (let key in obj) {
return false; // if the loop has started, object is not empty
}
return true;
}
// Doesn't work, DO NOT USE
/**
function importDataFromEditPage(url) {
url = "https://cors-anywhere.herokuapp.com/" + url;
var request = $.get(url, function(response) {
console.log( "https://cors-anywhere.herokuapp.com/" + url ); // success if printed
console.log(response);
var firstname = $(response).find("#first_name").val(); console.log("firstname: " + firstname);
var lastname = $(response).find("#surname").val();
$('input[name="nameatbirth"]').val(firstname);
$('input[name="fnameatbirth"]').val(lastname);
})
.fail(function() {
console.log( "error" );
})
request.crossDomain = true;
}
/**/
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment