MediaWiki:MarcEditorDynamicLinks.js: mudanças entre as edições

De Wikincat
Ir para navegação Ir para pesquisar
imported>Jaideraf
Sem resumo de edição
imported>Jaideraf
Sem resumo de edição
Linha 1: Linha 1:
// Author: @vitorsilverio
// Author: @vitorsilverio
// Author: @jaideraf
// Author: @jaideraf
if (document.readyState === "complete" || (document.readyState !== "loading")) {
main();
} else {
document.addEventListener("DOMContentLoaded", main);
}

function main() {
function main() {
let tag = "";
let tag = '';
let tagFromDropdown = "";
let tagFromDropdown = '';


function createMarcFieldHelpLink() {
function createMarcFieldHelpLink() {
const tagInputs = document.body.querySelectorAll(".tagInput");
const tagInputs = document.body.querySelectorAll('.tagInput');
tagInputs.forEach(function (elem) {
tagInputs.forEach((elem) => {
const marcFieldHelpLink = document.createElement("a");
const marcFieldHelpLink = document.createElement('a');
marcFieldHelpLink.setAttribute("target", "_blank");
marcFieldHelpLink.setAttribute('target', '_blank');
marcFieldHelpLink.innerText = `(?)`;
marcFieldHelpLink.innerText = '(?)';


const marcFieldHelpLinkClass = elem.closest("table")
const marcFieldHelpLinkClass = elem.closest('table')
.querySelector(".marcFieldHelpLink");
.querySelector('.marcFieldHelpLink');
marcFieldHelpLinkClass.appendChild(marcFieldHelpLink);
marcFieldHelpLinkClass.appendChild(marcFieldHelpLink);


// find the tag value from the chosen dropdown
// find the tag value from the chosen dropdown
marcFieldHelpLinkClass.addEventListener("mouseover", function (event) {
marcFieldHelpLinkClass.addEventListener('mouseover', (event) => {
tagFromDropdown = event.target.closest(".instanceMain")
tagFromDropdown = event.target.closest('.instanceMain')
.querySelector(".select2-selection__rendered")
.querySelector('.select2-selection__rendered')
.getAttribute("title");
.getAttribute('title');
tagFromDropdown = tagFromDropdown.substring(0, 3);
tagFromDropdown = tagFromDropdown.substring(0, 3);
marcFieldHelpLink.setAttribute("title",
marcFieldHelpLink.setAttribute(
'title',
`Documentação do campo ${tagFromDropdown}`);
// build the URL
`Documentação do campo ${tagFromDropdown}`
);
if (window.location.href.match(/A\d{6}/) ||
// build the URL
window.location.href.match(/AutRecord/)) {
if (window.location.href.match(/A\d{6}/)
marcFieldHelpLink.setAttribute("href",
"http://marc.febab.org/a" + tagFromDropdown);
|| window.location.href.match(/AutRecord/)) {
} else {
marcFieldHelpLink.setAttribute(
marcFieldHelpLink.setAttribute("href",
'href',
`https://www.loc.gov/marc/bibliographic/bd${tagFromDropdown}.html`);
`http://marc.febab.org/a${tagFromDropdown}`
}
);
});
} else {
marcFieldHelpLink.setAttribute(
});
}
'href',
`https://www.loc.gov/marc/bibliographic/bd${tagFromDropdown}.html`
);
}
});
});
}


function createAuthorityLinks() {
function createAuthorityLinks() {
const textareas = document.body.querySelectorAll("textarea.has-authority-control");
const textareas = document.body.querySelectorAll('textarea.has-authority-control');
textareas.forEach(function (elem) {
textareas.forEach((elem) => {
let authorityLink = document.createElement("a");
const authorityLink = document.createElement('a');
authorityLink.setAttribute("title", "Abre uma nova aba para criar ou editar um registro de autoridade");
authorityLink.setAttribute('title', 'Abre uma nova aba para criar ou editar um registro de autoridade');
authorityLink.setAttribute("target", "_blank");
authorityLink.setAttribute('target', '_blank');
authorityLink.innerText = "Criar autoridade";
authorityLink.innerText = 'Criar autoridade';
authorityLink.style.display = "none";
authorityLink.style.display = 'none';


let createAuthorityLink = elem.closest("table")
const createAuthorityLink = elem.closest('table')
.querySelector(".createAuthorityLink");
.querySelector('.createAuthorityLink');


createAuthorityLink.appendChild(authorityLink);
createAuthorityLink.appendChild(authorityLink);


const pattern1 = /\$0\sA\d{6}/;
const pattern1 = /\$0\sA\d{6}/;
const pattern2 = /\$0\s\(BN\)\d{9}/;
const pattern2 = /\$0\s\(BN\)\d{9}/;


elem.addEventListener("focusout", function () {
elem.addEventListener('focusout', () => {
// if already recorded
// if already recorded
if (pattern1.test(elem.value)) {
if (pattern1.test(elem.value)) {
// build the URL for Wikincat aut record edit
// build the URL for Wikincat aut record edit
authorityLink.innerText = "Editar autoridade";
authorityLink.innerText = 'Editar autoridade';
authorityLink.setAttribute("href",
authorityLink.setAttribute(
'href',
"/wiki/Special:FormEdit/AutRecord/Autoridade:" +
`/wiki/Special:FormEdit/AutRecord/Autoridade:${
elem.value.match(/A\d{6}/));
} else if (pattern2.test(elem.value)) {
elem.value.match(/A\d{6}/)}`
);
// build the URL for BN aut record view
} else if (pattern2.test(elem.value)) {
authorityLink.innerText = "Ver autoridade na BN";
authorityLink.setAttribute("href",
// build the URL for BN aut record view
authorityLink.innerText = 'Ver autoridade na BN';
"http://acervo.bn.br/sophia_web/autoridade/detalhe/" +
elem.value.match(/\d{9}/));
authorityLink.setAttribute(
} else {
'href',
// find the tag value from the chosen dropdown
`http://acervo.bn.br/sophia_web/autoridade/detalhe/${
tagFromDropdown = elem.closest(".instanceMain")
elem.value.match(/\d{9}/)}`
// PageForms 4.8
);
} else {
// .find(".select2-chosen").text();
// Page forms 4.9.1
// find the tag value from the chosen dropdown
tagFromDropdown = elem.closest('.instanceMain')
.querySelector(".select2-selection__rendered")
.getAttribute("title");
// PageForms 4.8
// .find(".select2-chosen").text();
tagFromDropdown = tagFromDropdown.substring(0, 3);
// find the tag value from checked radio button
// Page forms 4.9.1
.querySelector('.select2-selection__rendered')
.getAttribute('title');
tagFromDropdown = tagFromDropdown.substring(0, 3);
// find the tag value from checked radio button


let tagFromRadio = elem.closest(".instanceMain")
const tagFromRadio = elem.closest('.instanceMain')
.querySelector("input[origname='Field[authorityType]']:checked")
.querySelector('input[origname=\'Field[authorityType]\']:checked')
.value;
.value;


const authorityTypes = {
const authorityTypes = {
"Pessoa": "100",
'Pessoa': '100',
"Entidade coletiva": "110",
'Entidade coletiva': '110',
"Evento": "111",
'Evento': '111',
"Título uniforme": "130",
'Título uniforme': '130',
"Tópico": "150",
'Tópico': '150',
"Local": "151"
'Local': '151'
};
};


if (tagFromRadio in authorityTypes) {
if (tagFromRadio in authorityTypes) {
tag = authorityTypes[tagFromRadio];
tag = authorityTypes[tagFromRadio];
}
}


// declare the indicators default values
// declare the indicators default values
let ind1 = elem.closest(".instanceMain")
let ind1 = elem.closest('.instanceMain')
.querySelector("select[origname='Field[ind1]'] option:checked")
.querySelector('select[origname=\'Field[ind1]\'] option:checked')
.value;
.value;
let ind2 = elem.closest(".instanceMain")
let ind2 = elem.closest('.instanceMain')
.querySelector("select[origname='Field[ind2]'] option:checked")
.querySelector('select[origname=\'Field[ind2]\'] option:checked')
.value;
.value;


// declare the 008 field default values
// declare the 008 field default values
let directOrIndirectGeogSubdiv = "", // 06
let directOrIndirectGeogSubdiv = ''; // 06
kindOfRecord = "", // 09
let kindOfRecord = ''; // 09
descriptiveCatalogingRules = "c", // 10
let descriptiveCatalogingRules = 'c'; // 10
subjectHeadingSystem = "n", // 11
let subjectHeadingSystem = 'n'; // 11
typeOfSeries = "n", // 12
let typeOfSeries = 'n'; // 12
numberedOrUnnumberedSeries = "n", // 13
let numberedOrUnnumberedSeries = 'n'; // 13
headingUseMainOrAddedEntry = "a", // 14
let headingUseMainOrAddedEntry = 'a'; // 14
headingUseSubjectAddedEntry = "a", // 15
let headingUseSubjectAddedEntry = 'a'; // 15
headingUseSeriesAddedEntry = "b", // 16
let headingUseSeriesAddedEntry = 'b'; // 16
typeOfSubjectSubdivision = "n", // 17
let typeOfSubjectSubdivision = 'n'; // 17
undifferentiatedPersonalName = "n", // 32
let undifferentiatedPersonalName = 'n'; // 32
levelOfEstablishment = "a"; // 33
let levelOfEstablishment = 'a'; // 33


// set the 008 field and indicadors specific values
// set the 008 field and indicadors specific values
if (tag === "100") {
if (tag === '100') {
undifferentiatedPersonalName = "a";
undifferentiatedPersonalName = 'a';
ind2 = "";
ind2 = '';
}
}
if (tag === "110") {
if (tag === '110') {
// in case of autocomplete of 260
// in case of autocomplete of 260
if (tagFromDropdown === "260") {
if (tagFromDropdown === '260') {
directOrIndirectGeogSubdiv = "n";
directOrIndirectGeogSubdiv = 'n';
kindOfRecord = "b";
kindOfRecord = 'b';
headingUseMainOrAddedEntry = "b";
headingUseMainOrAddedEntry = 'b';
headingUseSubjectAddedEntry = "b";
headingUseSubjectAddedEntry = 'b';
levelOfEstablishment = "n";
levelOfEstablishment = 'n';
ind1 = "1";
ind1 = '1';
}
}
ind2 = "";
ind2 = '';
}
}
if (tag === "111") {
if (tag === '111') {
ind1 = "2";
ind1 = '2';
ind2 = "";
ind2 = '';
}
}
if (tag === "130") {
if (tag === '130') {
typeOfSeries = "a";
typeOfSeries = 'a';
numberedOrUnnumberedSeries = "b";
numberedOrUnnumberedSeries = 'b';
headingUseSeriesAddedEntry = "a";
headingUseSeriesAddedEntry = 'a';
if (tagFromDropdown === "130" ||
if (tagFromDropdown === '130'
tagFromDropdown === "630" ||
|| tagFromDropdown === '630'
tagFromDropdown === "730") {
|| tagFromDropdown === '730') {
ind2 = ind1; // an inverted value happens here, see and
ind2 = ind1; // an inverted value happens here, see and
ind1 = ""; // compare bib. and auth. 130 MARC definitions
ind1 = ''; // compare bib. and auth. 130 MARC definitions
}
}
ind1 = "";
ind1 = '';
}
}
if (tag === "150" || tag === "151") {
if (tag === '150' || tag === '151') {
descriptiveCatalogingRules = "n";
descriptiveCatalogingRules = 'n';
subjectHeadingSystem = "z";
subjectHeadingSystem = 'z';
headingUseMainOrAddedEntry = "b";
headingUseMainOrAddedEntry = 'b';
ind1 = ""; // 150 and 151 indicators must be empty in
ind1 = ''; // 150 and 151 indicators must be empty in
ind2 = ""; // authority MARC records
ind2 = ''; // authority MARC records
if (tag === "151") {
if (tag === '151') {
typeOfSubjectSubdivision = "d";
typeOfSubjectSubdivision = 'd';
}
}
}
}
// build the URL
// build the URL
const autRecordParams = {
const autRecordParams = {
"AutRecord": {
AutRecord: {
"directOrIndirectGeogSubdiv": directOrIndirectGeogSubdiv,
directOrIndirectGeogSubdiv,
"kindOfRecord": kindOfRecord,
kindOfRecord,
"descriptiveCatalogingRules": descriptiveCatalogingRules,
descriptiveCatalogingRules,
"subjectHeadingSystem": subjectHeadingSystem,
subjectHeadingSystem,
"typeOfSeries": typeOfSeries,
typeOfSeries,
"numberedOrUnnumberedSeries": numberedOrUnnumberedSeries,
numberedOrUnnumberedSeries,
"headingUseMainOrAddedEntry": headingUseMainOrAddedEntry,
headingUseMainOrAddedEntry,
"headingUseSubjectAddedEntry": headingUseSubjectAddedEntry,
headingUseSubjectAddedEntry,
"headingUseSeriesAddedEntry": headingUseSeriesAddedEntry,
headingUseSeriesAddedEntry,
"typeOfSubjectSubdivision": typeOfSubjectSubdivision,
typeOfSubjectSubdivision,
"undifferentiatedPersonalName": undifferentiatedPersonalName,
undifferentiatedPersonalName,
"levelOfEstablishment": levelOfEstablishment
levelOfEstablishment
},
},
"Field": {
Field: {
"1": {
'1': {
"tag": "040",
tag: '040',
"data": "$a BR-FlWIK $b por $c BR-FlWIK $d BR-FlUSC"
data: '$a BR-FlWIK $b por $c BR-FlWIK $d BR-FlUSC'
},
},
"2": {
'2': {
"tag": tag,
tag,
"ind1": ind1,
ind1,
"ind2": ind2,
ind2,
"data": elem.value
data: elem.value
},
},
"3": {
'3': {
"tag": "670",
tag: '670',
"data": "$a "
data: '$a '
}
}
}
}
};
};


let url = new URL(location.origin + "/wiki/Special:FormEdit/AutRecord");
const url = new URL(`${window.location.origin}/wiki/Special:FormEdit/AutRecord`);
// function to transform a 3rd level object to URL query strings
// function to transform a 3rd level object to URL query strings
const makeUrlParams = (obj) => {
const makeUrlParams = (obj) => {
for (let [key, value] of Object.entries(obj)) {
for (const [key, value] of Object.entries(obj)) {
if (typeof value === "object") {
if (typeof value === 'object') {
for (let [key2, value2] of Object.entries(value)) {
for (const [key2, value2] of Object.entries(value)) {
if (typeof value2 === "object") {
if (typeof value2 === 'object') {
for (let [key3, value3] of Object.entries(value2)) {
for (const [key3, value3] of Object.entries(value2)) {
url.searchParams.set(`${key}[${key2}][${key3}]`, value3);
url.searchParams.set(`${key}[${key2}][${key3}]`, value3);
}
}
} else {
} else {
url.searchParams.set(`${key}[${key2}]`, value2);
url.searchParams.set(`${key}[${key2}]`, value2);
}
}
}
} else {
url.searchParams.set(key, value);
}

}
};
makeUrlParams(autRecordParams);
authorityLink.innerText = "Criar autoridade";
authorityLink.setAttribute("href", url.pathname + url.search);
}
}
authorityLink.style.display = "";
} else {
});
url.searchParams.set(key, value);
});
}
}
}
};
makeUrlParams(autRecordParams);
authorityLink.innerText = 'Criar autoridade';
authorityLink.setAttribute('href', url.pathname + url.search);
}
authorityLink.style.display = '';
});
});
}


function rmCarriageReturn() {
function rmCarriageReturn() {
const textareas = document.body.querySelectorAll("textarea");
const textareas = document.body.querySelectorAll('textarea');
textareas.forEach(elem => {
textareas.forEach((textarea) => {
elem.value = elem.value.replace("
", "");
textarea.value = textarea.value.replace('
', '');
});
});
}
}


function rmHtmlTags() {
function rmHtmlTags() {
const textareas = document.body.querySelectorAll("textarea");
const textareas = document.body.querySelectorAll('textarea');
textareas.forEach(elem => {
textareas.forEach((textarea) => {
elem.value = elem.value.replace(/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g, "");
textarea.value = textarea.value.replace(/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g, '');
});
});
}
}


function rmTxtFromHiddenTexareas() {
function rmTxtFromHiddenTexareas() {
const textareas = document.body.querySelectorAll("span.hiddenByPF");
const textareas = document.body.querySelectorAll('span.hiddenByPF');
textareas.forEach(elem => {
textareas.forEach((elem) => {
elem.querySelectorAll("textarea").forEach(elem => {
elem.querySelectorAll('textarea').forEach((textarea) => {
elem.value = "";
textarea.value = '';
});
});
});
});
}
}


function normalizeInput() {
function normalizeInput() {
const textareas = document.body.querySelectorAll("textarea");
const textareas = document.body.querySelectorAll('textarea');
textareas.forEach(elem => {
textareas.forEach((textarea) => {
let normalizedInput = elem.value;
let normalizedInput = textarea.value;
normalizedInput = elem.value
normalizedInput = normalizedInput
// "$a Bar \n$b Baz" > " $a Bar $b Baz"
// "$a Bar \n$b Baz" > " $a Bar $b Baz"
.replace(/\s*(\$[a-z0-8])\s*/g, " $1 ")
.replace(/\s*(\$[a-z0-8])\s*/g, ' $1 ')
// "$a Bar\nFoo" > "$a Bar Foo" ou "$a Bar Foo" > "$a Bar Foo"
// "$a Bar\nFoo" > "$a Bar Foo" ou "$a Bar Foo" > "$a Bar Foo"
.replace(/\n|\s\s+|\t/g, " ")
.replace(/\n|\s\s+|\t/g, ' ')
// replace "|" by "%7C" (Ex.: http://viaf.org/processed/WKP|Q2484404)
// replace "|" by "%7C" (Ex.: http://viaf.org/processed/WKP|Q2484404)
.replace("|", "%7C");
.replace('|', '%7C');
elem.value = normalizedInput;
textarea.value = normalizedInput;
});
});
}
}


// calls main functions
// calls main functions
createMarcFieldHelpLink();
createMarcFieldHelpLink();
createAuthorityLinks();
createAuthorityLinks();
rmCarriageReturn();
rmCarriageReturn();
rmHtmlTags();
rmHtmlTags();


// Como instâncias do formulário são criadas pelo botão "Adicionar campo",
// Como instâncias do formulário são criadas pelo botão "Adicionar campo",
// é necessário observar o DOM a partir da classe ".multipleTemplateList".
// é necessário observar o DOM a partir da classe ".multipleTemplateList".
// Toda vez que o DOM é alterado (mais especificamente, quando uma nova
// Toda vez que o DOM é alterado (mais especificamente, quando uma nova
// child de ".multipleTemplateList" é criada), as funções
// child de ".multipleTemplateList" é criada), as funções
// createMarcFieldHelpLink() e createAuthorityLinks() são novamente chamadas.
// createMarcFieldHelpLink() e createAuthorityLinks() são novamente chamadas.
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver


// create an observer instance
// create an observer instance
let observer = new MutationObserver(function (mutations) {
const observer = new MutationObserver((mutations) => {
mutations.forEach(mutation => {
mutations.forEach((mutation) => {
let newNodes = mutation.addedNodes; // DOM NodeList
const newNodes = mutation.addedNodes; // DOM NodeList
if (newNodes.length !== 0) { // if there are new nodes added
if (newNodes.length !== 0) { // if there are new nodes added
createMarcFieldHelpLink();
createMarcFieldHelpLink();
createAuthorityLinks();
createAuthorityLinks();
}
}
});
});
});
});


// select the target node
// select the target node
const target = document.querySelector(".multipleTemplateList");
const target = document.querySelector('.multipleTemplateList');


// configuration of the observer
// configuration of the observer
let config = {
const config = {
childList: true,
childList: true,
attributes: false,
attributes: false,
subtree: false
subtree: false,
};
};


// pass in the target node, as well as the observer options
// pass in the target node, as well as the observer options
observer.observe(target, config);
observer.observe(target, config);


// when clicking save...
// when clicking save...
const saveBtn = document.body.querySelector("#wpSave");
const saveBtn = document.body.querySelector('#wpSave');
saveBtn.addEventListener("click", () => {
saveBtn.addEventListener('click', () => {
observer.disconnect();
observer.disconnect();
rmTxtFromHiddenTexareas();
rmTxtFromHiddenTexareas();
rmCarriageReturn();
rmCarriageReturn();
rmHtmlTags();
rmHtmlTags();
normalizeInput();
normalizeInput();
});
});
}

if (document.readyState === 'complete' || (document.readyState !== 'loading')) {
main();
} else {
document.addEventListener('DOMContentLoaded', main);
}
}

Edição das 13h45min de 16 de agosto de 2022

// Author: @vitorsilverio
// Author: @jaideraf
function main() {
  let tag = '';
  let tagFromDropdown = '';

  function createMarcFieldHelpLink() {
    const tagInputs = document.body.querySelectorAll('.tagInput');
    tagInputs.forEach((elem) => {
      const marcFieldHelpLink = document.createElement('a');
      marcFieldHelpLink.setAttribute('target', '_blank');
      marcFieldHelpLink.innerText = '(?)';

      const marcFieldHelpLinkClass = elem.closest('table')
        .querySelector('.marcFieldHelpLink');
      marcFieldHelpLinkClass.appendChild(marcFieldHelpLink);

      // find the tag value from the chosen dropdown
      marcFieldHelpLinkClass.addEventListener('mouseover', (event) => {
        tagFromDropdown = event.target.closest('.instanceMain')
          .querySelector('.select2-selection__rendered')
          .getAttribute('title');
        tagFromDropdown = tagFromDropdown.substring(0, 3);
        marcFieldHelpLink.setAttribute(
          'title',
          `Documentação do campo ${tagFromDropdown}`
        );
        // build the URL
        if (window.location.href.match(/A\d{6}/)
                    || window.location.href.match(/AutRecord/)) {
          marcFieldHelpLink.setAttribute(
            'href',
            `http://marc.febab.org/a${tagFromDropdown}`
          );
        } else {
          marcFieldHelpLink.setAttribute(
            'href',
            `https://www.loc.gov/marc/bibliographic/bd${tagFromDropdown}.html`
          );
        }
      });
    });
  }

  function createAuthorityLinks() {
    const textareas = document.body.querySelectorAll('textarea.has-authority-control');
    textareas.forEach((elem) => {
      const authorityLink = document.createElement('a');
      authorityLink.setAttribute('title', 'Abre uma nova aba para criar ou editar um registro de autoridade');
      authorityLink.setAttribute('target', '_blank');
      authorityLink.innerText = 'Criar autoridade';
      authorityLink.style.display = 'none';

      const createAuthorityLink = elem.closest('table')
        .querySelector('.createAuthorityLink');

      createAuthorityLink.appendChild(authorityLink);

      const pattern1 = /\$0\sA\d{6}/;
      const pattern2 = /\$0\s\(BN\)\d{9}/;

      elem.addEventListener('focusout', () => {
        // if already recorded
        if (pattern1.test(elem.value)) {
          // build the URL for Wikincat aut record edit
          authorityLink.innerText = 'Editar autoridade';
          authorityLink.setAttribute(
            'href',
            `/wiki/Special:FormEdit/AutRecord/Autoridade:${
              elem.value.match(/A\d{6}/)}`
          );
        } else if (pattern2.test(elem.value)) {
          // build the URL for BN aut record view
          authorityLink.innerText = 'Ver autoridade na BN';
          authorityLink.setAttribute(
            'href',
            `http://acervo.bn.br/sophia_web/autoridade/detalhe/${
              elem.value.match(/\d{9}/)}`
          );
        } else {
          // find the tag value from the chosen dropdown
          tagFromDropdown = elem.closest('.instanceMain')
          // PageForms 4.8
          // .find(".select2-chosen").text();
          // Page forms 4.9.1
            .querySelector('.select2-selection__rendered')
            .getAttribute('title');
          tagFromDropdown = tagFromDropdown.substring(0, 3);
          // find the tag value from checked radio button

          const tagFromRadio = elem.closest('.instanceMain')
            .querySelector('input[origname=\'Field[authorityType]\']:checked')
            .value;

          const authorityTypes = {
            'Pessoa': '100',
            'Entidade coletiva': '110',
            'Evento': '111',
            'Título uniforme': '130',
            'Tópico': '150',
            'Local': '151'
          };

          if (tagFromRadio in authorityTypes) {
            tag = authorityTypes[tagFromRadio];
          }

          // declare the indicators default values
          let ind1 = elem.closest('.instanceMain')
            .querySelector('select[origname=\'Field[ind1]\'] option:checked')
            .value;
          let ind2 = elem.closest('.instanceMain')
            .querySelector('select[origname=\'Field[ind2]\'] option:checked')
            .value;

          // declare the 008 field default values
          let directOrIndirectGeogSubdiv = ''; // 06
          let kindOfRecord = ''; // 09
          let descriptiveCatalogingRules = 'c'; // 10
          let subjectHeadingSystem = 'n'; // 11
          let typeOfSeries = 'n'; // 12
          let numberedOrUnnumberedSeries = 'n'; // 13
          let headingUseMainOrAddedEntry = 'a'; // 14
          let headingUseSubjectAddedEntry = 'a'; // 15
          let headingUseSeriesAddedEntry = 'b'; // 16
          let typeOfSubjectSubdivision = 'n'; // 17
          let undifferentiatedPersonalName = 'n'; // 32
          let levelOfEstablishment = 'a'; // 33

          // set the 008 field and indicadors specific values
          if (tag === '100') {
            undifferentiatedPersonalName = 'a';
            ind2 = '';
          }
          if (tag === '110') {
            // in case of autocomplete of 260
            if (tagFromDropdown === '260') {
              directOrIndirectGeogSubdiv = 'n';
              kindOfRecord = 'b';
              headingUseMainOrAddedEntry = 'b';
              headingUseSubjectAddedEntry = 'b';
              levelOfEstablishment = 'n';
              ind1 = '1';
            }
            ind2 = '';
          }
          if (tag === '111') {
            ind1 = '2';
            ind2 = '';
          }
          if (tag === '130') {
            typeOfSeries = 'a';
            numberedOrUnnumberedSeries = 'b';
            headingUseSeriesAddedEntry = 'a';
            if (tagFromDropdown === '130'
                            || tagFromDropdown === '630'
                            || tagFromDropdown === '730') {
              ind2 = ind1; // an inverted value happens here, see and
              ind1 = ''; // compare bib. and auth. 130 MARC definitions
            }
            ind1 = '';
          }
          if (tag === '150' || tag === '151') {
            descriptiveCatalogingRules = 'n';
            subjectHeadingSystem = 'z';
            headingUseMainOrAddedEntry = 'b';
            ind1 = ''; // 150 and 151 indicators must be empty in
            ind2 = ''; // authority MARC records
            if (tag === '151') {
              typeOfSubjectSubdivision = 'd';
            }
          }
          // build the URL
          const autRecordParams = {
            AutRecord: {
              directOrIndirectGeogSubdiv,
              kindOfRecord,
              descriptiveCatalogingRules,
              subjectHeadingSystem,
              typeOfSeries,
              numberedOrUnnumberedSeries,
              headingUseMainOrAddedEntry,
              headingUseSubjectAddedEntry,
              headingUseSeriesAddedEntry,
              typeOfSubjectSubdivision,
              undifferentiatedPersonalName,
              levelOfEstablishment
            },
            Field: {
              '1': {
                tag: '040',
                data: '$a BR-FlWIK $b por $c BR-FlWIK $d BR-FlUSC'
              },
              '2': {
                tag,
                ind1,
                ind2,
                data: elem.value
              },
              '3': {
                tag: '670',
                data: '$a '
              }
            }
          };

          const url = new URL(`${window.location.origin}/wiki/Special:FormEdit/AutRecord`);
          // function to transform a 3rd level object to URL query strings
          const makeUrlParams = (obj) => {
            for (const [key, value] of Object.entries(obj)) {
              if (typeof value === 'object') {
                for (const [key2, value2] of Object.entries(value)) {
                  if (typeof value2 === 'object') {
                    for (const [key3, value3] of Object.entries(value2)) {
                      url.searchParams.set(`${key}[${key2}][${key3}]`, value3);
                    }
                  } else {
                    url.searchParams.set(`${key}[${key2}]`, value2);
                  }
                }
              } else {
                url.searchParams.set(key, value);
              }
            }
          };
          makeUrlParams(autRecordParams);
          authorityLink.innerText = 'Criar autoridade';
          authorityLink.setAttribute('href', url.pathname + url.search);
        }
        authorityLink.style.display = '';
      });
    });
  }

  function rmCarriageReturn() {
    const textareas = document.body.querySelectorAll('textarea');
    textareas.forEach((textarea) => {
      textarea.value = textarea.value.replace('&#13;', '');
    });
  }

  function rmHtmlTags() {
    const textareas = document.body.querySelectorAll('textarea');
    textareas.forEach((textarea) => {
      textarea.value = textarea.value.replace(/<(?:"[^"]*"['"]*|'[^']*'['"]*|[^'">])+>/g, '');
    });
  }

  function rmTxtFromHiddenTexareas() {
    const textareas = document.body.querySelectorAll('span.hiddenByPF');
    textareas.forEach((elem) => {
      elem.querySelectorAll('textarea').forEach((textarea) => {
        textarea.value = '';
      });
    });
  }

  function normalizeInput() {
    const textareas = document.body.querySelectorAll('textarea');
    textareas.forEach((textarea) => {
      let normalizedInput = textarea.value;
      normalizedInput = normalizedInput
      // "$a   Bar   \n$b    Baz" > " $a Bar $b Baz"
        .replace(/\s*(\$[a-z0-8])\s*/g, ' $1 ')
      // "$a Bar\nFoo" > "$a Bar Foo" ou "$a Bar  Foo" > "$a Bar Foo"
        .replace(/\n|\s\s+|\t/g, ' ')
      // replace "|" by "%7C" (Ex.: http://viaf.org/processed/WKP|Q2484404)
        .replace('|', '%7C');
      textarea.value = normalizedInput;
    });
  }

  // calls main functions
  createMarcFieldHelpLink();
  createAuthorityLinks();
  rmCarriageReturn();
  rmHtmlTags();

  // Como instâncias do formulário são criadas pelo botão "Adicionar campo",
  // é necessário observar o DOM a partir da classe ".multipleTemplateList".
  // Toda vez que o DOM é alterado (mais especificamente, quando uma nova
  // child de ".multipleTemplateList" é criada), as funções
  // createMarcFieldHelpLink() e createAuthorityLinks() são novamente chamadas.
  // https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver

  // create an observer instance
  const observer = new MutationObserver((mutations) => {
    mutations.forEach((mutation) => {
      const newNodes = mutation.addedNodes; // DOM NodeList
      if (newNodes.length !== 0) { // if there are new nodes added
        createMarcFieldHelpLink();
        createAuthorityLinks();
      }
    });
  });

  // select the target node
  const target = document.querySelector('.multipleTemplateList');

  // configuration of the observer
  const config = {
    childList: true,
    attributes: false,
    subtree: false,
  };

  // pass in the target node, as well as the observer options
  observer.observe(target, config);

  // when clicking save...
  const saveBtn = document.body.querySelector('#wpSave');
  saveBtn.addEventListener('click', () => {
    observer.disconnect();
    rmTxtFromHiddenTexareas();
    rmCarriageReturn();
    rmHtmlTags();
    normalizeInput();
  });
}

if (document.readyState === 'complete' || (document.readyState !== 'loading')) {
  main();
} else {
  document.addEventListener('DOMContentLoaded', main);
}