Greasy Fork is available in English.

[RED/OPS/NWCD] Upload Assistant

Accurate filling of new upload/request and group/request edit forms based on foobar2000's playlist selection or web link, offline and online release integrity check, tracklist format customization, featured artists extraction, classical works formatting, cover art fetching from store, checking for previous upload, form enhancements and more

Ekde 2020/05/12. Vidu La ĝisdata versio.

// ==UserScript==
// @name         [RED/OPS/NWCD] Upload Assistant
// @namespace    https://greasyfork.org/users/321857-anakunda
// @version      1.222
// @description  Accurate filling of new upload/request and group/request edit forms based on foobar2000's playlist selection or web link, offline and online release integrity check, tracklist format customization, featured artists extraction, classical works formatting, cover art fetching from store, checking for previous upload, form enhancements and more
// @author       Anakunda
// @iconURL      https://redacted.ch/favicon.ico
// @match        https://redacted.ch/upload.php*
// @match        https://redacted.ch/torrents.php?action=editgroup&*
// @match        https://redacted.ch/torrents.php?action=edit&*
// @match        https://redacted.ch/requests.php?action=new*
// @match        https://redacted.ch/requests.php?action=edit*
// @match        https://notwhat.cd/upload.php*
// @match        https://notwhat.cd/torrents.php?action=editgroup&*
// @match        https://notwhat.cd/torrents.php?action=edit&*
// @match        https://notwhat.cd/requests.php?action=new*
// @match        https://notwhat.cd/requests.php?action=edit*
// @match        https://orpheus.network/upload.php*
// @match        https://orpheus.network/torrents.php?action=editgroup&*
// @match        https://orpheus.network/torrents.php?action=edit&*
// @match        https://orpheus.network/requests.php?action=new*
// @match        https://orpheus.network/requests.php?action=edit*
// @match        https://bemaniso.ws/upload.php*
// @match        https://bemaniso.ws/torrents.php?action=editgroup&*
// @match        https://bemaniso.ws/torrents.php?action=edit&*
// @match        https://bemaniso.ws/requests.php?action=new*
// @match        https://bemaniso.ws/requests.php?action=edit*
// @connect      file://*
// @connect      *
// @grant        GM_xmlhttpRequest
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @require      https://greasyfork.org/scripts/393837-qobuzlib/code/QobuzLib.js
// @require      https://greasyfork.org/scripts/401725-xhrlib/code/xhrLib.js
// @require      https://greasyfork.org/scripts/394414-ua-resource/code/UA-resource.js
// @require      https://greasyfork.org/scripts/396340-js-sha1/code/js-sha1.js
// @require      https://greasyfork.org/scripts/396360-js-stringdistance/code/js-stringdistance.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js
// //@require      https://connect.soundcloud.com/sdk/sdk-3.3.2.js
// ==/UserScript==

// Additional setup: to work, set the pattern below as built-in foobar2000 copy command or custom Text Tools plugin quick copy command
//   $replace($replace([%album artist%]$char(30)[%album%]$char(30)[$if3(%date%,%ORIGINAL RELEASE DATE%,%year%)]$char(30)[$if3(%releasedate%,%retail date%,%date%,%year%)]$char(30)[$if2(%label%,%publisher%)]$char(30)[$if3(%catalog%,%CATALOGNUMBER%,%CATALOG NUMBER%,%labelno%,%catalog #%,%SKU%)]$char(30)[%country%]$char(30)%__encoding%$char(30)%__codec%$char(30)[%__codec_profile%]$char(30)[%__bitrate%]$char(30)[%__bitspersample%]$char(30)[%__samplerate%]$char(30)[%__channels%]$char(30)[$if3(%media%,%format%,%source%,%MEDIATYPE%,%SOURCEMEDIA%,%discogs_format%)]$char(30)[%genre%[|%style%]]$char(30)[%discnumber%]$char(30)[$if2(%totaldiscs%,%disctotal%)]$char(30)[%discsubtitle%]$char(30)[%track number%]$char(30)[$if2(%totaltracks%,%TRACKTOTAL%)]$char(30)[%title%]$char(30)[%track artist%]$char(30)[$if($strcmp(%performer%,%artist%),,%performer%)]$char(30)[$if3(%composer%,%writer%,%SONGWRITER%,%author%,%LYRICIST%)]$char(30)[%conductor%]$char(30)[%remixer%]$char(30)[$if2(%compiler%,%mixer%)]$char(30)[$if2(%producer%,%producedby%)]$char(30)[%length_seconds_fp%]$char(30)[%length_samples%]$char(30)[%filesize%]$char(30)[%replaygain_album_gain%]$char(30)[%album dynamic range%]$char(30)[%__tool%][ | $if2(%MQAENCODER%,%ENCODER%)][ | %ENCODER_OPTIONS%]$char(30)[$if2(%url%,%www%)]$char(30)[$directory_path(%path%)]$char(30)[$if2(%comment%,%description%)]$char(30)$trim([BARCODE=$trim($replace($if3(%barcode%,%UPC%,%EAN%,%MCN%), ,)) ][DISCID=$trim(%DISCID%) ][ASIN=$trim(%ASIN%) ][ISRC=$trim(%ISRC%) ][ISWC=$trim(%ISWC%) ][DISCOGS_ID=$trim(%discogs_release_id%) ][MBID=$trim(%MUSICBRAINZ_ALBUMID%) ][ACCURATERIPCRC=$trim(%ACCURATERIPCRC%) ][ACCURATERIPDISCID=$trim(%ACCURATERIPDISCID%) ][ACCURATERIPID=$trim(%ACCURATERIPID%) ][SOURCEID=$trim($replace(%SOURCEID%, ,_)) ][CT_TOC=$trim(%CDTOC%) ][ITUNES_TOC=$trim(%ITUNES_CDDB_1%) ][RELEASETYPE=$replace($if2(%RELEASETYPE%,%RELEASE TYPE%), ,_) ][COMPILATION=$trim(%compilation%) ][EXPLICIT=$trim(%EXPLICIT%) ]SCENE=$if($and(%ENCODER%,%LANGUAGE%,%MEDIA%,%PUBLISHER%,%RELEASE TYPE%,%RETAIL DATE%,%RIP DATE%,%RIPPING TOOL%),1,0) [ORIGINALFORMAT=$trim($replace(%ORIGINALFORMAT%, ,_)) ][BPM=$trim(%BPM%) ]),$char(13),$char(29)),$char(10),$char(28))
//
// As alternative to pasted playlist, e.g. requests creation, valid URL to page on supported web can be used.
// List of supported domains:
//
// For music releases:
// - qobuz.com
// - highresaudio.com
// - bandcamp.com
// - prestomusic.com
// - discogs.com
// - supraphonline.cz
// - bontonland.cz (closing soon)
// - nativedsd.com
// - junodownload.com
// - hdtracks.com
// - deezer.com
// - spotify.com
// - prostudiomasters.com
// - play.google.com
// - 7digital.com
// - e-onkyo.com
// - acousticsounds.com
// - indies.eu
// - beatport.com
// - traxsource.com
// - musicbrainz.org
// - music.apple.com
// - vgmdb.net
// - tidal.com (requires account)
// - ototoy.jp
// - music.yandex.ru
// - mora.jp
// - allmusic.com
//
// For e-bbook releases:
// - martinus.cz, martinus.sk
// - goodreads.com
// - databazeknih.cz
//
// For application releases:
// - sanet.st

'use strict';

const isFirefox = /\b(?:Firefox)\b/.test(navigator.userAgent) || window.InstallTrigger;

function testDomain(domain) {
  return document.location.hostname.toLowerCase() == domain.toLowerCase();
}
function testPath(path, query) {
  return document.location.pathname.toLowerCase() == '/'.concat(path.toLowerCase(), '.php')
  	&& (!query || document.location.search.toLowerCase().startsWith('?'.concat(query.toLowerCase())));
}

const isRED = testDomain('redacted.ch');
const isNWCD = testDomain('notwhat.cd');
const isOPS = testDomain('orpheus.network');

const isUpload = testPath('upload');
const isEdit = testPath('torrents', 'action=editgroup&');
const isRequestNew = testPath('requests', 'action=new');
const isRequestEdit = testPath('requests', 'action=edit&');
const isAddFormat = isUpload && /\bgroupid=(\d+)\b/i.test(document.location.search);

const urlParser = /^\s*(https?:\/\/\S+)\s*$/i;
const dcRlsParser = /^https?:\/\/(?:\w+\.)*discogs\.com\/releases?\/(\d+)(?=$|\/|\?)/i;
const itunesRlsParser = /^https?:\/\/(?:\w+\.)*apple\.com\/.*\/(\d+)(?=$|\?)/i;
const mbrRlsParser = /^https?:\/\/(?:beta\.)?musicbrainz\.org\/(?:\w+\/)*release\/([\w\-]+)/i;
const dzrRlsParser = /^https?:\/\/(?:\w+\.)*deezer\.com\/(\w+\/)*album\/(\d+)$/i;
const imageExtensions = ['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp', 'tif', 'tiff', 'heic'];
const ptpimgOrigin = 'https://ptpimg.me';
const mbrRlsPrefix = 'https://musicbrainz.org/release/';
const discogsOrigin = 'https://www.discogs.com';
const deezerAlbumPrefix = 'https://www.deezer.com/album/';
const descriptionFields = ['album_desc', 'body', 'description', 'release_desc', 'release_lineage'];
const siteApiTimeframeStorageKey = document.location.hostname.concat(' API time frame');

const spotify_clientid = '6d358a207c634b1ebac640149a6090da';
const spotify_clientsecret = '4c59880a4ec241ed9c89a24e66468c64';
const discogs_token = 'CISOUfiQctZCkUedWJzPhzTXxRYihifZgflZAfEm';
const lastfm_api_key = 'b9f26370d7266fbb3151b2ad4f7a74c9';

const ulTimeFactor = 192;
const rehostTimeout = 30000;
const gazelleApiFrame = 10500;
const ctxt = document.createElement('canvas').getContext('2d');

var prefs = {
  autfill_delay: 500, // delay in ms to autofill form after pasting text into box, 0 to disable
  clean_on_apply: false, // clean the input box on successfull fill
  cleanup_descriptions: true, // pre-submit cleanup to all description fields (remove empty placeholders, redundant info and garbage like empty tag pairs etc.)
  keep_meaningles_composers: false, // keep composers from file tags also for non-composer emphasing genres
  default_medium: '', // preset this media type if it can't be deduced from metadata (Gazelle-compatible names as they appear in dropdown, empty string to not use)
  single_threshold: 10 * 60, // For autodetection of release type: max length of single in s
  EP_threshold: 30 * 60, // For autodetection of release type: max time of EP in s
  auto_rehost_cover: true, // PTPIMG / using 3rd party script
  auto_preview_cover: true,
  huge_image_warning: 5, // threshold in MB for making bandwith stressing cover size warning // 0 to disable
  cover_lookup_provider: 'all', // itunes | lastfm | deezer | musicbrainz | qobuz | google | all | empty for no lookup
  fetch_tags_from_artist: 0, // add N most used tags from release artist (if one) - experimental/may inject nonsense tags for coinciding artists; 0 for disable
  estimate_decade_tag: true, // deduce decade tag (1980s, etc.) from album year for regular albums
  check_whitespace: true, // check tags for leading/trailing spaces and unreadable characters
  honour_rg: false, // do a reminder on missing RG info; off by default
  honour_dr: false, // do a reminder on missing DR info (only for Hi-Res tracks); off by default
  honour_url: false, // do a reminder on missing source URL (tag URL)
  ops_always_edition: true, // (only new uploads) don't use original release but always specific edition (unify with other trackers)
  sacd_decoder: 'foobar2000\'s SACD decoder (DSD2PCM direct / 64fp / 30kHz lowpass)',
  use_store_logos: true, // use online source's pictograsm instead of url in textual form (if defined)
  insert_release_date: true, // ..to rls description
  selfrelease_label: 'self-released',
  upcoming_tags: '', // add this tag(s) to upcoming releases (requests); empty to disable
  remap_texttools_newlines: false, // convert underscores to linebreaks (ambiguous)
  messages_verbosity: 0,
  diag_mode: false,
  // online parsers specific
  apple_offer_alt_cover: true, // usually smaller version of preloaded cover
  use_kana: false, // include Kana(JP) version in artist/title names; applies to mora.jp online parser
  // online service credentials
  redacted_api_key: '',
  ptpimg_api_key: '',
  discogs_key: '', // Applicxation/Consumer Key
  discogs_secret: '', // Application/Consumer Secret
  //soundcloud_clientid: '',
  tidal_userid: '',
  tidal_userpassword: '',
  malzo_uid: '',
  malzo_password: '',
  imgbb_uid: '',
  imgbb_password: '',
  catbox_userhash: '',
  imagevenue_uid: '',
  imagevenue_password: '',
  imgbox_uid: '',
  imgbox_password: '',
  // request specific
  request_default_bounty: 0, // set this bounty in MB after successfull fill of request form / 0 for disable
  always_request_perfect_flac: false,
  include_tracklist_in_request: false, // false: include one line summary only; true: include full tracklisting
  // tracklist specific
  tracklist_style: 1, // 1: classic with components colouring, 2: propertional font right-justified, 3: classic center aligned
  singles_conventional_format: false, // force one track singles to be formatted same way as albums with numbered tracklist
  colorless_tracklist: false, // Strip all colours from tracklist
  sort_tracklist: true,
  reformat_trackartist: true, // (if track artist differs from main artist) rebuild track artist from partial track artists, turn off if generating wrong track artists
  max_tracklist_width: 80, // right margin of the right aligned tracklist. should not exceed the group description width on any device
  tracklist_size: 2, // PHPBB font size
  title_separator: '. ', // divisor of track# and title
  pad_leader: ' ',
  bpm_summary: true,
  tracklist_head_color: '#778899', // #4682B4 / #a7bdd0
  // classical tracklist only components colouring
  tracklist_disctitle_color: '#2bb7b7', // #bb831c
  tracklist_work_color: '#98984d', // #b16890
  tracklist_tracknumber_color: '#8899AA',
  tracklist_artist_color: '#8a6995',
  tracklist_composer_color: '#8ca014',
  tracklist_duration_color: '#33a6cc', // #2196f3
  // online check paramaters
  check_integrity_online: true, // If provided URL tag, compare local release with release online and lookup for discrepancies
  strict_online_check: false, // set to true for strict online check (metadata comparison is case sensitive)
  duration_divergency: 0.75, // maximum tolerated playlists difference in %
  vinyl_duration_divergency: 2.5, // maximum tolerated playlists difference in % for vinyl releases

  save: function() {
	for (var key in this) {
	  if (typeof this[key] != 'function' && this[key] != undefined) GM_setValue(key, this[key]);
	}
  },
};
Object.keys(prefs).forEach(key => { prefs[key] = GM_getValue(key, prefs[key]) });

document.head.appendChild(document.createElement('style')).innerHTML = `
.ua-messages {
  text-indent: -2em;
  margin-left: 2em;
  font: 8pt Verdana, Tahoma, sans-serif;
}
.ua-messages-bg { padding: 15px; text-align: left; background-color: darkslategray; }

.ua-critical { color: red; font-weight: bold; font-size: 10pt; }
.ua-warning { color: #ff8d00; font-weight: 500; font-size: 9pt; }
.ua-notice { color: #e3d67b; }
.ua-info { color: white; }

.ua-button { vertical-align: middle; background-color: transparent; }
.ua-button2 { /*color: beige; */width: 13em; font: 300 x-small "Segoe UI", Calibri, sans-serif; }
.ua-input {
  font: 600 x-small "Segoe UI", Calibri, sans-serif;
  color: slategray; background-color: antiquewhite;
  width: 620px; height: 40px;
  margin-top: 8px; margin-bottom: 8px;
}
.ua-input:focus { color: black; }

#cover-preview {
  width: 100%;
  /*box-shadow: 3px 3px 3px;*/
}
#cover-size {
  width: 100%;
  color: white; background-color: #0a4a75;
  font: 8.5pt Verdana, Tahoma, sans-serif;
  text-align: center;
  /*padding-top: 5px;*/
}

::placeholder {
  font: bold 12pt Calibri, "Segoe UI", sans-serif;
  color: #808080;
  /*text-shadow: 0px 0px 3px #b4b4b4;*/
}
`;

var ref, tbl, elem, child, messages = null, autofill = false, dom;
var tfMessages = [], siteArtistsCache = {}, notSiteArtistsCache = [];

if (isUpload) {
  if ((ref = document.querySelector('form#upload_table > div#dynamic_form')) == null) return;
  common1();
  let x = [];
  x.push(document.createElement('tr'));
  x[0].classList.add('ua-button');
  child = document.createElement('input');
  child.id = 'fill-from-text';
  child.value = 'Fill form (overwrite)';
  child.type = 'button';
  child.className = 'ua-button2';
  child.onclick = fillFromText;
  x[0].append(child);
  elem.append(x[0]);
  x.push(document.createElement('tr'));
  x[1].classList.add('ua-button');
  child = document.createElement('input');
  child.id = 'fill-from-text-weak';
  child.value = 'Fill form (keep values)';
  child.type = 'button';
  child.className = 'ua-button2';
  child.onclick = fillFromText;
  x[1].append(child);
  elem.append(x[1]);
  common2();
  ref.parentNode.insertBefore(tbl, ref);
} else if (isEdit) {
  if ((ref = document.querySelector('form.edit_form > div > div > input[type="submit"]')) == null) return;
  ref = ref.parentNode;
  ref.parentNode.insertBefore(document.createElement('br'), ref);
  common1();
  child = document.createElement('input');
  child.id = 'append-from-text';
  child.value = 'Fill from text (append)';
  child.type = 'button';
  child.className = 'ua-button2';
  child.style.height = '52px';
  child.onclick = fillFromText;
  elem.append(child);
  common2();
  tbl.style.marginBottom = '10px';
  ref.parentNode.insertBefore(tbl, ref);
} else if (isRequestNew) {
  if ((ref = document.getElementById('categories')) == null) return;
  ref = ref.parentNode.parentNode.nextElementSibling;
  ref.parentNode.insertBefore(document.createElement('br'), ref);
  common1();
  child = document.createElement('input');
  child.id = 'fill-from-text-weak';
  child.value = 'Fill from URL';
  child.type = 'button';
  child.className = 'ua-button2';
  child.style.height = '52px';
  child.onclick = fillFromText;
  elem.append(child);
  common2();
  child = document.createElement('td');
  child.colSpan = 2;
  child.append(tbl);
  elem = document.createElement('tr');
  elem.append(child);
  ref.parentNode.insertBefore(elem, ref);
} else if (isRequestEdit) {
  if ((ref = document.querySelector('input#button[type="submit"]')) == null) return;
  ref = ref.parentNode.parentNode;
  ref.parentNode.insertBefore(document.createElement('br'), ref);
  common1();
  child = document.createElement('input');
  child.id = 'append-from-text';
  child.value = 'Fill from text (append)';
  child.type = 'button';
  child.className = 'ua-button2';
  child.style.height = '52px';
  child.onclick = fillFromText;
  elem.append(child);
  common2();
  tbl.style.marginBottom = '10px';
  elem = document.createElement('tr');
  child = document.createElement('td');
  child.colSpan = 2;
  child.append(tbl);
  elem.append(child);
  ref.parentNode.insertBefore(elem, ref);
}

function common1() {
  tbl = document.createElement('tr');
  tbl.style.backgroundColor = 'darkgoldenrod';
  tbl.style.verticalAlign = 'middle';
  elem = document.createElement('td');
  elem.style.textAlign = 'center';
  child = document.createElement('textarea');
  child.id = 'UA-data';
  child.name = 'UA-data';
  child.className = 'ua-input';
  child.spellcheck = false;
  child.placeholder = 'Paste/drop album from foobar2000 or release URL here';
  child.onpaste = uaInsert;
  if (!isNWCD) {
	child.ondrop = uaInsert;
	child.ondragover = clear0;
	if (isFirefox) child.oninput = fixFirefoxDropBug;
  } else child.ondrop = child.ondragstart = child.ondragover = function(evt) {
	evt.preventDefault();
	evt.stopPropagation();
	return false;
  };
  var desc = document.getElementById('body');
  if (desc != null && urlParser.test(desc.value)) {
	child.value = RegExp.$1;
	desc.value = '';
	if (prefs.autfill_delay > 0) {
	  autofill = true;
	  setTimeout(fillFromText, prefs.autfill_delay);
	};
  }
  elem.append(child);
  tbl.append(elem);
  elem = document.createElement('td');
  elem.style.textAlign = 'center';
}
function common2() {
  tbl.append(elem);
  var tb = document.createElement('tbody');
  tb.append(tbl);
  tbl = document.createElement('table');
  tbl.id = 'upload assistant';
  tbl.append(tb);
}

if ((ref = document.getElementById('categories')) != null) {
  ref.addEventListener('change', function(e) {
	elem = document.getElementById('upload assistant');
	if (elem != null) elem.style.visibility = this.value < 4
		|| ['Music', 'Applications', 'E-Books', 'Audiobooks'].includes(this.value) ? 'visible' : 'collapse';
	setTimeout(setHandlers, 2000);
  });
}

if ((ref = document.getElementById('upload-table') || document.querySelector('form.edit_form')
   || document.getElementById('upload_table') || document.getElementById('request_form')) != null) {
  ref.ondragover = voidDragHandler1;
  ref.ondrop = voidDragHandler1;
}
setHandlers();
if ((ref = isUpload ? document.getElementById('file') : null) != null) {
  ref.oninput = function(evt) { if (evt.target.files.length > 0) validataTorrentFile(evt.target.files[0]) };
  if (ref.files.length > 0) validataTorrentFile(ref.files[0]);
}
if (!isRED && (ref = document.querySelector('table#dnulist')) != null) {
  function toggleVisibility() {
	var show = ref.style.display.toLowerCase() == 'none';
	ref.style.display = show ? 'block' : 'none';
	ref.previousElementSibling.style.display = show ? 'block' : 'none';
  }
  toggleVisibility();
  if ((ref = document.querySelector('h3#dnu_header')) != null) {
	elem = ref.parentNode;
	child = document.createElement('a');
	child.href = '#';
	child.onclick = function(evt) {
	  if ((ref = document.querySelector('table#dnulist')) != null) toggleVisibility();
	};
	child.append(ref);
	elem.prepend(child);
  }
}

if (isRequestNew) {
  let title = document.querySelector('input[name="title"]');
  if (title != null) for (i = 1; i < 6; ++i) setTimeout(function(e) { title.readOnly = false }, i * 1000);
}

Array.prototype.includesCaseless = function(str) {
  if (typeof str != 'string') return false;
  str = str.toLowerCase();
  return this.find(elem => typeof elem == 'string' && elem.toLowerCase() == str) != undefined;
};
Array.prototype.pushUnique = function(...items) {
  items.forEach(it => { if (!this.includes(it)) this.push(it) });
  return this.length;
};
Array.prototype.pushUniqueCaseless = function(...items) {
  items.forEach(it => { if (!this.includesCaseless(it)) this.push(it) });
  return this.length;
};
// Array.prototype.getUnique = function(prop) {
//   return this.every((it) => it[prop] && it[prop] == this[0][prop]) ? this[0][prop] : null;
// };
Array.prototype.equalTo = function(arr) {
  return Array.isArray(arr) && arr.length == this.length
  	&& Array.from(arr).sort().toString() == Array.from(this).sort().toString();
};
Array.prototype.equalCaselessTo = function(arr) {
  function adjust(elem) { return typeof elem == 'string' ? elem.toLowerCase() : elem }
  return Array.isArray(arr) && arr.length == this.length
  	&& arr.map(adjust).sort().toString() == this.map(adjust).sort().toString();
};
Array.prototype.homogeneous = function() {
  return this.every(elem => elem === this[0]);
}

String.prototype.toASCII = function() {
  return this.normalize("NFKD").replace(/[\x00-\x1F\u0080-\uFFFF]/g, '');
};
String.prototype.trueLength = function() {
  return Array.from(this).length;
  //return this.normalize('NFKD').replace(/[\u0300-\u036f]/g, '').length;
//   var index = 0, width = 0, len = 0;
//   while (index < this.length) {
// 	var point = this.codePointAt(index);
// 	width = 0;
// 	while (point) {
// 	  ++width;
// 	  point = point >> 8;
// 	}
// 	index += Math.round(width / 2);
// 	++len;
//   }
//   return len;
};
String.prototype.flatten = function() {
  return this.replace(/\n/g, '\x1C').replace(/\r/g, '\x1D');
};
String.prototype.expand = function() {
  return this.replace(/\x1D/g, '\r').replace(/\x1C/g, '\n');
};
String.prototype.titleCase = function() {
  return this.toLowerCase().split(' ').map(x => x[0].toUpperCase() + x.slice(1)).join(' ');
};
String.prototype.collapseGaps = function() {
  return this.replace(/(?:[ \t\xA0]*\r?\n){3,}/g, '\n\n').replace(/\[(\w+)\]\[\/\1\]/ig,'').trim();
};
Date.prototype.getDateValue = function() {
  return Math.floor((this.getTime() / 1000 / 60 - this.getTimezoneOffset()) / 60 / 24);
};
Date.prototype.isExactDate = function() {
  return this.getUTCMilliseconds() > 0 || this.getUTCSeconds() > 0 || this.getUTCMinutes() > 0 || this.getUTCHours() > 0
	|| this.getUTCDate() > 1 || this.getUTCMonth() > 0;
};
File.prototype.getText = function(encoding) {
  return new Promise(function(resolve, reject) {
	var reader = new FileReader();
	reader.onload = function() { resolve(reader.result) };
	reader.onerror = reader.ontimeout = error => { reject('FileReader error (' + this.name + ')') };
	reader.readAsText(this, encoding);
  }.bind(this));
};
class HTML extends String { };

const excludedCountries = [
  /\b(?:United\s+States|USA?)\b/,
  /\b(?:United\s+Kingdom|(?:Great\s+)?Britain|England|GB|UK)\b/,
  /\b(?:Europe|European\s+Union|EU)\b/,
  /\b(?:Unknown)\b/,
];

class TagManager extends Array {
  constructor(...tags) {
	super();
	this.presubstitutions = [
	  [/\b(?:Singer\/Songwriter)\b/i, 'singer.songwriter'],
	  [/\b(?:Pop\/Rock)\b/i, 'pop.rock'],
	  [/\b(?:Folk\/Rock)\b/i, 'folk.rock'],
	  [/\s*,\s*(?:&\s*|and\s+)/i, ' & '],
	];
	this.substitutions = [
	  [/^Pop\s*(?:[\-\−\—\–]\s*)?Rock$/i, 'pop.rock'],
	  [/^Rock\s*(?:[\-\−\—\–]\s*)?Pop$/i, 'pop.rock'],
	  [/^Rock\s+n\s+Roll$/i, 'rock.and.roll'],
	  ['AOR', 'album.oriented.rock'],
	  [/^(?:Prog)\.?\s*(?:Rock)$/i, 'progressive.rock'],
	  [/^Synth[\s\-\−\—\–]+Pop$/i, 'synthpop'],
	  [/^World(?:\s+and\s+|\s*[&+]\s*)Country$/i, 'world.music', 'country'],
	  ['World', 'world.music'],
	  [/^(?:Singer(?:\s+and\s+|\s*[&+]\s*))?Songwriter$/i, 'singer.songwriter'],
	  [/^(?:R\s*(?:[\'\’\`][Nn](?:\s+|[\'\’\`]\s*)|&\s*)B|RnB)$/i, 'rhytm.and.blues'],
	  [/\b(?:Soundtracks?)$/i, 'score'],
	  ['Electro', 'electronic'],
	  ['Metal', 'heavy.metal'],
	  ['NonFiction', 'non.fiction'],
	  ['Rap', 'hip.hop'],
	  ['NeoSoul', 'neo.soul'],
	  ['NuJazz', 'nu.jazz'],
	  [/^J[\s\-]Pop$/i, 'jpop'],
	  [/^K[\s\-]Pop$/i, 'jpop'],
	  [/^J[\s\-]Rock$/i, 'jrock'],
	  ['Hardcore', 'hardcore.punk'],
	  ['Garage', 'garage.rock'],
	  [/^(?:Neo[\s\-\−\—\–]+Classical)$/i, 'neoclassical'],
	  [/^(?:Bluesy[\s\-\−\—\–]+Rock)$/i, 'blues.rock'],
	  [/^(?:Be[\s\-\−\—\–]+Bop)$/i, 'bebop'],
	  [/^(?:Chill)[\s\-\−\—\–]+(?:Out)$/i, 'chillout'],
	  [/^(?:Atmospheric)[\s\-\−\—\–]+(?:Black)$/i, 'atmospheric.black.metal'],
	  ['GoaTrance', 'goa.trance'],
	  [/^Female\s+Vocal\w*$/i, 'female.vocalist'],
	  ['Contemporary R&B', 'contemporary.rhytm.and.blues'],
	  // Country aliases
	  ['Canada', 'canadian'],
	  ['Australia', 'australian'],
	  ['New Zealand', 'new.zealander'],
	  ['Japan', 'japanese'],
	  ['Taiwan', 'thai'],
	  ['China', 'chinese'],
	  ['Singapore', 'singaporean'],
	  [/^(?:Russia|Russian\s+Federation|Россия|USSR|СССР)$/i, 'russian'],
	  ['Turkey', 'turkish'],
	  ['Israel', 'israeli'],
	  ['France', 'french'],
	  ['Germany', 'german'],
	  ['Spain', 'spanish'],
	  ['Italy', 'italian'],
	  ['Sweden', 'swedish'],
	  ['Norway', 'norwegian'],
	  ['Finland', 'finnish'],
	  ['Greece', 'greek'],
	  [/^(?:Netherlands|Holland)$/i, 'dutch'],
	  ['Belgium', 'belgian'],
	  ['Luxembourg', 'luxembourgish'],
	  ['Denmark', 'danish'],
	  ['Switzerland', 'swiss'],
	  ['Austria', 'austrian'],
	  ['Portugal', 'portugese'],
	  ['Ireland', 'irish'],
	  ['Scotland', 'scotish'],
	  ['Iceland', 'icelandic'],
	  [/^(?:Czech\s+Republic|Czechia)$/i, 'czech'],
	  [/^(?:Slovak\s+Republic|Slovakia)$/i, 'slovak'],
	  ['Hungary', 'hungarian'],
	  ['Poland', 'polish'],
	  ['Estonia', 'estonian'],
	  ['Latvia', 'latvian'],
	  ['Lithuania', 'lithuanian'],
	  ['Moldova', 'moldovan'],
	  ['Armenia', 'armenian'],
	  ['Ukraine', 'ukrainian'],
	  ['Yugoslavia', 'yugoslav'],
	  ['Serbia', 'serbian'],
	  ['Slovenia', 'slovenian'],
	  ['Croatia', 'croatian'],
	  ['Macedonia', 'macedonian'],
	  ['Montenegro', 'montenegrin'],
	  ['Romania', 'romanian'],
	  ['Malta', 'maltese'],
	  ['Brazil', 'brazilian'],
	  ['Mexico', 'mexican'],
	  ['Argentina', 'argentinean'],
	  ['Jamaica', 'jamaican'],
	  // Books
	  ['Beletrie', 'fiction'],
	  ['Satira', 'satire'],
	  ['Komiks', 'comics'],
	  ['Komix', 'comics'],
	  // Removals
	  ['Unknown'],
	  ['Other'],
	  ['New'],
	  ['Ostatni'],
	  ['Knihy'],
	  ['Audioknihy'],
	  ['dsbm'],
	  [/^(?:Audio\s*kniha|Audio\s*Book)$/i],
	].concat(excludedCountries.map(it => [it]));
	this.splits = [
	  ['Alternative', 'Indie'],
	  ['Rock', 'Pop'],
	  ['Soul', 'Funk'],
	  ['Ska', 'Rocksteady'],
	  ['Jazz Fusion', 'Jazz Rock'],
	  ['Rock', 'Pop'],
	  ['Jazz', 'Funk'],
	];
	this.additions = [
	  [/^(?:(?:(?:Be|Post|Neo)[\s\-\−\—\–]*)?Bop|Modal|Fusion|Free[\s\-\−\—\–]+Improvisation|Modern\s+Creative|Jazz[\s\-\−\—\–]+Fusion|Big[\s\-\−\—\–]*Band)$/i, 'jazz'],
	  [/^(?:(?:Free|Cool|Avant[\s\-\−\—\–]*Garde|Contemporary|Vocal|Instrumental|Crossover|Modal|Mainstream|Modern|Soul|Smooth|Piano|Latin|Afro[\s\-\−\—\–]*Cuban)[\s\-\−\—\–]+Jazz)$/i, 'jazz'],
	  [/^(?:Opera)$/i, 'classical'],
	  [/\b(?:Chamber[\s\-\−\—\–]+Music)\b/i, 'classical'],
	  [/\b(?:Orchestral[\s\-\−\—\–]+Music)\b/i, 'classical'],
	  [/^(?:Symphony)$/i, 'classical'],
	  [/^(?:Sacred\s+Vocal)\b/i, 'classical'],
	  [/\b(?:Soundtracks?|Films?|Games?|Video|Series?|Theatre|Musical)\b/i, 'score'],
	];
	if (tags.length > 0) this.add(...tags);
  }

  add(...tags) {
	var added = 0;
	for (var tag of tags) {
	  if (typeof tag != 'string') continue;
	  qobuzTranslations.forEach(function(it) { if (tag.toLowerCase() == it[0].toLowerCase()) tag = it[1] });
	  this.presubstitutions.forEach(k => { if (k[0].test(tag)) tag = tag.replace(k[0], k[1]) });
	  tag.split(/\s*[\,\/\;\>\|]+\s*/).forEach(function(tag) {
		//qobuzTranslations.forEach(function(it) { if (tag == it[0]) tag = it[1] });
		tag = tag.toASCII().replace(/\(.*?\)|\[.*?\]|\{.*?\}/g, '').trim();
		if (tag.length <= 0 || tag == '?') return null;
		function test(obj) {
		  return typeof obj == 'string' && tag.toLowerCase() == obj.toLowerCase()
		  	|| obj instanceof RegExp && obj.test(tag);
		}
		for (var k of this.substitutions) {
		  if (test(k[0])) {
			if (k.length >= 1) added += this.add(...k.slice(1));
				else addMessage('invalid tag \'' + tag + '\' found', 'warning');
			return;
		  }
		}
		for (k of this.additions) {
		  if (test(k[0])) added += this.add(...k.slice(1));
		}
		for (k of this.splits) {
		  if (new RegExp('^' + k[0] + '(?:\\s+and\\s+|\\s*[&+]\\s*)' + k[1] + '$', 'i').test(tag)) {
			added += this.add(k[0], k[1]); return;
		  }
		  if (new RegExp('^' + k[1] + '(?:\\s+and\\s+|\\s*[&+]\\s*)' + k[0] + '$', 'i').test(tag)) {
			added += this.add(k[0], k[1]); return;
		  }
		}
		tag = tag.
		  replace(/^(?:Alt\.)\s*(\w+)$/i, 'Alternative $1').
		  replace(/\b(?:Alt\.)(?=\s+)/i, 'Alternative').
		  replace(/^[3-9]0s$/i, '19$0').
		  replace(/^[0-2]0s$/i, '20$0').
		  replace(/\b(Psy)[\s\-\−\—\–]+(Trance|Core|Chill)\b/i, '$1$2').
		  replace(/\s*(?:[\'\’\`][Nn](?:\s+|[\'\’\`]\s*)|[\&\+]\s*)/, ' and ').
		  replace(/[\s\-\−\—\–\_\.\,\'\`\~]+/g, '.').
		  replace(/[^\w\.]+/g, '').
		  toLowerCase();
		if (tag.length >= 2 && !this.includes(tag)) {
		  this.push(tag);
		  ++added;
		}
	  }.bind(this));
	}
	return added;
  }
  toString() { return Array.from(this).sort().join(', ') }
};

return;

function fillFromText(evt) {
  if (evt == undefined && !autofill) return;
  autofill = false;
  var overwrite = this.id == 'fill-from-text';
  var clipBoard = document.getElementById('UA-data');
  if (clipBoard == null) return false;
  messages = document.getElementById('UA-messages');
  //let promise = clientInformation.clipboard.readText().then(text => clipBoard = text);
  //if (typeof clipBoard != 'string') return false;
  var i, matches, sourceUrl, category = document.getElementById('categories'), xhr = new XMLHttpRequest();
  if (category == null && document.getElementById('releasetype') != null
	  || category != null && (category.value == 0 || category.value == 'Music')) return fillFromText_Music();
  if (category != null && (category.value == 1 || category.value == 'Applications')) return fillFromText_Apps();
  if (category != null && (category.value == 2 || category.value == 3
	|| category.value == 'E-Books' || category.value == 'Audiobooks')) return fillFromText_Ebooks();
  return category == null ? fillFromText_Apps(true).catch(reason => fillFromText_Ebooks()) : Promise.reject('no category');

  function fillFromText_Music() {
	if (messages != null) messages.parentNode.removeChild(messages);
	const divs = ['—', '⸺', '⸻'];
	const vaParser = /^(?:Various(?:\s+Artists)?|VA|\<various\s+artists\>|Různí(?:\s+interpreti)?)$/i;
	const VA = 'Various Artists';
	const multiArtistParsers = [
	  /\s*[,;\u3001](?!\s*(?:[JjSs]r)\b)(?:\s*[Aa]nd\s+)?\s*/,
	  /\s+[\/\|\×]\s+/,
	];
	const pseudoArtistParsers = [
	  /^(?:#??N[\/\-]?A|[JS]r\.?)$/i,
	  /^(?:traditional|lidová)$/i,
	  /\b(?:traditional|lidová)$/,
	  /^(?:tradiční|lidová)\s+/,
	  /^(?:[Aa]nonym)/,
	  /^(?:[Ll]iturgical\b|[Ll]iturgick[áý])/,
	  /^(?:auditorium|[Oo]becenstvo|[Pp]ublikum)$/,
	  /^(?:Various\s+Composers)$/i,
	  /^(?:Guests|Friends)$/i,
	];
	const ampersandParsers = [
	  /\s+(?:meets|vs\.?|X)\s+(?!\s*(?:[\&\/\+\,\;]|and))/i,
	  /\s*[;\/\|\×]\s*(?!\s*(?:\s*[\&\/\+\,\;]|and))/i,
	  /\s+(?:[\&\+]|and)\s+(?!his\b|her\b|Friends$|Strings$)/i, // /\s+(?:[\&\+]|and)\s+(?!(?:The|his|her|Friends)\b)/i,
	  /\s*\+\s*(?!(?:his\b|her\b|Friends$|Strings$))/i,
	];
	const featParsers = [
	  /\s+(?:meets)\s+(.*?)\s*$/i,
	  /\s+(?:[Ww]ith)\s+(?!his\b|her\b|Friends$|Strings$)(.*?)\s*$/,
	  /(?:\s+[\-\−\—\–\_])?\s+(?:[Ff](?:eaturing|t\.))\s+(.*?)\s*$/,
	  /(?:\s+[\-\−\—\–\_])?\s+(?:[Ff](?:ea)?t\.)\s+(.*?)\s*$/, // [0]
	  /\s+\[\s*f(?:eat(?:\.|uring)|t\.)\s+([^\[\]]+?)\s*\]/i, // [1]
	  /\s+\(\s*f(?:eat(?:\.|uring)|t\.)\s+([^\(\)]+?)\s*\)/i, // [2]
	  /\s+\[\s*(?:(?:en\s+)?duo\s+)?avec\s+([^\[\]]+?)\s*\]/i, // [3]
	  /\s+\(\s*(?:(?:en\s+)?duo\s+)?avec\s+([^\(\)]+?)\s*\)/i, // [4]
	  /\s+\[\s*with\s+(?![Hh]is\b|[Hh]er\b|Friends$|Strings$)([^\[\]]+?)\s*\]/, // [5]
	  /\s+\(\s*with\s+(?![Hh]is\b|[Hh]er\b|Friends$|Strings$)([^\(\)]+?)\s*\)/, // [6]
	];
	const remixParsers = [
	  /\s+\((?:The\s+)Remix(?:e[sd])?\)/i,
	  /\s+\[(?:The\s+)Remix(?:e[sd])?\]/i,
	  /\s+(?:The\s+)Remix(?:e[sd])?\s*$/i,
	  /^(Remixes)\b/,
	  /\s+\(([^\(\)]+?)(?:[\'\’\`]s)?\s+(?:(?:Extended|Enhanced)\s+)?Remix\)/i,
	  /\s+\[([^\[\]]+?)(?:[\'\’\`]s)?\s+(?:(?:Extended|Enhanced)\s+)?Remix\]/i,
	  /\s+\(\s*(?:(Extended|Enhanced)\s+)?Remix(?:ed)?\s+by\s+([^\(\)]+)\)/i,
	  /\s+\[\s*(?:(Extended|Enhanced)\s+)?Remix(?:ed)?\s+by\s+([^\[\]]+)\]/i,
	];
	const otherArtistsParsers = [
	  [/^(.*?)\s+(?:under|(?:conducted)\s+by)\s+(.*)$/, 4],
	  [/^()(.*?)\s+\(conductor\)$/i, 4],
	  //[/^()(.*?)\s+\(.*\)$/i, 1],
	];
	const artistStrips = [
	  /\s+(?:aka|AKA)\.?\s+(.*)$/,
	  /\s+\(([^\(\)]+)\)$/,
	  /\s+\[([^\[\]]+)\]$/,
	  /\s+\{([^\{\}]+)\}$/,
	];
	var isVA, onlineSource = urlParser.test(clipBoard.value) && RegExp.$1, ajaxRejects = 0;
	return (onlineSource ? urlResolver(onlineSource).then(fetchOnline_Music) :
	  Promise.resolve(clipBoard.value.split(/(?:\r?\n)+/).filter(line => line.trim().length > 0).map(function(line, ndx) {
		const fields = [
		  /* 00 */ 'artist', 'album', 'album_year', 'release_date', 'label', 'catalog', 'country', 'encoding',
		  /* 08 */ 'codec', 'codec_profile', 'bitrate', 'bd', 'sr', 'channels', 'media', 'genre', 'discnumber',
		  /* 17 */ 'totaldiscs', 'discsubtitle', 'tracknumber', 'totaltracks', 'title', 'track_artist', 'performer',
		  /* 24 */ 'composer', 'conductor', 'remixer', 'compiler', 'producer', 'duration', 'samples', 'filesize',
		  /* 32 */ 'ag', 'dr', 'vendor', 'url', 'dirpath', 'description', 'identifiers',
		];
		var metaData = line.expand().split('\x1E'), track = { identifiers: {} }, identifiers = [];
		if (metaData.length < fields.length) {
		  console.error('invalid data format for track #' + (ndx + 1) + ': length:', metaData.length,
			'(' + fields.length + '); metaData:', metaData, '; line:', line);
		  throw 'invalid data format for track #' + (ndx + 1) + ' (see console log for details)';
		} else if (metaData.length > fields.length) {
		  console.warn('unexpected data format for track #' + (ndx + 1) + ': length:', metaData.length,
			'(' + fields.length + '); metaData:', metaData, '; line:', line);
		}
	  	fields.forEach(function(propName) {
		  if (propName == 'identifiers') {
			metaData.shift().trim().split(/\s+/).forEach(function(id) {
			  if (/^([\w\-]+)[=:](\S*)$/.test(id)) track.identifiers[RegExp.$1.toUpperCase()] = RegExp.$2.replace(/\x1B/g, ' ');
			});
		  } else {
			track[propName] = metaData.shift();
			if (track[propName] === '') track[propName] = undefined;
		  }
		});
	  	if (prefs.check_whitespace) Object.keys(track).forEach(function(propName) {
		  if (typeof track[propName] != 'string') return;
		  if (propName != 'description' && (track[propName].includes('\r') || track[propName].includes('\n'))) {
			track[propName] = track[propName].replace(/[\r\n]+/g, '');
			addMessage('track #' + (ndx + 1) + ' contains linebreaks in tag <' + propName + '>', 'warning');
		  }
		  if ((i = propName == 'description' ? /[\x00-\x08\x0B\x0C\x0E-\x19]+/g : /[\x00-\x19]+/g).test(track[propName])) {
			track[propName] = track[propName].replace(i, '');
			addMessage('track #' + (ndx + 1) + ' contains control codes in tag <' + propName + '>', 'warning');
		  }
		  if (/^[\s\xA0]+$/.test(track[propName])) {
			track[propName] = undefined;
			addMessage('track #' + (ndx + 1) + ' in tag <' + propName + '> contains only whitespace', 'warning');
		  } else if (/^[\s\xA0]+|[\s\xA0]+$/.test(track[propName])) {
			track[propName] = track[propName].trim();
			addMessage('track #' + (ndx + 1) + ' in tag <' + propName + '> contains leading/trailing whitespace', 'warning');
		  }
		  if (/[ \xA0]{2,}/.test(track[propName])) {
			track[propName] = track[propName].replace(/[ \xA0]{2,}/g, ' ')
			addMessage('track #' + (ndx + 1) + ' in tag <' + propName + '> contains multiple spaces', 'warning');
		  }
		});
		if (track.description == '.') track.description = undefined; else if (track.description) {
		  if (prefs.remap_texttools_newlines)
			track.description = track.description.replace(/__/g, '\r\n').replace(/_/g, '\n') // ambiguous
		  track.description = track.description.collapseGaps();
		}
		['bitrate', 'bd', 'sr', 'channels', 'totaldiscs', 'totaltracks', 'samples', 'filesize', 'dr'].forEach(function(propName) {
		  if (track[propName] !== undefined) track[propName] = parseInt(track[propName]);
		});
		['duration'].forEach(function(propName) {
		  if (track[propName] !== undefined) track[propName] = parseFloat(track[propName]);
		});
		if (track.album_year) track.album_year = extractYear(track.album_year) || NaN;
		['ag', 'tg', 'ap', 'tp'].forEach(function(propName) {
		  track[propName] = /^([\+\-]?\d+(?:\.\d+)?)\s*dB\b/i.test(track[propName]) ? parseFloat(RegExp.$1) : undefined;
		});
		return track;
	  }))
	).then(parseTracks).catch(e => { if (e) addMessage(e, 'critical') });

	function parseTracks(tracks) {
	  if (tracks.length <= 0) {
		clipBoard.value = '';
		throw 'no tracks found';
	  }
	  if (prefs.diag_mode) console.debug('Parsing tracks:', tracks);
	  var albumBitrate = 0, totalTime = 0, albumSize = 0, media, release = { totaldiscs: 1, srs: [] };
	  var allowedFormats = Array.from(document.querySelectorAll('select#format > option'))
	  	.filter(option => option.value.length > 0).map(option => option.value);
	  if (allowedFormats.length <= 0) allowedFormats = ["MP3", "FLAC", "AAC", "AC3", "DTS"];
	  tracks.forEach(function(track) {
		processTrackArtists(track);
		if (!track.artist) {
		  clipBoard.value = '';
		  throw new HTML('main artist must be defined for every track' + ruleLink('2.3.16.4'));
		}
		if (!track.album) {
		  clipBoard.value = '';
		  throw new HTML('album title must be defined for every track' + ruleLink('2.3.16.4'));
		}
		if (!track.tracknumber) {
		  clipBoard.value = '';
		  throw new HTML('all track numbers must be defined' + ruleLink('2.3.16.4'));
		}
		if (!track.title) {
		  clipBoard.value = '';
		  throw new HTML('all track titles must be defined' + ruleLink('2.3.16.4'));
		}
		if (track.duration != undefined && isUpload && (isNaN(track.duration) || track.duration <= 0)) {
		  clipBoard.value = '';
		  throw 'invalid track #' + track.tracknumber + ' length: ' + track.duration;
		}
		if (!onlineSource && track.codec && !allowedFormats.includes(track.codec)) {
		  clipBoard.value = '';
		  throw 'disallowed codec present (' + track.codec + ')';
		}
		if (!onlineSource && /\b(?:MQAEncode) v(\d+(?:\.\d+)*)\b/.test(track.vendor)) {
		  clipBoard.value = '';
		  throw 'MQA encoded release (' + RegExp.lastMatch + ')';
		}
		if (/^(?:#?N\/A|No\s+Label|Not\s+On\s+Label|\[no\s+label\])$/i.test(track.label)) track.label = undefined;
		if (/^(?:#?N\/A|none)$/i.test(track.catalog)) track.catalog = undefined;
		if (/^(\d+)\s*[\/]\s*(\d+)$/.test(track.tracknumber)) { // track/totaltracks
		  addMessage('nonstandard track number formatting for track ' + RegExp.$1 + ': ' + track.tracknumber, 'warning');
		  track.tracknumber = RegExp.$1;
		  if (!track.totaltracks) track.totaltracks = parseInt(RegExp.$2);
		} else if (/^(\d+)[\.\-](\d+)$/.test(track.tracknumber)) { // discnumber.tracknumber
		  addMessage('nonstandard track number formatting for track ' + RegExp.$2 + ': ' + track.tracknumber, 'warning');
		  if (!track.discnumber) track.discnumber = parseInt(RegExp.$1);
		  track.tracknumber = RegExp.$2;
		}
		if (track.discnumber) {
		  if (/^(\d+)\s*\/\s*(\d+)/.test(track.discnumber)) {
			addMessage('nonstandard disc number formatting for track ' + track.tracknumber + ': ' + track.discnumber, 'warning');
			track.discnumber = RegExp.$1;
			if (!track.totaldiscs) track.totaldiscs = RegExp.$2;
		  } else track.discnumber = parseInt(track.discnumber);
		  if (isNaN(track.discnumber)) {
			addMessage('invalid disc numbering for track ' + track.tracknumber, 'warning');
			track.discnumber = undefined;
		  }
		  if (track.discnumber > release.totaldiscs) release.totaldiscs = track.discnumber;
		}
		totalTime += track.duration;
		albumBitrate += track.bitrate * track.duration;
		albumSize += track.filesize;
		if (!onlineSource && track.bitrate > 0) {
		  let minBitrate = [24, 12];
		  switch (track.codec) {
			case 'FLAC':
			  if (track.sr > 0 && track.bd > 0) minBitrate = [
				Math.round(Math.max(track.sr * track.bd / 1400, 192)),
				Math.round(Math.max(track.sr * track.bd / 2000, 192)),
			  ];
			  break;
			case 'MP3':
			  switch (track.codec_profile) {
				case 'VBR V0': minBitrate = [192, 96]; break;
				case 'VBR V1': minBitrate = [160, 80]; break;
				case 'VBR V2': minBitrate = [128, 64]; break;
			  }
			  break;
			case 'AAC':
			  if (/\b(?:TVBR)\sq(\d+)\b/.test(track.vendor)) minBitrate = [
				Math.round(Math.max(parseInt(RegExp.$1) * 1.9, 192)),
				Math.round(Math.max(parseInt(RegExp.$1) * 1.4, 192)),
			  ]; else if (/\b(?:(?:CV|A|C)BR)\s(\d+)kbps\b/.test(track.vendor)) minBitrate = [
				Math.round(Math.max(parseInt(RegExp.$1) * 0.75, 192)),
				Math.round(Math.max(parseInt(RegExp.$1) * 0.4, 192)),
			  ];
			  break;
		  }
		  if (minBitrate && track.bitrate < minBitrate[0]) addMessage('track #' + track.tracknumber +
			' suspiciously low bitrate (' + track.bitrate + ' kbps)', track.bitrate < minBitrate[1] ? 'warning' : 'notice');
		}
	  });
	  if (!tracks.every(track => track.discnumber > 0) && !tracks.every(track => !track.discnumber)) {
		addMessage('inconsistent release (mix of tracks with and without disc number)', 'warning');
	  }
	  if (!onlineSource && release.totaldiscs > 1 && tracks.some(it => it.totaldiscs != release.totaldiscs))
		addMessage('at least one track not having properly set TOTALDISCS (' + release.totaldiscs + ')', 'info');

	  function setUniqueProperty(propName, propNameLiteral) {
		let homogeneous = new Set(tracks.map(it => it[propName]).filter(it => it != undefined && it != null));
		if (homogeneous.size > 1) {
		  var diverses = '', it = homogeneous.values(), val;
		  while (!(val = it.next()).done) diverses += '<br>\t' + val.value;
		  clipBoard.value = '';
		  throw new HTML('mixed releases not accepted (' + propNameLiteral + ') - supposedly user compilation' + diverses);
		}
		release[propName] = homogeneous.values().next().value;
	  }
	  setUniqueProperty('artist', 'album artist');
	  ['artists', 'featuring_artists', 'composers', 'conductors', 'performers', 'compilers', 'remixers', 'producers'].forEach(function(role) {
		if (tracks.every(track => Array.isArray(track[role]) && track[role].equalTo(tracks[0][role]))) release[role] = tracks[0][role];
	  });
	  setUniqueProperty('album', 'album title');
	  setUniqueProperty('album_year', 'album year');
	  setUniqueProperty('release_date', 'release date');
	  setUniqueProperty('encoding', 'encoding');
	  setUniqueProperty('codec', 'codec');
	  setUniqueProperty('codec_profile', 'codec profile');
	  setUniqueProperty('vendor', 'vendor');
	  setUniqueProperty('media', 'media');
	  setUniqueProperty('channels', 'channels');
	  setUniqueProperty('label', 'label');
	  setUniqueProperty('country', 'country');

	  tracks.forEach(function(iter) {
		setProperty('trackArtists', 'track_artist');
		setProperty('totalTracks', 'totaltracks');
		setProperty('discSubtitles', 'discsubtitle');
		setProperty('composers', 'composer');
		setProperty('catalogs', 'catalog');
		setProperty('bitrates', 'bitrate');
		setProperty('bds', 'bd');
		setProperty('ags', 'ag');
		setProperty('drs', 'dr');
		if (iter.sr) if (typeof release.srs[iter.sr] != 'number') release.srs[iter.sr] = iter.duration;
		  else release.srs[iter.sr] += iter.duration;
		setProperty('dirpaths', 'dirpath');
		setProperty('descriptions', 'description');
		setProperty('genres', 'genre');
		setProperty('urls', 'url');
		setProperty('coverUrls', 'cover_url');

		function setProperty(propName, trackProp) {
		  if (!Array.isArray(release[propName])) release[propName] = [];
		  if (iter[trackProp] !== undefined && iter[trackProp] !== null && (typeof iter[trackProp] != 'string'
				|| iter[trackProp].length > 0) && !release[propName].includes(iter[trackProp])) {
			release[propName].push(iter[trackProp]);
		  }
		}
	  });
	  if (!release.totalTracks) addMessage('total tracks not set', 'warning');
	  if (release.totalTracks.length > 0) {
		if (release.totalTracks.length > 1) {
		  addMessage('total tracks not consistent across release: ' + release.totalTracks, 'warning');
		} else if (release.totalTracks[0] != tracks.length) {
		  addMessage('total tracks not matching tracklist length: ' +
			release.totalTracks[0] + ' != ' + tracks.length, 'warning');
		}
	  }
	  tracks.forEach(function(track1, ndx1) {
		if (tracks.some((track2, ndx2) => ndx2 < ndx1 && track1.tracknumber == track2.tracknumber
			&& track1.discnumber == track2.discnumber && track1.discsubtitle == track2.discsubtitle)) {
		  addMessage('duplicate track ' + (track1.discnumber ? track1.discnumber + '-' : '') +
			(track1.discsubtitle ? track1.discsubtitle + '-' : '') + track1.tracknumber, 'warning');
		}
	  });
	  if (!onlineSource) {
		function validatorFunc(arr, validator, str) {
		  if (arr.length <= 0 || !arr.some(validator)) return true;
		  clipBoard.value = '';
		  throw 'disallowed ' + str + ' present (' + arr.filter(validator) + ')';
		}
		validatorFunc(release.bds, bd => ![16, 24].includes(bd), 'bit depths');
		validatorFunc(Object.keys(release.srs),
		  sr => sr < 44100 || sr > 192000 || sr % 44100 != 0 && sr % 48000 != 0, 'sample rates');
		if (prefs.honour_rg && tracks.some(track => track.ag === undefined))
		  addMessage('at least one track is missing RG info', 'notice');
		if (release.ags.length > 1) addMessage('album RG differs across the release', 'notice');
		if (prefs.honour_dr && tracks.some(track => track.bd > 16 && track.dr === undefined))
		  addMessage('at least one high resolution track is missing DR info', 'notice');
		release.urls.forEach(function(url) {
		  if (/^https?:\/\/(\w+\.)*7digital\.com\/.*\?f=/i.test(url))
			addMessage('session id present in online source URL: ' + url, 'notice');
		});
	  }
	  if (elementWritable(document.getElementById('image') || document.querySelector('input[name="image"]'))) {
		(release.coverUrls.length > 0 ? setCover(release.coverUrls[0]) : Promise.reject('No cover URL'))
			.catch(getCoverOnline).catch(searchCoverOnline);
	  }
	  var albumBPM = Math.round(tracks.reduce(function(acc, track) {
		return acc + parseInt(track.identifiers.BPM) * track.duration;
	  }, 0) / totalTime);
	  var composerEmphasis = tracks.some(track => track.identifiers.COMPOSEREMPHASIS);
	  var isFromDSD = false, isClassical = false;
	  var canSort = tracks.every((tr1, ndx1) => tracks.every((tr2, ndx2) => ndx1 == ndx2
		|| tr1.tracknumber != tr2.tracknumber || tr1.discnumber != tr2.discnumber));
	  var yadg_prefil = '', releaseType, editionTitle, iter, rx;
	  var barCode = getHomoIdentifier('BARCODE');
	  if (barCode) barCode = parseInt(barCode.replace(/\s+/g, ''));
	  if (!Number.isInteger(barCode)) {
		if (release.catalogs.length == 1) barCode = parseInt(release.catalogs[0].replace(/[\s\-]/g, ''));
		if (!Number.isInteger(barCode) || barCode < 10**10) barCode = undefined;
	  }
	  var tags = new TagManager();
	  albumBitrate /= totalTime;
	  const isCompilation = tracks.every(track => track.identifiers.COMPILATION == 1
		|| /^(?:Compilation)$/i.test(track.identifiers.RELEASETYPE));
	  if (tracks.every(track => /^(?:Single)$/i.test(track.identifiers.RELEASETYPE))
		  || tracks.length == 1 && totalTime > 0 && totalTime < prefs.single_threshold) {
		releaseType = getReleaseIndex('Single');
	  } else if (tracks.every(it => it.identifiers.RELEASETYPE == 'EP')) {
		releaseType = getReleaseIndex('EP');
	  } else if (tracks.every(it => /^soundtrack$/i.test(it.identifiers.RELEASETYPE))) {
		releaseType = getReleaseIndex('Soundtrack');
		tags.add('score');
		composerEmphasis = true;
	  }
	  if (release.genres.length > 0) {
		const classicalGenreParsers = [
		  /\b(?:Classical|Classique|Klassik|Symphony|Symphonic(?:al)?|Operas?|Operettas?|Ballets?|(?:Violin|Cello|Piano)\s+Solos?|Chamber|Choral|Choirs?|Orchestral|Etudes?|Duets|Concertos?|Cantatas?|Requiems?|Passions?|Mass(?:es)?|Oratorios?|Poems?|Sacred|Secular|Vocal\s+Music)\b/i,
		];
		release.genres.forEach(function(genre) {
		  classicalGenreParsers.forEach(function(classicalGenreParser) {
			if (classicalGenreParser.test(genre) && !/\b(?:metal|rock|pop)\b/i.test(genre)) {
			  composerEmphasis = true;
			  isClassical = true
			}
		  });
		  if (/\b(?:Jazz|Vocal)\b/i.test(genre) && !/\b(?:Nu|Future|Acid)[\s\-\−\—\–]*Jazz\b/i.test(genre)
			  && !/\bElectr(?:o|ic)[\s\-\−\—\–]?Swing\b/i.test(genre)) {
			composerEmphasis = true;
		  }
		  if (/\b(?:Soundtracks?|Score|Films?|Games?|Video|Series?|Theatre|Musical)\b/i.test(genre)) {
			if (!releaseType) releaseType = getReleaseIndex('Soundtrack');
			composerEmphasis = true;
		  }
		  if (/\b(?:Christmas\s+Music)\b/i.test(genre)) {
			composerEmphasis = true;
		  }
		  tags.add(...genre.split(/\s*\|\s*/));
		});
		if (release.genres.length > 1) addMessage('inconsistent genre accross album: ' + release.genres.join(' / '), 'warning');
	  }
	  if (!onlineSource && isClassical && !tracks.every(track => track.composer)) {
		addMessage(new HTML('all tracks composers must be set for clasical music' + ruleLink('2.3.17')), 'warning');
		//return false;
	  }
	  // Processing artists: recognition, splitting and dividing to categores
	  const roleCollisions = [
		[4, 5], // main
		[0, 4], // guest
		[], // remixer
		[], // composer
		[], // conductor
		[], // DJ/compiler
		[], // producer
	  ];
	  isVA = vaParser.test(release.artist);
	  var artists = [], albumGuests = [];
	  for (i = 0; i < 7; ++i) artists[i] = [];

	  if (!isVA) {
		if (Array.isArray(release.artists) && release.artists.length > 0) {
		  artists[0] = release.artists.filter(exclusions);
		  if (Array.isArray(release.featuring_artists)) albumGuests = release.featuring_artists;
		  yadg_prefil = joinArtists(artists[0]);
		} else {
		  yadg_prefil = spliceGuests(release.artist);
		  addArtists(0, yadg_prefil);
		  artists[0] = artists[0].filter(exclusions);
		  albumGuests = Array.from(artists[1]);
		}
		if (ampersandParsers.some(rx => rx.test(yadg_prefil))) getSiteArtist(yadg_prefil); // priority cache record

		function exclusions(artist) {
		  return !['conductors', 'compilers']
			.some(category => Array.isArray(release[category]) && release[category].includesCaseless(artist));
		}
	  }

	  featParsers.slice(3).forEach(function(rx, ndx) {
		if ((matches = rx.exec(release.album)) == null) return;
		if (ndx >= 5 && !splitArtists(matches[1], multiArtistParsers.concat(ampersandParsers.slice(1)))
			.every((artist, ndx) => looksLikeTrueName(artist, 1))) return;
		addArtists(1, matches[1]);
		artists[0].forEach(guest => { if (albumGuests.includesCaseless(guest)) albumGuests.push(guest) });
		addMessage('featured artist(s) in album title (' + release.album + ')', 'warning');
		release.album = release.album.replace(rx, '');
	  });
	  remixParsers.slice(4).forEach(function(rx) {
		if (rx.test(release.album)) addArtists(2, RegExp.$1.replace(/\b\d{4}\b/g, '').replace(/\s{2,}/g, ' ').trim());
	  })
	  if (((matches = /^(.*?)\s+Presents\s+(.*)$/.exec(release.album)) != null
			|| isVA && (matches = (/\s+\(compiled\s+by\s+(.*?)\)\s*$/i.exec(release.album)
			|| /\s+compiled\s+by\s+(.*?)\s*$/i.exec(release.album))) != null) && looksLikeTrueName(matches[1])) {
		addArtists(5, matches[1]);
		if (!releaseType) releaseType = getReleaseIndex('Compilation');
	  }

	  for (iter of tracks) {
		['track_artist', 'performer', 'track_guest'].forEach(function(role) {
		  var arrayName = role.concat('s');
		  addTrackPerformers(Array.isArray(iter[arrayName]) && iter[arrayName].length > 0 ? iter[arrayName] : iter[role]);
		});
		[
		  [2, 'remixer'],
		  [3, 'composer'],
		  [4, 'conductor'],
		  [5, 'compiler'],
		  [6, 'producer'],
		].forEach(function(role) {
		  var arrayName = role[1].concat('s');
		  addArtists(role[0], Array.isArray(iter[arrayName]) && iter[arrayName].length > 0 ? iter[arrayName] : iter[role[1]]);
		});

		if (iter.title) {
		  featParsers.slice(3).forEach(function(rx, ndx) {
			if ((matches = rx.exec(iter.title)) == null) return;
			var featArtists = splitArtists(matches[1], multiArtistParsers.concat(ampersandParsers.slice(1)));
			if (ndx >= 5 && !featArtists.every((artist, ndx) => looksLikeTrueName(artist, 1))) return;
			if (Array.isArray(iter.track_artists) && iter.track_artists.length > 0) {
			  if (!Array.isArray(iter.track_guests)) iter.track_guests = [];
			  featArtists.forEach(function(featArtist) {
				if (!iter.track_artists.includesCaseless(featArtist) && !iter.track_guests.includesCaseless(featArtist))
					track_guests.push(featArtist);
			  });
			  if (!isVA && iter.track_artists.equalCaselessTo(release.artists)
				  && iter.track_guests.equalCaselessTo(release.featuring_artists)) {
				iter.track_artists = iter.track_guests = iter.track_artist = undefined;
			  } else iter.track_artist = joinArtists(iter.track_artists).concat(' feat. ', joinArtists(iter.track_guests));
			} else {
			  useTA = iter.track_artist && !featArtists.some(featArtist => iter.track_artist.includes(featArtist)
				  || Array.isArray(iter.track_artists) && iter.track_artists.includes(featArtist)
				  || Array.isArray(iter.track_guests) && iter.track_guests.includes(featArtist));
			  iter.track_artist = iter[useTA ? 'track_artist' : 'artist'].concat(' feat. ', matches[1]);
			}
			addArtists(1, matches[1]);
			addMessage('featured artist(s) in track title (#' + iter.tracknumber + ': ' + iter.title + ')', 'warning');
			iter.title = iter.title.replace(rx, '');
		  });
		  if (!iter.remixer) remixParsers.slice(4).forEach(function(rx) {
			if (rx.test(iter.title)) addArtists(2, RegExp.$1.replace(/\b\d{4}\b/g, '').replace(/\s{2,}/g, ' ').trim());
		  });
		}
		if (isClassical && !iter.composer && /^([^\(\)\[\]\{\},:]+?)(?:\s*\((?:\d{4}\s*-|b\.)\s*\d{4}\))/.test(iter.discsubtitle)) {
		  //track.composer = RegExp.$1;
		  addArtists(3, RegExp.$1);
		}
	  }
	  for (i = 0; i < Math.round(tracks.length / 2); ++i) splitAmpersands();
	  albumGuests = splitAmpersands(albumGuests);

	  function addArtists(ndx, _artists) {
		(typeof _artists == 'string' ? splitArtists(_artists) : Array.isArray(_artists) ? _artists : []).forEach(function(artist) {
		  artist = ndx != 0 ? strip(artist) : guessOtherArtists(artist);
		  if (artist.length > 0 && !pseudoArtistParsers.some(rx => rx.test(artist))
			  && !artists[ndx].includesCaseless(artist)
			  && !roleCollisions[ndx].some(n => artists[n].includesCaseless(artist))) artists[ndx].push(artist);
		});
	  }
	  function addTrackPerformers(_artists) {
		(typeof _artists == 'string' ? splitArtists(spliceGuests(_artists, 1)) : Array.isArray(_artists) ? _artists : []).forEach(function(artist) {
		  artist = guessOtherArtists(artist);
		  if (artist.length > 0 && !pseudoArtistParsers.some(rx => rx.test(artist))
			&& !artists[0].includesCaseless(artist)
			&& (isVA || !artists[1].includesCaseless(artist))) artists[isVA ? 0 : 1].push(artist);
		});
	  }
	  function splitAmpersands(_artists) {
		if (_artists) {
		  let result;
		  if (typeof _artists == 'string') result = splitArtists(_artists);
		  	else if (Array.isArray(_artists)) result = Array.from(_artists);
		  		else return [];
		  splitInternal(result);
		  return result;
		}
		for (let ndx = 0; ndx < artists.length; ++ndx) splitInternal(artists[ndx], roleCollisions[ndx]);

		function splitInternal(refArr, roleCollisions) {
		  ampersandParsers.forEach(function(ampersandParser) {
			for (var i = refArr.length; i > 0; --i) {
			  var j = refArr[i - 1].split(ampersandParser).map(strip);
			  if (j.length <= 1 || !j.some(it1 => artists.some(it2 => it2.includesCaseless(it1)))
				  && !j.every(looksLikeTrueName) || getSiteArtist(refArr[i - 1])) continue;
			  refArr.splice(i - 1, 1, ...j.filter(function(artist) {
				return !refArr.includesCaseless(artist) && !pseudoArtistParsers.some(rx => rx.test(artist))
				  && (!Array.isArray(roleCollisions) || !roleCollisions.some(n => artists[n].includesCaseless(artist)));
			  }));
			}
		  });
		}
	  }
	  function spliceGuests(str, level = 1) {
		(level > 0 ? featParsers.slice(level) : featParsers).forEach(function(rx, ndx) {
		  var matches = rx.exec(str);
		  if (matches != null && (level + ndx < 8
				|| splitArtists(matches[1]).every((artist, ndx) => looksLikeTrueName(artist, 1)))) {
			addArtists(1, matches[1]);
			str = str.replace(rx, '');
		  }
		});
		return str;
	  }
	  function guessOtherArtists(name) {
		otherArtistsParsers.forEach(function(it) {
		  if (!it[0].test(name)) return;
		  addArtists(it[1], RegExp.$2);
		  name = RegExp.$1;
		});
		return strip(name);
	  }
	  function getArtists(trackArtist) {
		if (!trackArtist || typeof trackArtist != 'string') trackArtist = '';
		otherArtistsParsers.forEach(it => { if (it[0].test(trackArtist)) trackArtist = RegExp.$1 });
		var result = [ [], [] ];
		featParsers.slice(1).forEach(function(rx, ndx) {
		  if ((matches = rx.exec(trackArtist)) == null || ndx >= 7 && !looksLikeTrueName(matches[1], 1)) return;
		  splitAmpersands(matches[1]).forEach(artist => { result[1].pushUniqueCaseless(artist) });
		  trackArtist = trackArtist.replace(rx, '');
		});
		splitAmpersands(trackArtist).forEach(artist => { result[0].pushUniqueCaseless(artist) });
		return result;
	  }
	  function realTrackArtist(trackArtist) {
		var result, trackArtists = getArtists(trackArtist);
		if (trackArtists[0].length > 0 && (!trackArtists[0].equalCaselessTo(artists[0]) || !trackArtists[1].equalCaselessTo(albumGuests)))
		  if (prefs.reformat_trackartist) {
			result = joinArtists(trackArtists[0]);
			if (trackArtists[1].length > 0) result += ' feat. '.concat(joinArtists(trackArtists[1]));
		  } else result = trackArtist;
		return result;
	  }

	  if (elementWritable(document.getElementById('artist') || document.getElementById('artist_0'))) {
		let artistIndex = 0;
		const enSorter = /^(?:The)\s+/;
		catLoop: for (i = 0; i < artists.length; ++i) for (iter of artists[i]
			.filter(artist => !roleCollisions[i].some(n => artists[n].includesCaseless(artist)))
			.sort((a, b) => a.replace(enSorter, '').localeCompare(b.replace(enSorter, '')))) {
		  if (isUpload) {
			var id = 'artist';
			if (artistIndex > 0) id += '_' + artistIndex;
			while ((ref = document.getElementById(id)) == null) AddArtistField();
		  } else {
			while ((ref = document.querySelectorAll('input[name="artists[]"]')).length <= artistIndex) AddArtistField();
			ref = ref[artistIndex];
		  }
		  if (ref == null) throw new Error('Failed to allocate artist fields');
		  ref.value = iter;
		  ref.nextElementSibling.value = i + 1;
		  if (++artistIndex >= 200) break catLoop;
		}
		if (overwrite && artistIndex > 0) while (document.getElementById('artist_' + artistIndex) != null) {
		  RemoveArtistField();
		}
	  }

	  // Processing album title
	  const mediaParsers = [
		[/\s+(?:\[(?:LP|Vinyl|12"|7")\]|\((?:LP|Vinyl|12"|7")\))$/, 'Vinyl'],
		[/\s+(?:\[SA-?CD\]|\(SA-?CD\))$/, 'SACD'],
		[/\s+(?:\[(?:Blu[\s\-\−\—\–]?Ray|BD|BRD?)\]|\((?:Blu[\s\-\−\—\–]?Ray|BD|BRD?)\))$/, isOPS ? 'BD' : 'Blu-Ray'],
		[/\s+(?:\[DVD(?:-?A)?\]|\(DVD(?:-?A)?\))$/, 'DVD'],
	  ];
	  const releaseTypeParsers = [
		[/\s+(?:-\s+Single|\[Single\]|\(Single\))$/i, 'Single', true, true],
		[/\s+(?:(?:-\s+)?EP|\[EP\]|\(EP\))$/, 'EP', true, true],
		[/\s+\((?:Live|En\s+directo?|(?:Ao|En)\s+Vivo)\b[^\(\)]*\)$/i, 'Live album', false, false],
		[/\s+\[(?:Live|En\s+directo?|(?:Ao|En)\s+Vivo)\b[^\[\]]*\]$/i, 'Live album', false, false],
		[/(?:^Live\s+(?:[aA]t|[Ii]n)\b|^Directo?\s+[Ee]n\b|\bUnplugged\b|\bAcoustic\s+Stage\b|\s+Live$)/, 'Live album', false, false],
		[/\b(?:(?:Best\s+of|Greatest\s+Hits|Complete\s+(.+?\s+)(?:Albums|Recordings))\b|Collection$)|^The(\s+\w+)+Years$/i, 'Anthology', false, false],
	  ];
	  const editionParsers = [
		/\s+\(((?:Remaster(?:ed)?|Remasterizado|Remasterisée|Reissu(?:ed)?|Deluxe|Enhanced|Expanded|Limited|Version|\d+th\s+Anniversary)\b[^\(\)]*|[^\(\)]*\b(?:Edition|Version|Promo|Release|Édition|Reissue))\)$/i,
		/\s+\[((?:Remaster(?:ed)?|Remasterizado|Remasterisée|Reissu(?:ed)?|Deluxe|Enhanced|Expanded|Limited|Version|\d+th\s+Anniversary)\b[^\[\]]*|[^\[\]]*\b(?:Edition|Version|Promo|Release|Édition|Reissue))\]$/i,
		/\s+-\s+([^\[\]\(\)\-\−\—\–]*\b(?:(?:Remaster(?:ed)?|Remasterizado|Remasterisée|Bonus\s+Track)\b[^\[\]\(\)\-\−\—\–]*|Reissue|Edition|Version|Promo|Enhanced|Release|Édition))$/i,
	  ];
	  var album = release.album;
	  releaseTypeParsers.forEach(function(it) {
		if (it[0].test(album)) {
		  if (it[2] || !releaseType) releaseType = getReleaseIndex(it[1]);
		  if (it[3]) album = album.replace(it[0], '');
		}
	  });
	  rx = '\\b(?:Soundtrack|Score|Motion\\s+Picture|Series|Television|Original(?:\\s+\\w+)?\\s+Cast|Music\\s+from|(?:Musique|Bande)\\s+originale)\\b';
	  if (reInParenthesis(rx).test(album) || reInBrackets(rx).test(album)) {
		if (!releaseType) releaseType = getReleaseIndex('Soundtrack');
		tags.add('score');
		composerEmphasis = true;
	  }
	  remixParsers.forEach(function(rx) {
		if (rx.test(album) && !releaseType) releaseType = getReleaseIndex('Remix');
	  });
	  editionParsers.forEach(function(rx) {
		if (rx.test(album) && (!RegExp.$1.toLowerCase().startsWith('remaster') || !release.album_year
		   	|| release.album_year != extractYear(release.release_date)) && !isRequestNew && !isRequestEdit) {
		  album = album.replace(rx, '');
		  editionTitle = RegExp.$1;
		}
	  });
	  mediaParsers.forEach(function(it) {
		if (it[0].test(album)) {
		  album = album.replace(it[0], '');
		  media = it[1];
		}
	  });
	  if (elementWritable(ref = document.getElementById('title') || document.querySelector('input[name="title"]'))) {
		ref.value = album;
	  }

	  if (yadg_prefil) yadg_prefil += ' ';
	  yadg_prefil += album;
	  if (elementWritable(ref = document.getElementById('yadg_input'))) {
		ref.value = yadg_prefil || '';
		if (yadg_prefil && (ref = document.getElementById('yadg_submit')) != null && !ref.disabled) ref.click();
	  }

	  if (!release.album_year) release.album_year = parseInt(getHomoIdentifier('PUBYEAR')) || undefined;
	  if (elementWritable(ref = document.getElementById('year'))) {
		ref.value = release.album_year || '';
	  }
	  i = release.release_date && extractYear(release.release_date);
	  if (elementWritable(ref = document.getElementById('remaster_year'))
		  || !isUpload && i > 0 && (ref = document.querySelector('input[name="year"]')) != null && !ref.disabled) {
		ref.value = i || '';
	  }
	  //if (tracks.every(it => it.identifiers.EXPLICIT == 0)) editionTitle = 'Clean' + (editionTitle ? ' / ' + editionTitle : '');
	  [/\s+\(([^\(\)]+)\)\s*$/, /\s+\[([^\[\]]+)\]\s*$/, /\s+\{([^\{\}]+)\}\s*$/].forEach(function(rx) {
		var version = tracks.map(track => rx.test(track.title) ? RegExp.$1 : null);
		version = version.homogeneous() && version[0] || undefined;
		if (!editionTitle && /\b(?:Remastered|Remasterisée|Remasterizado|Acoustic|Instrumental)\b/i.test(version)
			&& releaseType != getReleaseIndex('Single')) editionTitle = version;
		if (!releaseType && /\b(?:Live)\b/i.test(version)) releaseType = getReleaseIndex('Live album');
	  });
	  if (elementWritable(ref = document.getElementById('remaster_title'))) ref.value = editionTitle || '';
	  if (elementWritable(ref = document.getElementById('remaster_record_label')
			|| document.querySelector('input[name="recordlabel"]'))) {
		ref.value = release.label ? prefs.selfrelease_label && !isVA && release.label == release.artist
		  || /^(?:independent|vlastní\s+náklad|Self[\s\-]Released)$/i.test(release.label)
		  || /^iMD-/.test(release.label) ? prefs.selfrelease_label : release.label.split(/\s*;\s*/g).join(' / ') : '';
	  }
	  if (elementWritable(ref = document.getElementById('remaster_catalogue_number')
			|| document.querySelector('input[name="cataloguenumber"]'))) {
		ref.value = release.catalogs.length >= 1
			&& release.catalogs.map(it => it.replace(/\s*;\s*/g, ' / ')).join(' / ') || barCode || '';
	  }
	  var scene = getHomoIdentifier('SCENE');
	  if (isUpload && scene != undefined && (ref = document.getElementById('scene')) != null && !ref.disabled) try {
		ref.checked = eval(scene.toLowerCase());
	  } catch(e) { console.warn('Invalid SCENE value (' + scene + ')') }
	  var br_isSet = (ref = document.getElementById('bitrate')) != null && ref.value;
	  if (elementWritable(ref = document.getElementById('format')) && allowedFormats.includes(release.codec)) {
		ref.value = release.codec || (isRED ? '' : '---');
		ref.onchange(); //exec(function() { Format() });
	  }
	  if (isRequestNew) {
		if (prefs.always_request_perfect_flac) reqSelectFormats('FLAC');
			else if (release.codec) reqSelectFormats(release.codec);
	  }
	  var sel;
	  if (release.encoding == 'lossless') {
		sel = tracks.some(track => track.bd == 24) ? '24bit Lossless' : 'Lossless';
	  } else if (release.bitrates.length >= 1) {
		let lame_version = release.codec == 'MP3' && /^LAME(\d+)\.(\d+)/i.test(release.vendor) ?
			parseInt(RegExp.$1) * 1000 + parseInt(RegExp.$2) : undefined;
		if (release.codec == 'MP3' && release.codec_profile == 'VBR V0') {
		  sel = lame_version >= 3094 ? 'V0 (VBR)' : 'APX (VBR)'
		} else if (release.codec == 'MP3' && release.codec_profile == 'VBR V1') {
		  sel = 'V1 (VBR)'
		} else if (release.codec == 'MP3' && release.codec_profile == 'VBR V2') {
		  sel = lame_version >= 3094 ? sel = 'V2 (VBR)' : 'APS (VBR)'
		} else if (release.bitrates.length == 1 && [192, 256, 320].includes(Math.round(release.bitrates[0]))) {
		  sel = Math.round(release.bitrates[0]);
		} else {
		  sel = 'Other';
		}
	  }
	  if ((ref = document.getElementById('bitrate')) != null && !ref.disabled && (overwrite || !br_isSet)) {
		ref.value = sel || '';
		ref.onchange(); //exec(function() { Bitrate() });
		if (sel == 'Other' && (ref = document.getElementById('other_bitrate')) != null) {
		  ref.value = Math.round(release.bitrates.length == 1 ? release.bitrates[0] : albumBitrate);
		  if ((ref = document.getElementById('vbr')) != null) ref.checked = release.bitrates.length > 1;
		}
	  }
	  if (isRequestNew) {
		if (prefs.always_request_perfect_flac) {
		  reqSelectBitrates('Lossless', '24bit Lossless');
		} else if (sel) reqSelectBitrates(sel);
	  }
	  if (release.media) {
		sel = undefined;
		[
		  [/\b(?:WEB|File|Download|digital\s+media)\b|^Digital$/i, 'WEB'],
		  [/\bCD\b/, 'CD'],
		  [/\b(?:SA-?CD|[Hh]ybrid)\b/, 'SACD'],
		  [/\b(?:[Bb]lu[\-\−\—\–\s]?[Rr]ay|BRD?|BD)\b/, isOPS ? 'BD' : 'Blu-Ray'],
		  [/\bDVD(?:-?A)?\b/, 'DVD'],
		  [/\b(?:[Vv]inyl\b|LP\b|12"|7")/, 'Vinyl'],
		].forEach(k => { if (k[0].test(release.media)) sel = k[1] });
		media = sel || media;
	  }
	  if (!media) {
		if (tracks.every(isRedBook)) {
		  addMessage('media not determined - CD estimated', 'info');
		  media = 'CD';
		} else if (tracks.some(t => t.bd > 16 || (t.sr > 0 && t.sr != 44100) || t.samples > 0 && t.samples % 588 != 0)) {
		  addMessage('media not determined - NOT CD', 'info');
		}
	  } else if (media != 'CD' && tracks.every(isRedBook)) {
		addMessage('CD as source media is estimated (' + media + ')', 'info');
	  }
	  if (elementWritable(ref = document.getElementById('media'))) {
		ref.value = media || !tracks.some(notRedBook) && prefs.default_medium || (isRED ? '' : '---');
	  }
	  if (isRequestNew) {
		if (prefs.always_request_perfect_flac) reqSelectMedias('WEB', 'CD', isOPS ? 'BD' : 'Blu-Ray', 'DVD', 'SACD')
			else if (media) reqSelectMedias(media);
	  }
	  function isRedBook(track) {
		return track.bd == 16 && track.sr == 44100 && track.channels == 2 && track.samples > 0 && track.samples % 588 == 0;
	  }
	  function notRedBook(track) {
		return track.bd && track.bd != 16 || track.sr && track.sr != 44100
			|| track.channels && track.channels != 2 || track.samples && track.samples % 588 != 0;
	  }
	  if (tracks.every(it => it.identifiers.ORIGINALFORMAT && it.identifiers.ORIGINALFORMAT.includes('DSD'))) {
		isFromDSD = true;
	  }
	  // Release type
	  if (!releaseType/* || isCompilation)*/) {
		if (/\b(?:Mixtape)\b/i.test(release.album)) releaseType = getReleaseIndex('Mixtape');
		else if (isVA) releaseType = getReleaseIndex('Compilation');
		else if (isCompilation) releaseType = getReleaseIndex('Anthology');
	  }
	  if ((!releaseType || releaseType == 5) && totalTime <= prefs.EP_threshold && tracks.every(function(track) {
		const rxs = [/\s+\([^\(\)]+\)\s*$/, /\s+\[[^\[\]]+\]\s*$/];
		return rxs.reduce((acc, rx) => acc.replace(rx, ''), track.title)
		  	== rxs.reduce((acc, rx) => acc.replace(rx, ''), tracks[0].title);
	  })) {
		releaseType = getReleaseIndex('Single');
	  }
	  if (!releaseType) if (totalTime > 0 && totalTime < prefs.single_threshold) {
		releaseType = getReleaseIndex('Single');
	  } else if (totalTime > 0 && totalTime < prefs.EP_threshold) {
		releaseType = getReleaseIndex('EP');
	  }
	  if ((ref = document.getElementById('releasetype')) != null && !ref.disabled
		  && (overwrite || ref.value == 0 || ref.value == '---')) ref.value = releaseType || getReleaseIndex('Album');
	  // Tags
	  if (prefs.estimate_decade_tag && (isNaN(totalTime) || totalTime < 2 * 60 * 60)
		  && release.album_year > 1900 && [1, 3, 5, 9, 13, undefined].includes(releaseType)
		  /*&& !/\b(?:Remaster(?:ed)?|Remasterizado|Remasterisée|Reissue|Anniversary|Collector(?:'?s)?)\b/i.test(editionTitle)*/)
		tags.add(Math.floor(release.album_year/10) * 10 + 's'); // experimental
	  if (release.country) {
		if (!excludedCountries.some(it => it.test(release.country))) tags.add(release.country);
	  }
	  if (!composerEmphasis && tracks.every(track => track.identifiers.HASLYRICS == 0)) tags.add('instrumental');
	  if (elementWritable(ref = document.getElementById('tags'))) {
		ref.value = tags.toString();
		if (artists[0].length == 1 && prefs.fetch_tags_from_artist > 0) setTimeout(function() {
		  var artist = getSiteArtist(artists[0][0]);
		  if (!artist) return;
		  tags.add(...artist.tags.sort((a, b) => b.count - a.count).map(it => it.name)
			.slice(0, prefs.fetch_tags_from_artist));
		  var ref = document.getElementById('tags');
		  ref.value = tags.toString();
		}, 3000);
	  }
	  if (!composerEmphasis/* && release.genres.length > 0*/ && !prefs.keep_meaningles_composers) {
		document.querySelectorAll('input[name="artists[]"]').forEach(function(i) {
		  if (['4', '5'].includes(i.nextElementSibling.value)) i.value = '';
		});
	  }

	  const doubleParsParsers = [
		/\(+(\([^\(\)]*\))\)+/,
		/\[+(\[[^\[\]]*\])\]+/,
		/\{+(\{[^\{\}]*\})\}+/,
	  ];
	  tracks.forEach(function(track) {
		doubleParsParsers.forEach(function(rx) {
		  if (!rx.test(track.title)) return;
		  addMessage('doubled parentheses in track #' + track.tracknumber + ' title ("' + track.title + '")', 'warning');
		  //track.title.replace(rx, RegExp.$1);
		});
	  });
	  if (tracks.length > 1 && tracks.map(track => track.title).homogeneous()) {
		addMessage('all tracks having same title: ' + tracks[0].title, 'warning');
	  }
	  if (isUpload && !isOPS) findPreviousUploads();
	  // Album description
	  sourceUrl = getStoreUrl();
	  if ((ref = document.querySelector('tr#autofill_tr > td > select')) != null) {
		if (i = getHomoIdentifier('DISCOGS_ID')) {
		  ref.value = 'discogs';
		  ref.onchange();
		  if (elementWritable(ref = document.getElementById('discogs'))) ref.value = i;
		} else if (i = getHomoIdentifier('MBID')) {
		  ref.value = 'musicbrainz';
		  ref.onchange();
		  if (elementWritable(ref = document.getElementById('musicbrainz'))) ref.value = i;
		}
	  }
	  const vinylTest = /^((?:Vinyl|LP) rip by\s+)(.*)$/im;
	  const vinyltrackParser = /^([A-Z])[\-\.\s]?((\d+)(?:\.\d+)?)$/;
	  const classicalWorkParsers = [
		/^(.*\S):\s+(.*)$/,
		/^(.+?):\s+([IVXC]+\.\s+.*)$/,
	  ];
	  var description;
	  if (isRequestNew || isRequestEdit) { // request
		description = [];
		if (release.release_date && !/^\s*\d{4}\s*$/.test(release.release_date) && !isNaN(i = new Date(release.release_date))) {
		  let today = new Date().getDateValue();
		  description.push((i.getDateValue() < today ? 'Released' : 'Releasing') + ' ' + i.toDateString());
		  if (prefs.upcoming_tags && i.getDateValue() >= today && (ref = document.getElementById('tags')) != null && !ref.disabled) {
			let tags = new TagManager(ref.value);
			tags.add(prefs.upcoming_tags);
			ref.value = tags.toString();
		  }
		}
		if (!prefs.include_tracklist_in_request) {
		  let summary = '';
		  if (release.totaldiscs > 1) summary += release.totaldiscs + ' discs, ';
		  summary += tracks.length + ' track'; if (tracks.length > 1) summary += 's';
		  if (totalTime > 0) summary += ', ' + makeTimeString(totalTime);
		  description.push(summary);
		}
		if (sourceUrl || release.urls.length > 0) description.push(getUrls());
		if (release.catalogs.length == 1 && /^\d{10,}$/.test(release.catalogs[0]) || /^\d{10,}$/.test(barCode)) {
		  description.push('[url=https://www.google.com/search?q=' + RegExp.lastMatch + ']Find more stores...[/url]');
		}
		if (prefs.include_tracklist_in_request) description.push(genPlaylist());
		if (release.descriptions.length > 0) Array.prototype.push.apply(description, release.descriptions);
		description = genAlbumHeader().concat(description.join('\n\n'));
		if (description.length > 0) {
		  ref = document.getElementById('description') || document.querySelector('textarea[name="description"]');
		  if (elementWritable(ref)) {
			ref.value = description;
		  } else if (isRequestEdit && ref != null && !ref.disabled) {
			ref.value = ref.value.length > 0 ? ref.value.concat('\n\n', description) : ref.value = description;
			preview(0);
		  }
		}
	  } else { // upload
		description = '';
		if (prefs.bpm_summary && albumBPM > 0) description += '\n\nAverage album BPM: [code]' + albumBPM + '[/code]';
		/*if (release.release_date) {
		  let rd = new Date(release.release_date);
		  if (!isNaN(rd)) description = '\n\nRelease date: ' + rd.toDateString();
		}*/
		let vinylRipInfo;
		if (release.descriptions.length > 0) {
		  description += '\n\n';
		  if (isRED && prefs.tracklist_style == 3) description += '[pad=0|20]';
		  if (release.descriptions.length == 1 && release.descriptions[0]
			  && (matches = vinylTest.exec(release.descriptions[0])) != null) {
			vinylRipInfo = release.descriptions[0].slice(matches.index).trim().split(/(?:[ \t]*\r?\n)+/);
			description += release.descriptions[0].slice(0, matches.index).trim();
		  } else description += release.descriptions.join('\n\n');
		  if (isRED && prefs.tracklist_style == 3) description += '[/pad]';
		}
		let oa = fetchOnlineAdditions().then(t => { description += '\n\n'.concat(t) }, reason => undefined);
		if (elementWritable(ref = document.getElementById('album_desc'))) {
		  ref.value = genPlaylist();
		  finalizeDesc(ref);
		}
		if ((ref = document.getElementById('body') || document.querySelector('textarea[name="body"]')) != null && !ref.disabled) {
		  if (ref.value.length == 0) ref.value = genPlaylist(); else {
			let editioninfo = '';
			if (editionTitle) {
			  editioninfo = '[size=5][b]' + editionTitle;
			  if (release.release_date && (i = extractYear(release.release_date)) > 0) editioninfo += ' (' + i + ')';
			  editioninfo += '[/b][/size]\n\n';
			}
			ref.value = ref.value.concat('\n\n', editioninfo, genPlaylist(false, false));
		  }
		  finalizeDesc(ref);
		}
		function finalizeDesc(elem) {
		  oa.then(function() {
			if (description) elem.value += description;
			preview(0);
		  });
		}
		// Release description
		if (elementWritable(ref = document.getElementById('release_samplerate'))) {
		  ref.value = Object.keys(release.srs).length == 1 && Object.keys(release.srs)[0] ?
			Math.floor(Object.keys(release.srs)[0] / 1000) :
		  		Object.keys(release.srs).length > 1 || isNaN(Object.keys(release.srs)[0]) ? '999' : '';
		}
		let lineage = '', rlsDesc = '';
		let drInfo = '[hide=DR' + (release.drs.length == 1 ? release.drs[0] : '') + '][pre][/pre]';
		let hasSR = Object.keys(release.srs).length > 0;
		let srInfo = hasSR ? Object.keys(release.srs).filter(sr => sr > 0).sort((a, b) => release.srs[b] - release.srs[a])
			.map(f => f / 1000).join('/').concat('kHz') : '';
		if (tracks.some(track => track.bd > 16)) {
		  if ([isOPS ? 'BD' : 'Blu-Ray', 'DVD', 'SACD'].includes(media)) {
			if (!isNWCD) rlsDesc = srInfo;
			addChannelInfo();
			if (media == 'SACD' || isFromDSD) addDSDInfo();
			if (prefs.cleanup_descriptions) addDRInfo();
			//addRGInfo();
			addHybridInfo();
			drInfo += '[/hide]';
		  } else if (media == 'Vinyl') {
			let hassr = hasSR && (!isNWCD || Object.keys(release.srs).length > 1);
			if (hassr) lineage = srInfo + ' ';
			if (vinylRipInfo) {
			  if (vinylTest.test(vinylRipInfo[0]) && RegExp.$2.toLowerCase() != 'unknown')
				vinylRipInfo[0] = vinylRipInfo[0].replace(vinylTest, '$1[color=blue]$2[/color]');
			  if (hassr) vinylRipInfo[0] = vinylRipInfo[0].replace(/^Vinyl\b/, 'vinyl');
			  lineage += vinylRipInfo[0] + '\n\n[u]Lineage:[/u]' + vinylRipInfo.slice(1).map(l => '\n'.concat([
				// RuTracker translation
				['Код класса состояния винила', 'Vinyl condition class'],
				['Устройство воспроизведения', 'Turntable'],
				['Головка звукоснимателя', 'Cartridge'],
				['Картридж', 'Cartridge'],
				['Предварительный усилитель', 'Preamplifier'],
				['АЦП', 'ADC'],
				['Программа-оцифровщик', 'Software'],
				['Обработка звука', 'Audio post-processing'],
				['Обработка', 'Post-processing'],
			  ].reduce((acc, it) => acc.replace(it[0], it[1]), l))).join('');
			} else lineage += (hassr ? 'Vinyl' : ' vinyl') + ' rip by [color=blue][/color]\n\n[u]Lineage:[/u]\n';
			let imgs = '\n[img][/img]'.repeat(6);
			if (!isNWCD) drInfo += '\n'.concat(imgs); else lineage += '\n\n[hide]'.concat(imgs.slice(1), '[/hide]');
			drInfo += '[/hide]';
		  } else { // WEB Hi-Res
			if (!isNWCD || Object.keys(release.srs).length > 1) rlsDesc = srInfo;
			if (release.channels && release.channels != 2) addChannelInfo();
			if (isFromDSD) addDSDInfo();
			if (!isFromDSD || prefs.cleanup_descriptions) addDRInfo();
			//addRGInfo();
			addHybridInfo();
			if (isFromDSD || prefs.cleanup_descriptions || Object.keys(release.srs).length == 1
				&& Object.keys(release.srs)[0] == 88200) drInfo += '[/hide]'; else drInfo = null;
		  }
		} else { // 16bit or lossy
		  if (Object.keys(release.srs).some(f => f != 44100)) rlsDesc = srInfo;
		  if (release.channels && release.channels != 2) addChannelInfo();
		  addDRInfo();
		  //addRGInfo();
		  if (prefs.cleanup_descriptions) drInfo += '[/hide]'; else drInfo = null;
		  if (release.codec == 'MP3' && release.vendor) {
			// TODO: parse mp3 vendor string
		  } else if (['AAC', 'Opus', 'Vorbis'].includes(release.codec) && release.vendor) {
			let _encoder_settings = release.vendor;
			if (release.codec == 'AAC' && /^qaac\s+[\d\.]+/i.test(release.vendor)) {
			  let enc = [];
			  if (matches = release.vendor.match(/\bqaac\s+([\d\.]+)\b/i)) enc[0] = matches[1];
			  if (matches = release.vendor.match(/\bCoreAudioToolbox\s+([\d\.]+)\b/i)) enc[1] = matches[1];
			  if (matches = release.vendor.match(/\b(AAC-\S+)\s+Encoder\b/i)) enc[2] = matches[1];
			  if (matches = release.vendor.match(/\b([TC]VBR|ABR|CBR)\s+(\S+)\b/)) { enc[3] = matches[1]; enc[4] = matches[2]; }
			  if (matches = release.vendor.match(/\bQuality\s+(\d+)\b/i)) enc[5] = matches[1];
			  _encoder_settings = 'Converted by Apple\'s ' + enc[2] + ' encoder (' + enc[3] + '-' + enc[4] + ')';
			}
			lineage = _encoder_settings;
		  }
		}
		function addDSDInfo() {
		  var nfo = ' DSD64';
		  if (prefs.sacd_decoder) nfo += ' using ' + prefs.sacd_decoder;
		  nfo += '\nOutput gain: [code]+0dB[/code]';
		  if (isNWCD) lineage = 'From' .concat(nfo); else {
			if (rlsDesc.length > 0) rlsDesc += ' from'; else rlsDesc = 'From';
			rlsDesc += nfo;
		  }
		}
		function addDRInfo() {
		  if (release.drs.length < 1 || document.getElementById('release_dynamicrange') != null) return;
		  var nfo = 'DR' + release.drs[0];
		  if (release.drs[0] < 4) nfo = '[color=red]'.concat(nfo, '[/color]');
		  if (rlsDesc.length > 0) rlsDesc += ' | ';
		  rlsDesc += nfo;
		}
		function addRGInfo() {
		  if (release.ags.length <= 0) return;
		  if (rlsDesc.length > 0) rlsDesc += ' | ';
		  rlsDesc += 'RG'; //rlsDesc += 'RG ' + ags[0];
		}
		function addChannelInfo() {
		  if (!release.channels) return;
		  var chi = getChanString(release.channels);
		  if (chi.length <= 0) return;
		  if (rlsDesc.length > 0) rlsDesc += ', '; else rlsDesc = 'Channels configuration: ';
		  rlsDesc += chi;
		}
		function addHybridInfo() {
		  if (release.bds.length > 1) release.bds.filter(bd => bd != 24).forEach(function(bd) {
			var hybrid_tracks = tracks.filter(it => it.bd == bd).sort(trackComparer).map(function(it) {
			  return (release.totaldiscs > 1 && it.discnumber ? it.discnumber + '-' : '').concat(it.tracknumber);
			});
			if (hybrid_tracks.length < 1) return;
			if (rlsDesc.length > 0) rlsDesc += '\n';
			rlsDesc += 'Note: track';
			if (hybrid_tracks.length > 1) rlsDesc += 's';
			rlsDesc += ' #' + hybrid_tracks.join(', ') +
			  (hybrid_tracks.length > 1 ? ' are' : ' is') + ' ' + bd + 'bit lossless';
		  });
		}
		function addRlsDate() {
		  if (prefs.insert_release_date && release.release_date && !/^\s*\d{4}\s*$/.test(release.release_date)
			  && !isNaN(i = new Date(release.release_date))) rlsDesc.push('Released ' + i.toDateString());
		}
		rlsDesc = rlsDesc.length > 0 ? [rlsDesc] : [];
		if ((ref = document.getElementById('release_lineage')) != null) {
		  lineage = lineage ? [lineage] : [];
		  if (drInfo) rlsDesc.push(drInfo);
		  addRlsDate();
		  if (sourceUrl || release.urls.length > 0) lineage.push(getUrls());
		  if (elementWritable(ref)) {
			ref.value = lineage.join('\n\n');
			preview(1);
		  }
		} else {
		  if (lineage.length > 0) rlsDesc.push(lineage);
		  if (drInfo) rlsDesc.push(drInfo);
		  addRlsDate();
		  if (sourceUrl || release.urls.length > 0) rlsDesc.push(getUrls());
		}
		if (elementWritable(ref = document.getElementById('release_desc'))) {
		  ref.value = rlsDesc.join('\n\n');
		  if (rlsDesc.length > 0) preview(isNWCD ? 2 : 1);
		}
		if (release.encoding == 'lossless' && release.codec == 'FLAC'
			&& tracks.some(track => track.bd == 24) && release.dirpaths.length == 1) {
		  if ((ref = document.getElementById('release_desc')) != null) GM_xmlhttpRequest({
			method: 'GET',
			url: new URL('file:'.concat(release.dirpaths[0], '\\foo_dr.txt')).href,
			responseType: 'blob',
			onload: function(response) {
			  if (response.status < 200 || response.status >= 400) return defaultErrorHandler(response);
			  if (!/(\[hide=DR\d*\]\[pre\])\[\/pre\]/im.test(ref.value)) return;
			  var ndx = RegExp.lastIndex + RegExp.$1.length;
			  ref.value = ref.value.slice(0, ndx).concat(response.responseText, ref.value.slice(ndx));
			},
			onerror: error => { console.error('foo_dr.txt not exists or is forbidden to read') },
			ontimeout: defaultTimeoutHandler,
		  });
		}
	  }
	  if (ajaxRejects > 0) {
		i = 'AJAX request(s) eliminated due to Gazelle policy. ' +
		  'Multiple artists not split correctly? Relaunch parsing in overwrite mode without page reload';
		try {
		  let delay = JSON.parse(window.localStorage[siteApiTimeframeStorageKey]).timeStamp + gazelleApiFrame - Date.now();
		  if (delay >= 0) {
			i += ' after ' + Math.ceil(delay / 1000) + 's';
			setTimeout(() => { addMessage('new AJAX timeframe for requery available', 'info') }, delay);
		  }
		  addMessage(i + '.', 'notice');
		} catch(e) { console.error(e) }
	  }
	  if (elementWritable(ref = document.getElementById('release_dynamicrange'))) {
		ref.value = release.drs.length == 1 ? release.drs[0] : '';
	  }
	  if (isRequestNew && prefs.request_default_bounty > 0) {
		let amount = prefs.request_default_bounty < 1024 ? prefs.request_default_bounty : prefs.request_default_bounty / 1024;
		if ((ref = document.getElementById('amount_box')) != null && !ref.disabled) ref.value = amount;
		if ((ref = document.getElementById('unit')) != null && !ref.disabled) {
		  ref.value = prefs.request_default_bounty < 1024 ? 'mb' : 'gb';
		}
		try { Calculate() } catch(e) { /* Orpheus bug void handler */ }
	  }
	  if (!media && (ref = document.getElementById('media')) != null && ref.value && ref.value != '---') media = ref.value;
	  if (!onlineSource) {
		if (prefs.honour_url && !sourceUrl && release.urls.length <= 0) addMessage('No lineage URL', 'notice');
		onlineSource = (function() {
		  if (sourceUrl || release.urls.length > 0) return urlResolver(sourceUrl || release.urls[0])
			.then(sourceUrl => fetchOnline_Music(sourceUrl, true).then(completeFromOnlineSource));
		  return Promise.reject('No lineage URL');
		})();
		if (prefs.check_integrity_online) onlineSource.catch(reason => lookupOnlineSource().then(function(result) {
		  if (typeof result == 'object') return parseLastFm(result);
		  if (urlParser.test(result)) return fetchOnline_Music(result, true);
		  return Promise.reject('Unhandled format');
		})).then(onlineCheck).catch(function(reason) {
		  if (!media || media == 'WEB') tracks.forEach(function(track) {
			if (!track.duration || track.duration < 29.6 || track.duration > 30.4) return;
			addMessage('track ' + track.tracknumber + ' possible track preview', 'warning');
		  });
	  	});
	  }
	  if (prefs.clean_on_apply) clipBoard.value = '';
	  prefs.save();
	  return true;

	  // ---------------------------------------------------------------------------------------------------------------

	  function genPlaylist(pad = true, header = true) {
		var style = prefs.tracklist_style;
		if (style == 2 && (tracks.map(track => track.title).some(notMonospaced))
			|| tracks.map(track => track.track_artist).some(notMonospaced)
			|| composerEmphasis && tracks.map(track => track.composer).some(notMonospaced)) style = 3;
		if (!style || style <= 0) return null;
		var playlist = '';
		if (tracks.length > 1 || prefs.singles_conventional_format || isRequestNew || isRequestEdit) {
		  if (style == 3) playlist = '[align=center]';
		  if (pad && isRED) playlist += '[pad=8|0|0|0]';
		  if (header) playlist += genAlbumHeader();
		  playlist += '[size=4][b][color=' + prefs.tracklist_head_color + ']Tracklisting[/color][/b][/size]';
		  if (pad && isRED) playlist += '[/pad]';
		  playlist += '\n'; //'[hr]';
		  let lastDisc, lastSubtitle, lastWork, lastSide, vinylTrackWidth;
		  let block = 0, classicalWorks = new Map();
		  if (composerEmphasis /*isClassical*/ && !tracks.some(it => it.discsubtitle)) {
			tracks.forEach(function(track) {
			  if (!track.composer) return;
			  (/*isClassical ? classicalWorkParsers : */classicalWorkParsers.slice(1)).forEach(function(classicalWorkParser) {
				if (track.classical_work || !classicalWorkParser.test(track.title)) return;
				classicalWorks.set(track.classical_work = RegExp.$1, {});
				track.classical_title = RegExp.$2;
			  });
			});
			for (iter of classicalWorks.keys()) {
			  let work = tracks.filter(track => track.classical_work == iter);
			  if (work.length > 1 || tracks.every(track => track.classical_work)) {
				if (work[0].track_artist && work[0].track_artist != release.artist && work.map(track => track.track_artist).homogeneous())
				  classicalWorks.get(iter).performer = realTrackArtist(work[0].track_artist);
				if (work[0].composer && release.composers.length > 1 && work.map(track => track.composer).homogeneous())
				  classicalWorks.get(iter).composer = work[0].composer;
			  } else {
				work.forEach(function(track) {
				  delete track.classical_work;
				  delete track.classical_title;
				});
				classicalWorks.delete(iter);
			  }
			}
		  }
		  let track, duration, volumes = new Map(tracks.map(it => [it.discnumber, undefined])), tnOffset = 0;
		  volumes.forEach(function(val, key) {
			volumes.set(key, new Set(tracks.filter(it => it.discnumber == key).map(it => it.discsubtitle)).size)
		  });
		  if (!tracks.every(it => !isNaN(parseInt(it.tracknumber.toString())))
			  && !tracks.every(it => vinyltrackParser.test(it.tracknumber.toString().toUpperCase()))) {
			addMessage('inconsistent tracks numbering (' + tracks.map(it => it.tracknumber) + ')', 'warning');
		  }
		  vinylTrackWidth = tracks.reduce(function(acc, it) {
			return Math.max(vinyltrackParser.test(it.tracknumber.toString().toUpperCase()) && parseInt(RegExp.$3), acc);
		  }, 0);
		  if (vinylTrackWidth) {
			vinylTrackWidth = vinylTrackWidth.toString().length;
			tracks.forEach(function(it) {
			  if (vinyltrackParser.test(it.tracknumber.toString().toUpperCase()) != null)
				it.tracknumber = RegExp.$1 + RegExp.$3.padStart(vinylTrackWidth, '0');
			});
			++vinylTrackWidth;
		  }
		  if (release.totaldiscs < 2 && tracks.reduce(computeLowestTrack, undefined) - 1)
			addMessage('volume ' + iter.discnumber + ' track numbering not starting from 1', 'info');
		  const padStart = '[pad=0|0|5|0]';
		  if (canSort && prefs.sort_tracklist) tracks.sort(trackComparer);
		  for (iter of tracks) {
			let title = '', trackArtist = undefined;
			if (iter.track_artist && iter.track_artist != release.artist) trackArtist = realTrackArtist(iter.track_artist);
			var ttwidth = vinylTrackWidth || (release.totaldiscs > 1 && iter.discnumber ?
				tracks.filter(it => it.discnumber == iter.discnumber) : tracks).reduce(function (accumulator, it) {
			  return Math.max(accumulator, (parseInt(it.tracknumber) || it.tracknumber).toString().length);
			}, 2);
			function realTrackNumber() {
			  var tn = parseInt(iter.tracknumber);
			  return isNaN(tn) ? iter.tracknumber : (tn - tnOffset).toString().padStart(ttwidth, '0');
			}
			switch (style) {
			  case 1:
			  case 3: {
				prologue('[size=' + prefs.tracklist_size + ']', '[/size]\n');
				track = '[b][color=' + prefs.tracklist_tracknumber_color + ']';
				track += realTrackNumber();
				track += '[/color][/b]' + prefs.title_separator;
				if (trackArtist && (!iter.classical_work || !classicalWorks.get(iter.classical_work).performer)) {
				  title = '[color=' + prefs.tracklist_artist_color + ']' + trackArtist + '[/color] - ';
				}
				title += iter.classical_title || iter.title;
				if (iter.composer && composerEmphasis && release.composers.length != 1
					&& (!iter.classical_work || !classicalWorks.get(iter.classical_work).composer)) {
				  title = title.concat(' [color=', prefs.tracklist_composer_color, '](', iter.composer, ')[/color]');
				}
				playlist += track + title;
				if (iter.duration) playlist += ' [i][color=' + prefs.tracklist_duration_color +'][' +
				  makeTimeString(iter.duration) + '][/color][/i]';
				break;
			  }
			  case 2: {
				prologue('[size=' + prefs.tracklist_size + '][pre]', '[/pre][/size]');
				track = realTrackNumber();
				track += prefs.title_separator;
				if (trackArtist && (!iter.classical_work || !classicalWorks.get(iter.classical_work).performer)) {
				  title = trackArtist + ' - ';
				}
				title += iter.classical_title || iter.title;
				if (composerEmphasis && iter.composer && release.composers.length != 1
					&& (!iter.classical_work || !classicalWorks.get(iter.classical_work).composer)) {
				  title = title.concat(' (', iter.composer, ')');
				}
				let l = 0, j, left, padding, spc;
				duration = iter.duration ? '[' + makeTimeString(iter.duration) + ']' : null;
				let width = prefs.max_tracklist_width - track.length;
				if (duration) width -= duration.length + 1;
				while (title.trueLength() > 0) {
				  j = width;
				  if (title.trueLength() > width) {
					while (j > 0 && title[j] != ' ') { --j }
					if (j <= 0) j = width;
				  }
				  left = title.slice(0, j).trim();
				  if (++l <= 1) {
					playlist += track + left;
					if (duration) {
					  spc = width - left.trueLength();
					  padding = (spc < 2 ? ' '.repeat(spc) : ' ' + prefs.pad_leader.repeat(spc - 1)) + ' ';
					  playlist += padding + duration;
					}
					width = prefs.max_tracklist_width - track.length - 2;
				  } else playlist += '\n' + ' '.repeat(track.length) + left;
				  title = title.slice(j).trim();
				}
				break;
			  }
			}
		  }
		  switch (style) {
			case 1:
			case 3:
			  if (totalTime > 0) playlist += '\n\n' + divs[0].repeat(10) + '\n[color=' + prefs.tracklist_duration_color +
				']Total time: [i]' + makeTimeString(totalTime) + '[/i][/color][/size]';
			  break;
			case 2:
			  if (totalTime > 0) {
				duration = '[' + makeTimeString(totalTime) + ']';
				playlist += '\n\n' + divs[0].repeat(32).padStart(prefs.max_tracklist_width);
				playlist += '\n' + 'Total time:'.padEnd(prefs.max_tracklist_width - duration.length) + duration;
			  }
			  playlist += '[/pre][/size]';
			  break;
		  }
		  if (style == 3) playlist += '[/align]';

		  function computeLowestTrack(acc, track) {
			if (Number.isNaN(acc)) return NaN;
			var tn = parseInt(track.tracknumber);
			if (isNaN(tn)) return NaN;
			return isNaN(acc) || tn < acc ? tn : acc;
		  }

		  function prologue(prefix, postfix) {
			function block1() {
			  if (block == 3) playlist += postfix;
			  playlist += '\n';
			  if (isRED && ![1, 2].includes(block)) playlist += padStart;
			  block = 1;
			}
			function block2() {
			  if (block == 3) playlist += postfix;
			  playlist += '\n';
			  if (isRED && ![1, 2].includes(block)) playlist += padStart;
			  block = 2;
			}
			function block3() {
			  //if (block == 2 && isRED) playlist += '[hr]';
			  if (isRED && [1, 2].includes(block)) playlist += '[/pad]';
			  playlist += '\n';
			  if (block != 3) playlist += prefix;
			  block = 3;
			}
			if (release.totaldiscs > 1 && iter.discnumber != lastDisc) {
			  block1();
			  lastDisc = iter.discnumber;
			  lastSubtitle = lastWork = undefined;
			  playlist += '[color=' + prefs.tracklist_disctitle_color + '][size=3][b]';
			  if (iter.identifiers.VOL_MEDIA && tracks.filter(it => it.discnumber == iter.discnumber)
				  .every(it => it.identifiers.VOL_MEDIA == iter.identifiers.VOL_MEDIA)) {
				playlist += iter.identifiers.VOL_MEDIA.toUpperCase() + ' ';
			  }
			  playlist += 'Disc ' + iter.discnumber;
			  if (iter.discsubtitle && (volumes.get(iter.discnumber) || 0) == 1) {
				playlist += ' – ' + iter.discsubtitle;
				lastSubtitle = iter.discsubtitle;
			  }
			  playlist += '[/b][/size]';
			  duration = tracks.filter(it => it.discnumber == iter.discnumber).reduce((acc, it) => acc + it.duration, 0);
			  if (duration > 0) playlist += ' [size=2][i][' + makeTimeString(duration) + '][/i][/size]';
			  playlist += '[/color]';
			  tnOffset = tracks.filter(track => track.discnumber == iter.discnumber).reduce(computeLowestTrack, undefined) - 1 || 0;
			  if (tnOffset) addMessage('volume ' + iter.discnumber + ' track numbering not starting from 1', 'info');
			}
			if (iter.discsubtitle != lastSubtitle) {
			  if (block != 1 || iter.discsubtitle) block1();
			  if (iter.discsubtitle) {
				playlist += '[color=' + prefs.tracklist_work_color + '][size=2][b]' + iter.discsubtitle + '[/b][/size]';
				duration = tracks.filter(it => it.discsubtitle == iter.discsubtitle)
				  .reduce((acc, it) => acc + it.duration, 0);
				if (duration > 0) playlist += ' [size=1][i][' + makeTimeString(duration) + '][/i][/size]';
				playlist += '[/color]';
			  }
			  lastSubtitle = iter.discsubtitle;
			}
			if (iter.classical_work != lastWork) {
			  if (iter.classical_work) {
				block2();
				playlist += '[color=' + prefs.tracklist_work_color + '][size=2][b]';
				if (release.composers.length != 1 && classicalWorks.get(iter.classical_work).composer) {
				  playlist += classicalWorks.get(iter.classical_work).composer + ': ';
				}
				playlist += iter.classical_work;
				playlist += '[/b]';
				if (classicalWorks.get(iter.classical_work).performer
					&& classicalWorks.get(iter.classical_work).performer != release.artist) {
				  playlist += ' (' + classicalWorks.get(iter.classical_work).performer + ')';
				}
				playlist += '[/size]';
				duration = tracks.filter(it => it.classical_work == iter.classical_work)
				  .reduce((acc, it) => acc + it.duration, 0);
				if (duration > 0) playlist += ' [size=1][i][' + makeTimeString(duration) + '][/i][/size]';
				playlist += '[/color]';
			  } else {
				if (block > 2) block1();
			  }
			  lastWork = iter.classical_work;
			}
			if (vinyltrackParser.test(iter.tracknumber)) {
			  if (block == 3 && lastSide && RegExp.$1 != lastSide) playlist += '\n';
			  lastSide = RegExp.$1;
			}
			block3();
		  } // prologue
		} else { // single
		  playlist += '[align=center]';
		  playlist += isRED ? '[pad=20|20|20|20]' : '';
		  playlist += '[size=4][b][color=' + prefs.tracklist_artist_color + ']' + release.artist + '[/color]';
		  playlist += isRED ? '[hr]' : '\n'.concat(divs[0].repeat(24), '\n');
		  playlist += tracks[0].title + '[/b]';
		  if (tracks[0].composer) {
			playlist += '\n[i][color=' + prefs.tracklist_composer_color + '](' + tracks[0].composer + ')[/color][/i]';
		  }
		  playlist += '\n\n[color=' + prefs.tracklist_duration_color +'][' + makeTimeString(tracks[0].duration) + '][/color][/size]';
		  if (isRED) playlist += '[/pad]';
		  playlist += '[/align]';
		}
		return prefs.colorless_tracklist ? playlist.replace(/\[color=\S+?\]/ig, '').replace(/\[\/color\]/ig, '') : playlist;
	  }

	  function getUrls() {
		var result = [];
		if (sourceUrl) result.push(sourceUrl);
		Array.prototype.push.apply(result, release.urls.filter(function(url) {
		  return !sourceUrl || url.toLowerCase() != sourceUrl.toLowerCase();
		}));
		return result.map(function(url, ndx) {
		  if (!urlParser.test(url)) return url;
		  url = new URL(url);
		  var logoUrl = prefs.use_store_logos ? [
			[/\b(?:qobuz\.com)$/i, 'https://ptpimg.me/1saep4.png'],
			[/\b(?:bandcamp\.com)$/i, 'https://ptpimg.me/vwki92.jpg' /*'https://ptpimg.me/7evz4g.png'*/],
			//[/\b(?:highresaudio\.com)$/i, 'https://ptpimg.me/65xx03.png'],
			[/\b(?:7digital\.com)$/i, 'https://ptpimg.me/300scj.png'],
			[/\b(?:mora\.jp)$/i, 'https://ptpimg.me/ma53oh.png'],
			[/\b(?:deezer\.com)$/i, 'https://ptpimg.me/181799.png'],
			[/\b(?:spotify\.com)$/i, 'https://ptpimg.me/xo5d1p.png'],
			[/\b(?:tidal\.com)$/i, 'https://ptpimg.me/w80424.png'],
			[/\b(?:music\.apple\.com)$/i, 'https://ptpimg.me/in7u5u.png'],
			[/\b(?:prestomusic\.com)$/i, 'https://ptpimg.me/q86vjt.png'],
			[/\b(?:prostudiomasters\.com)$/i, 'https://ptpimg.me/xkm0th.png'],
			[/\b(?:supraphonline\.cz)$/i, 'https://ptpimg.me/h85655.png'],
			[/^(?:(?:www\.)?hdtracks\.)/i, 'https://ptpimg.me/wx36i4.png'],
			[/\b(?:nativedsd\.com)$/i, 'https://ptpimg.me/m6j8gp.png'],
			[/\b(?:indies\.eu)$/i, 'https://ptpimg.me/8a4w49.png'],
			[/\b(?:e-onkyo\.com)$/i, 'https://ptpimg.me/uke3n1.png'],
			[/\b(?:beatport\.com)$/i, 'https://ptpimg.me/lf8q75.png'],
			[/\b(?:junodownload\.com)$/i, 'https://ptpimg.me/6c7y42.png'],
		  ].reduce((acc, site) => acc || (site[0].test(url.hostname) ? site[1] : null), null) : null;
		  return logoUrl ? '[url='.concat(url, '][img]', logoUrl, '[/img][/url]') : '[url]'.concat(url, '[/url]');
		}).join('\n');
	  }

	  function genAlbumHeader() {
		return !isVA && artists[0].length >= 3 ? '[size=4]' +
		  joinArtists(artists[0], artist => '[artist]' + artist + '[/artist]') + ' – ' + release.album + '[/size]\n\n' : '';
	  }

	  function findPreviousUploads() {
		let search = new URLSearchParams(document.location.search);
		if (search.get('groupid')) localFetch('/torrents.php?action=grouplog&groupid=' + search.get('groupid')).then(function(dom) {
		  dom.querySelectorAll('table > tbody > tr.rowa').forEach(function(tr) {
			if (/^\s*deleted\b/i.test(tr.children[3].textContent))
			  scanLog('Torrent ' + tr.children[1].firstChild.textContent);
		  });
		}); else {
		  let query = release.album;
		  if (!isVA && artists[0].length > 0 && artists[0].length < 3) query = artists[0].join(', ').concat(' - ', query);
		  scanLog(query);
		}

		function scanLog(query) {
		  localFetch('/log.php?search=' + encodeURIComponent(query)).then(function(dom) {
			dom.querySelectorAll('table > tbody > tr.rowb').forEach(function(tr) {
			  var size, msg = tr.children[1].textContent.trim();
			  if (/\b[\d\s]+(?:\.\d+)?\s*(?:([KMGT])I?)?B\b/.test(msg)) size = get_size_from_string(RegExp.lastMatch);
			  if (!msg.includes('deleted') || (/\[(.*)\/(.*)\/(.*)\]/.test(msg) ?
				 !release.codec || release.codec != RegExp.$1
				 //|| !release.encoding || release.encoding != RegExp.$2
				 || !media || media != RegExp.$3 :
				 !size || !albumSize || Math.abs(albumSize / size - 1) >= 0.1)) return;
			  addMessage('possibly same release previously uploaded and deleted: ' + msg, 'warning');
			});
		  });
		}

		function get_size_from_string(str) {
		  var matches = /\b([\d\s]+(?:\.\d+)?)\s*(?:([KMGT])I?)?B\b/.exec(str.replace(',', '.').toUpperCase());
		  if (!matches) return null;
		  var size = parseFloat(matches[1].replace(/\s+/g, ''));
		  if (matches[2] == 'K') { size *= Math.pow(1024, 1) }
		  else if (matches[2] == 'M') { size *= Math.pow(1024, 2) }
		  else if (matches[2] == 'G') { size *= Math.pow(1024, 3) }
		  else if (matches[2] == 'T') { size *= Math.pow(1024, 4) }
		  return Math.round(size);
		}
	  }

	  function getHomoIdentifier(id) {
		id = id.toUpperCase();
		return tracks.every((elem, ndx, arr) => elem.identifiers[id] != undefined
			&& elem.identifiers[id] === arr[0].identifiers[id]) ? tracks[0].identifiers[id] : undefined;
	  }

	  function getStoreUrl() {
		for (var it of [
		  ['ACOUSTICSOUNDS_ID', 'https://store.acousticsounds.com/d/{ID}/'],
		  ['ALLMUSIC_ID', 'https://www.allmusic.com/album/{ID}'],
		  ['AMAZON_ID', 'https://www.amazon.com/gp/product/{ID}'],
		  ['AMID', 'https://www.allmusic.com/album/{ID}'],
		  ['APPLE_ID', 'https://music.apple.com/album/{ID}'],
		  ['ASIN', 'https://www.amazon.com/gp/product/{ID}'],
		  ['BEATPORT_ID', 'https://www.beatport.com/release/2/{ID}'],
		  ['DEEZER_ID', deezerAlbumPrefix + '{ID}'],
		  ['DISCOGS_ID', discogsOrigin + '/release/{ID}'],
		  ['EONKYO_ID', 'https://www.e-onkyo.com/music/album/{ID}/'],
		  ['GOOGLE_ID', 'https://play.google.com/store/music/album/?id={ID}'],
		  ['INDIESSCOPE_ID', 'https://www.indies.eu/alba/{ID}/'],
		  ['ITUNES_ID', 'https://music.apple.com/album/{ID}'],
		  ['JUNODOWNLOAD_ID', 'https://www.junodownload.com/products/{ID}'],
		  ['MBID', mbrRlsPrefix + '{ID}'],
		  ['PROSTUDIOMASTERS_ID', 'https://www.prostudiomasters.com/album/page/{ID}'],
		  ['SPOTIFY_ID', 'https://open.spotify.com/album/{ID}'],
		  ['TRAXSOURCE_ID', 'https://www.traxsource.com/title/{ID}/'],
		  ['VGMDB_ID', 'https://vgmdb.net/album/{ID}'],
		  ['TIDAL_ID', 'https://listen.tidal.com/album/{ID}'],
		  ['OTOTOY_ID', 'https://ototoy.jp/_/default/p/{ID}'],
		  ['YANDEX_ID', 'https://music.yandex.ru/album/{ID}'],
		]) {
		  let ID = getHomoIdentifier(it[0]);
		  if (ID) return it[1].replace('{ID}', ID);
		}
		return undefined;
	  }

	  function getCoverOnline() {
		var url = sourceUrl || release.urls[0], apiFirst;
		if ((i = getHomoIdentifier('APPLE_ID') || getHomoIdentifier('ITUNES_ID')) || itunesRlsParser.test(url) && (i = parseInt(RegExp.$1))) {
		  apiFirst = queryItunesAPI('lookup', { id: i })
			.then(lookup => lookup.resultCount > 0 ? setItunesImage(lookup.results[0]) : Promise.reject('no cover'));
		} else if (i = getHomoIdentifier('DEEZER_ID') || dzrRlsParser.test(url) && (i = parseInt(RegExp.$1))) {
		  apiFirst = queryDeezerAPI('album/' + i)
		  	.then(result => result.id ? setDeezerImage(result) : Promise.reject('No cover'));
		} else if ((prefs.discogs_key && prefs.discogs_secret || discogs_token)
			&& (i = getHomoIdentifier('DISCOGS_ID') || dcRlsParser.test(url) && RegExp.$1)) {
		  apiFirst = queryDiscogsAPI('releases/' + i).then(function(release) {
			return release.images.length > 0 ? setCover(release.images[0].uri) : Promise.reject('No cover');
		  });
		} else if ((i = getHomoIdentifier('MBID') || mbrRlsParser.test(url) && RegExp.$1)) {
		  apiFirst = getMusicBrainzCovers(i).then(function(covers) {
			return covers != null ? setCover(covers[1][0]) : Promise.reject('No cover');
		  });
		} else if (i = getHomoIdentifier('TIDAL_ID') || tidalRlsParser(url)
			&& (i = parseInt(RegExp.$1)) > 0) apiFirst = queryTidalAPI('album', { albumId: i }).then(function(album) {
		  for (var row of album.rows) {
			var albumHeader = row.modules.find(module => module.type == 'ALBUM_HEADER');
			if (albumHeader != undefined && albumHeader.album.cover) return 'https://resources.tidal.com/images/'
			  .concat(albumHeader.album.cover.replace(/-/g, '/'), '/1280x1280.jpg');
		  }
		  return Promise.reject('Image not found');
		}); else if (url.toLowerCase().includes('mora.jp/')) apiFirst = loadMoraMetadata(url).then(function(packageMeta) {
		  return setCover(packageMeta.packageUrl + packageMeta.fullsizeimage);
		}); else apiFirst = Promise.reject('No known API binding');
		return apiFirst.catch(reason => imageUrlResolver(url).then(setCover));
	  }

	  function searchCoverOnline() {
		switch (typeof prefs.cover_lookup_provider == 'string' && prefs.cover_lookup_provider.toLowerCase()) {
		  case 'itunes': return searchCoverOnline_iTunes();
		  case 'deezer': return searchCoverOnline_Deezer();
		  case 'google': return searchCoverOnline_GooglePlay();
		  case 'musicbrainz': return searchCoverOnline_MBR();
		  case 'lastfm': return searchCoverOnline_LastFM();
		  case 'qobuz': return searchCoverOnline_Qobuz();
		  case 'all': return searchCoverOnline_iTunes()
						.catch(searchCoverOnline_LastFM)
							.catch(searchCoverOnline_Deezer)
								.catch(searchCoverOnline_MBR)
									.catch(searchCoverOnline_Qobuz)
										.catch(searchCoverOnline_GooglePlay);
		}
		return Promise.reject('No valid service selected');

		function searchCoverOnline_iTunes() {
		  return amLookup().then(album => setItunesImage(album)
			.then(imgUrl => { info('Apple Music', album.collectionViewUrl, album.collectionId) }));
		}
		function searchCoverOnline_Deezer() {
		  return deezerLookup().then(function(album) {
			return setDeezerImage(album)
			  .then(imgUrl => { info('Deezer', deezerAlbumPrefix + album.id, album.id) });
		  });
		}
		function searchCoverOnline_GooglePlay() {
		  var query = new URLSearchParams({
			q: '"' + (isVA ? VA : release.artist) + '" "' + release.album + '"',
			c: 'music',
		  });
		  return globalFetch('https://play.google.com/store/search?'.concat(query)).then(function(response) {
			try {
			  let _objs = loadGoogleData(response);
			  let _results = _objs.filter(function(obj) { try { return typeof obj[0][4] == 'boolean' } catch(e) { return false } });
			  if (_results.length == 1) _results = _results[0][0][1]; else throw 'Results metadata not found';
			} catch(e) { }
			var results = response.document.querySelectorAll('div:first-of-type + div[jscontroller]:last-of-type');
			if (results.length > 0) for (var ndx = 0; ndx < results.length; ++ndx) {
			  let items = [];
			  results[ndx].querySelectorAll(':scope > div').forEach(function(result) {
				var img = result.querySelector('span > span > img');
				img = img != null ? (img.src || img.dataset.src).replace(/=[a-z]\d+$/, '=w0') : null;
				var album = result.querySelector('a > div[title]');
				if (album == null) return;
				var artist = album.parentNode.parentNode.parentNode.querySelector('a > div:not([title])')
				artist = artist != null ? artist.textContent.trim() : null;
				var url = album.parentNode.href;
				var id = /\?id=(\w+)\b/i.test(album.parentNode.href) && RegExp.$1 || null;
				album = album.textContent.trim();
				items.push({ id: id, url: url, artist: artist, album: album, imgUrl: img });
			  });
			  for (i = 0; i < 3; ++i) {
				var f = items.filter(release => releasesMatch(release.artist, release.album, i));
				if (f.length > 1) return Promise.reject('Google Play Music: ambiguity');
				if (f.length == 1) break;
			  }
			  if (i >= 3) return Promise.reject('Google Play Music: no matches');
			  if (prefs.diag_mode && i == 2) console.debug('Google Play Music fuzzy match:', release, '==', f[0]);
			  if (f[0].imgUrl) return setCover(f[0].imgUrl)
				.then(release => { info('Google Play Music', f[0].url, f[0].id) });
			}
		  });
		  return Promise.reject('Google Play Music: no matches');
		}
		function searchCoverOnline_MBR() {
		  return mbLookupByBarcode().catch(mbLookupByASIN).catch(mbLookupByTOC)
			.catch(reason => mbLookup().then(release => [release]))
			.catch(mbLookupByAutoTOC)
			.then(releases => Promise.all(releases.map(release => getMusicBrainzCovers(release.id))))
			.then(function(releases) {
			  for (var rls of releases) if (rls != null) return _setCover(rls);
			  return Promise.reject('MusicBrainz: no covers found');
			});

		  function _setCover(rls) {
			return setCover(rls[1][0]).then(function(imgUrl) {
			  if (/\/release\/(\S+)$/i.test(rls[0])) info('Musicbrains', rls[0], RegExp.$1);
			  return imgUrl;
			});
		  }
		}
		function searchCoverOnline_LastFM() {
		  return queryLastFmAPI('album.getinfo', {
			artist: (isVA ? VA : release.artist),
			album: release.album,
		  }).then(function(result) {
			if (result.error) return Promise.reject('Last.fm: '.concat(result.message));
			var image = ['mega', 'extralarge', '', 'large', 'medium', 'small'].reduce(function(acc, size) {
			  return acc || result.album.image.find(image => image.size == size && urlParser.test(image['#text']));
			}, undefined);
			if (!image) return Promise.reject('Last.fm: no cover for matched album');
			image = image['#text'];
			return setCover(image.replace(/\/\d+(?:x\d+|s)\//i, '/')).catch(reason => setCover(image))
				.then(() => { info('Last.fm', result.album.url, result.album.id || result.album.mbid || '#N/A') });
		  });
		}
		function searchCoverOnline_Qobuz() {
		  qbLookup().then(function(album) {
			return setCover(album.imgUrl.replace(/_\d+(?=\.\w+$)/, '_max'))
			  .catch(reason => setCover(album.imgUrl.replace(/_\d+(?=\.\w+$)/, '_600')))
			  .catch(reason => setCover(album.imgUrl))
			  .then(function(imgUrl) {
				info('Qobuz', album.href, album.id);
				return imgUrl;
			  });
		  });
		}
		function info(service, url, id) {
		  addMessage(new HTML('used cover image from ' + service + ' release Id ' +
			'<a style="color: #00f3ff;" target="_blank" href="'+ url + '">' + id + '</a>'), 'info');
		}
	  }

	  function setItunesImage(album) {
		return urlParser.test(album.artworkUrl100) ?
		  setCover(album.artworkUrl100.replace('100x100bb', '100000x100000-999'))
			.catch(reason => setCover(album.artworkUrl100)) : Promise.reject('Apple Music image not valid URL');
	  }
	  function setDeezerImage(album) {
		return urlParser.test(album.cover_xl) ?
		  setCover(album.cover_xl.replace('1000x1000-000000-80-0-0', '1400x1400-000000-100-0-0'))
			.catch(reason => setCover(album.cover_xl)) : Promise.reject('Deezer image not valid URL');
	  }

	  function completeFromOnlineSource(onlineTracks) {
		fillMissingValue(document.getElementById('media'), 'media');
		fillMissingValue(document.getElementById('year'), 'album_year');
		ref = document.getElementById('remaster_year') || !isUpload && document.querySelector('input[name="year"]');
		if (ref != null && !ref.disabled && (ref.value == '' || !isRED && ref.value == '---')) {
		  var value = getHomoValue('release_date');
		  if (value != null) ref.value = extractYear(value);
		}
		fillMissingValue(document.getElementById('remaster_record_label')
			|| document.querySelector('input[name="recordlabel"]'), 'label');
		if (elementWritable(ref = document.getElementById('remaster_catalogue_number')
			|| document.querySelector('input[name="cataloguenumber"]'))) {
		  let catNo = getHomoValue('catalog');
		  if (!catNo && onlineTracks.every(track => track.identifiers.BARCODE
			   && track.identifiers.BARCODE == onlineTracks[0].identifiers.BARCODE)) {
			catNo = parseInt(onlineTracks[0].identifiers.BARCODE.replace(/\s+/g, ''));
		  }
		  if (catNo) ref.value = catNo;
		}
		return onlineTracks;

		function getHomoValue(propName) {
		  return onlineTracks[0][propName] && onlineTracks.map(track => track[propName]).homogeneous() ?
			onlineTracks[0][propName] : null;
		}
		function fillMissingValue(node, propName) {
		  if (!node || node.disabled || node.value != '' && (isRED || node.value != '---')) return;
		  var value = getHomoValue(propName);
		  if (value != null) node.value = value;
		}
	  }

	  function onlineCheck(onlineTracks) {
		if (!Array.isArray(onlineTracks) || onlineTracks.length <= 0) {
		  addMessage('online check not performed (empty tracklist)', 'notice');
		  return Promise.reject('No tracks');
		}
		var issueCounter = 0, hiresTimes = onlineTracks.some(function(track) {
		  var remainder = Math.floor((track.duration - Math.floor(track.duration)) * 1000) / 100;
		  return remainder > Math.floor(remainder);
		});
		onlineTracks.forEach(processTrackArtists);
		if (onlineTracks[0].artist && onlineTracks.map(track => track.artist).homogeneous()
			&& (isVA ? !vaParser.test(onlineTracks[0].artist) : mainArtistMismatch())) {
		  ++issueCounter;
		  addMessage(new HTML('online album main artist mismatch ("' +
			safeText(release.artist).bold() + '" ≠ "' + safeText(onlineTracks[0].artist).bold() + '")'), 'warning');
		}
		if (onlineTracks[0].album && onlineTracks.map(track => track.album).homogeneous()
			&& mismatch(release.album, onlineTracks[0].album)
		   	&& mismatch(release.album, removeFeatArtists(onlineTracks[0].album))) {
		  ++issueCounter;
		  addMessage(new HTML('online album title mismatch ("' +
			safeText(release.album).bold() + '" ≠ "' + safeText(onlineTracks[0].album).bold() + '")'), 'warning');
		}
		if (onlineTracks[0].label && onlineTracks.map(track => track.label).homogeneous()
			&& mismatch(release.label, onlineTracks[0].label, /-/g)) {
		  ++issueCounter;
		  addMessage(new HTML('online album label mismatch ("' +
			safeText(release.label).bold() + '" ≠ "' + safeText(onlineTracks[0].label).bold() + '")'), 'notice');
		}
		if (release.catalogs.length == 1
			&& onlineTracks[0].catalog && onlineTracks.map(track => track.catalog).homogeneous()
			&& mismatch(release.catalogs[0], onlineTracks[0].catalog, /[\s\-]/g)) {
		  ++issueCounter;
		  addMessage(new HTML('online album catalogue# mismatch ("' +
			safeText(release.catalogs[0]).bold() + '" ≠ "' + safeText(onlineTracks[0].catalog).bold() + '")'), 'notice');
		}
		if (onlineTracks[0].album_year && onlineTracks.map(track => track.album_year).homogeneous()
			&& release.album_year != onlineTracks[0].album_year) {
		  ++issueCounter;
		  addMessage(new HTML('online album year mismatch (' +
			(release.album_year || '<unset>').toString().bold() + ' ≠ ' + onlineTracks[0].album_year.toString().bold() + ')'), 'warning');
		}
		if (onlineTracks[0].release_date && release.release_date && onlineTracks.map(track => track.release_date).homogeneous()
			&& new Date(release.release_date.toString()).getDateValue()
				!= new Date(onlineTracks[0].release_date.toString()).getDateValue()) {
		  ++issueCounter;
		  addMessage(new HTML('online album release date mismatch (' +
			(release.release_date || '<unset>').toString().bold() + ' ≠ ' + onlineTracks[0].release_date.toString().bold() + ')'), 'notice');
		}
		if (tracks.length != onlineTracks.length) {
		  ++issueCounter;
		  addMessage(new HTML('online album different tracklist length (' + tracks.length.toString().bold() +
			' ≠ ' + onlineTracks.length.toString().bold() + ')'), 'warning');
		}
		if (totalTime > 0) {
		  let ttOnline = onlineTracks.reduce((acc, track) => acc + (track.duration || NaN), 0);
		  if (ttOnline > 0 && Math.abs(totalTime - ttOnline) * 100 / ttOnline > (media == 'Vinyl' ?
				prefs.vinyl_duration_divergency : hiresTimes ? 0.1 : prefs.duration_divergency)) {
			++issueCounter;
			addMessage(new HTML('online album duration mismatch (' + makeTimeString(totalTime).bold() +
				' ≠ ' + makeTimeString(ttOnline).bold() + ')'), 'warning');
		  }
		}
		for (let ndx = 0; ndx < tracks.length; ++ndx) {
		  if (ndx >= onlineTracks.length) {
			addMessage('end of online tracklist reached, tracks from #' + (ndx + 1) + ' to end will not be checked', 'notice');
			break;
		  }
		  if (mismatch(tracks[ndx].title, onlineTracks[ndx].title)
				&& mismatch(tracks[ndx].title, removeFeatArtists(onlineTracks[ndx].title))) {
			++issueCounter;
			addMessage('online track #' + (ndx + 1) + ' title mismatch ("' +
				(tracks[ndx].title || '') + '" ≠ "' + (onlineTracks[ndx].title || '') + '")', 'warning');
		  }
		  if (onlineTracks[ndx].track_artist && onlineTracks[ndx].track_artist != onlineTracks[ndx].artist) {
			var trackArtists = getArtists(tracks[ndx].track_artist);
			var onlineSrackArtists = getArtists(onlineTracks[ndx].track_artist);
			if (!trackArtists[0].equalCaselessTo(onlineSrackArtists[0])
				|| !trackArtists[1].equalCaselessTo(onlineSrackArtists[1])) {
			  ++issueCounter;
			  addMessage('online track #' + (ndx + 1) + ' track artist mismatch ("' +
				(tracks[ndx].track_artist || '') + '" ≠ "' + (onlineTracks[ndx].track_artist || '') + '")', 'notice');
			}
		  }
		  if (onlineTracks[ndx].tracknumber && tracks[ndx].tracknumber != onlineTracks[ndx].tracknumber) {
			++issueCounter;
			addMessage('online track #' + (ndx + 1) + ' track number mismatch (' +
				(tracks[ndx].tracknumber || '<unset>') + ' ≠ ' + onlineTracks[ndx].tracknumber + ')',
				release.totaldiscs > 1 ? 'notice' : 'warning');
		  }
		  if (onlineTracks[ndx].discnumber && (onlineTracks[ndx].discnumber > 1 || tracks[ndx].discnumber)
			  && tracks[ndx].discnumber != onlineTracks[ndx].discnumber) {
			++issueCounter;
			addMessage('online track #' + (ndx + 1) + ' disc number mismatch (' +
				(tracks[ndx].discnumber || '<unset>') + ' ≠ ' + onlineTracks[ndx].discnumber + ')', 'warning');
		  }
		  if (onlineTracks[ndx].discsubtitle && mismatch(tracks[ndx].discsubtitle, onlineTracks[ndx].discsubtitle)) {
			++issueCounter;
			addMessage('online track #' + (ndx + 1) + ' disc subtitle mismatch ("' +
				(tracks[ndx].discsubtitle || '') + '" ≠ "' + onlineTracks[ndx].discsubtitle + '")', 'notice');
		  }
		  let timeDif = tracks[ndx].duration && onlineTracks[ndx].duration
			  && Math.abs(tracks[ndx].duration - onlineTracks[ndx].duration);
		  if (timeDif >= (media != 'Vinyl' ? 2.5 : hiresTimes ? 0.1 : 5)) {
			++issueCounter;
			addMessage('online track #' + (ndx + 1) + ' duration mismatch (' +
				makeTimeString(tracks[ndx].duration) + ' ≠ ' + makeTimeString(onlineTracks[ndx].duration) + ')',
				(timeDif >= (media != 'Vinyl' ? 5 : hiresTimes ? 0.2 : 8) ? 'warning' : 'notice'));
		  }
		}
		if (issueCounter == 0) {
		  i = 'online check completed without remarks';
		  if (prefs.messages_verbosity >= 1) addMessage(i, 'info'); else console.debug(i);
		}

		function mainArtistMismatch() {
		  var onlineMainArtists = getArtists(onlineTracks[0].artist);
		  return !onlineMainArtists[0].equalCaselessTo(artists[0]) || !onlineMainArtists[1].equalCaselessTo(albumGuests);
		}
		function removeFeatArtists(title) {
		  return featParsers.slice(3).reduce(function(acc, rx, ndx) {
			return rx.test(acc) && (ndx < 5 || splitArtists(RegExp.$1).every((artist, ndx) => looksLikeTrueName(artist, 1))) ?
			  acc.replace(rx, '') : acc;
		  }, title || '')
		}
		function mismatch(localStr, onlineStr, rx) {
		  return normalize(localStr) != normalize(onlineStr);

		  function normalize(val) {
			if (val == undefined || val == null) return '';
			if (typeof val != 'string') val = val.toString();
			if (rx instanceof RegExp || typeof rx == 'string') val = val.replace(rx, '');
			val = val.replace(/[\(\)\-\s]+/g, '');
			return prefs.strict_online_check ? val : val.toLowerCase();
		  }
		}
	  }

	  function lookupOnlineSource() {
		const commonMedia = !media || ['CD', 'WEB'].includes(media);
		const noMultivolume = !release.totaldiscs || release.totaldiscs < 2;
		var workers = [
		  /*  0 */ barCode ? querySpotifyAPI('search', { q: 'barcode:' + barCode, type: 'album' })
		  	.then(result => result.albums.total > 0 ? result.albums.items : Promise.reject('Spotify: no matches'))
		  		: Promise.reject('Spotify: unknown barcode'),
		  /*  1 */ commonMedia ? spotifyLookup() : Promise.reject('Spotify: different media'),
		  /*  2 */ mbLookupByBarcode(),
		  /*  3 */ mbLookupByASIN(),
		  /*  4 */ mbLookupByTOC(),
		  /*  5 */ mbLookup(),
		  /*  6 */ commonMedia && noMultivolume ? deezerLookup() : Promise.reject('Deezer: different media'),
		  /*  7 */ commonMedia ? amLookup() : Promise.reject('Apple Music: different media'),
		  /*  8 */ dcLookup(),
		  /*  9 */ mbLookupByAutoTOC(),
		  /* 10 */ commonMedia && noMultivolume ? queryLastFmAPI('album.getinfo', {
			  artist: (isVA ? VA : release.artist),
			  album: release.album,
			}).then(result => result.error ? Promise.reject('Last.fm: '.concat(result.message)) : result.album)
		  		: Promise.reject('Last.fm: different media'),
		];
		if (prefs.diag_mode) workers.forEach(function(worker, ndx) {
		  worker.then(result => { console.debug('Worker[' + ndx + '] matched:', result) })
		  	.catch(reason => { console.debug('Worker[' + ndx + '] failed:', reason) });
		});
		return workers[0].then(function(albums) {
		  if (prefs.diag_mode) console.debug('Spotify lookup by barcode successfull:', barCode, ' matches:', albums.length);
		  info('Spotify', albums[0].external_urls.spotify, albums[0].id);
		  return albums[0].href;
		}).catch(reason => workers[1].then(function(album) {
		  info('Spotify', album.external_urls.spotify, album.id);
		  return album.href;
		})).catch(reason => workers[2].then(mbEpilogue))
		.catch(reason => workers[3].then(mbEpilogue))
		.catch(reason => workers[4].then(mbEpilogue))
		.catch(reason => workers[5].then(function(release) {
		  info('MusicBrainz', mbrRlsPrefix + release.id, release.id);
		  return mbrRlsPrefix.concat(release.id);
		})).catch(reason => workers[6].then(function(album) {
		  info('Deezer', deezerAlbumPrefix.concat(album.id), album.id);
		  return 'https://api.deezer.com/album/'.concat(album.id);
		})).catch(reason => workers[7].then(function(collection) {
		  info('Apple Music', collection.collectionViewUrl, collection.collectionId);
		  return collection.collectionViewUrl;
		})).catch(reason => workers[8].then(function(releases) {
		  info('Discogs', discogsOrigin.concat(releases[0].uri), releases[0].id);
		  return releases[0].resource_url;
		})).catch(reason => workers[9].then(mbEpilogue))
		.catch(reason => workers[10].then(function(album) {
		  info('Last.fm', album.url, album.id || album.mbid || '#N/A');
		  return album; // return object
		})).catch(function(reason) {
		  reason = 'online check not performed (no matches for this release)';
		  if (prefs.check_integrity_online) addMessage(reason, 'notice');
		  return Promise.reject(reason);
		});

		function mbEpilogue(releases) {
		  info('MusicBrainz', mbrRlsPrefix + releases[0].id, releases[0].id);
		  return mbrRlsPrefix + releases[0].id;
		}
		function info(service, url, id) {
		  if (prefs.check_integrity_online) addMessage(new HTML('checking online against ' + service +
			' release Id <a style="color: #00f3ff;" target="_blank" href="' + url + '">' + id + '</a>'), 'info');
		}
	  }

	  function spotifyLookup() {
		return querySpotifyAPI('search', {
		  q: 'artist:"' + release.artist + '" album:"' + release.album + '"',
		  type: 'album',
		  limit: 50,
		}).then(function(result) {
		  if (result.albums.total <= 0) return Promise.reject('Spotify: no matches');
		  for (i = 0; i < 3; ++i) {
			var f = filter(i);
			if (f.length > 1) return Promise.reject('Spotify: ambiguity');
			if (f.length == 1) break;
		  }
		  if (i >= 3) return Promise.reject('Spotify: no matches');
		  if (prefs.diag_mode && i == 2) console.debug('Spotify fuzzy match:', release, '==', f[0]);
		  return f[0];

		  function filter(level) {
			return result.albums.items.filter(function(album) {
			  return (album.album_type == 'single' ? ['Single', 'EP'].some(rt => releaseType == getReleaseIndex(rt))
					: releaseType != getReleaseIndex('Single'))
			  	&& releasesMatch(album.artists.map(artist => artist.name), album.name, level);
			});
		  }
		})
	  }
	  function deezerLookup() {
		return queryDeezerAPI('search', {
		  q: 'artist:"' + release.artist + '" album:"' + release.album + '"',
		  strict: 'on',
		  order: 'RANKING',
		}).then(function(result) {
		  if (result.total <= 0) return Promise.reject('Deezer: no matches');
		  for (i = 0; i < 3; ++i) {
			var f = filter(i);
			if (f.length > 1) return Promise.reject('Deezer: ambiguity');
			if (f.length == 1) break;
		  }
		  if (i >= 3) return Promise.reject('Deezer: no matches');
		  if (i == 2) console.debug('Deezer fuzzy match:', release, '==', f[0]);
		  return f[0];

		  function filter(level) {
			var albums = [];
			result.data.forEach(function(match) {
			  if (!releasesMatch(match.artist.name, match.album.title, level)) return;
			  if (albums.find(album => album.id == match.album.id) == undefined) albums.push(match.album);
			});
			return albums;
		  }
		});
	  }
	  function amLookup() {
		return queryItunesAPI('search', {
		  term: '"' + (isVA ? VA : release.artist) + '" "' + release.album + '"',
		  media: 'music',
		  entity: 'album',
		  //country: 'US',
		}).then(function(result) {
		  if (result.resultCount <= 0) return Promise.reject('Apple Music: no matches');
		  for (i = 0; i < 3; ++i) {
			var f = filter(i);
			if (f.length > 1) return Promise.reject('Apple Music: ambiguity');
			if (f.length == 1) break;
		  }
		  if (i >= 3) return Promise.reject('Apple Music: no matches');
		  if (prefs.diag_mode && i == 2) console.debug('Apple Music fuzzy match:', release, '==', f[0]);
		  return f[0];

		  function filter(level) {
			var preFilter = result.results.filter(function(collection) {
			  var isSingle = collection.collectionName.endsWith(' - Single');
			  if (isSingle) collection.collectionName = collection.collectionName.slice(0, -9);
			  var isEP = collection.collectionName.endsWith(' - EP');
			  if (isEP) collection.collectionName = collection.collectionName.slice(0, -5);
			  isSingle = isSingle || collection.collectionType == 'Single';
			  isEP = !isSingle && (isEP || collection.collectionType == 'EP');
			  return (releaseType == getReleaseIndex('Single')) == isSingle
			  	&& (!isEP || releaseType == getReleaseIndex('EP'))
			  	&& releasesMatch(collection.artistName, collection.collectionName, level);
			});
			return preFilter.some(collection => /\b(?:explicit)/i.test(collection.collectionExplicitness)) ?
			  preFilter.filter(collection => !/\b(?:clean)/i.test(collection.collectionExplicitness)) : preFilter;
		  }
		});
	  }
	  function mbLookupByBarcode() {
		if (!barCode) return Promise.reject('MusicBrainz: unknown barcode');
		return queryMusicBrainzAPI('release', { query: 'barcode:' + barCode }).then(function(result) {
		  if (result.count <= 0) return Promise.reject('MusicBrainz: no matches');
		  if (prefs.diag_mode) console.debug('MusicBrainz lookup by barcode successfull: ' + barCode + '; matches: ' + result.count);
		  return result.releases;
		});
	  }
	  function mbLookupByASIN() {
		var asin = getHomoIdentifier('ASIN');
		if (!asin) return Promise.reject('MusicBrainz: unknown ASIN');
		asin = asin.replace(/\s+/g, '');
		return queryMusicBrainzAPI('release', { query: 'asin:' + asin }).then(function(result) {
		  if (result.count <= 0) return Promise.reject('MusicBrainz: no matches');
		  if (prefs.diag_mode) console.debug('MusicBrainz lookup by ASIN successfull: ' + asin + '; matches: ' + result.count);
		  return result.releases;
		});
	  }
	  function mbLookupByTOC() {
		var TOC;
		if (TOC = getHomoIdentifier('ITUNES_TOC')) { // iTunes scheme
		  TOC = TOC.split('+').map(index => parseInt(index));
		  TOC = [1, TOC[2], TOC[1]].concat(TOC.slice(3));
		} else if (TOC = getHomoIdentifier('CT_TOC')) { // CUETools scheme
		  TOC = TOC.split('+').map(index => parseInt(index, 16));
		  TOC = [1, TOC.shift(), TOC.pop()].concat(TOC);
		}
		return mbLookupByDiscID(TOC);
	  }
	  function mbLookupByAutoTOC() {
		if (release.totaldiscs > 1) return Promise.reject('TOC lookup not possible for multidisc release');
		if (!tracks.every(track => track.sr > 0 && track.samples > 0))
		  return Promise.reject('MusicBrainz: insufficient information for TOC calculation');
		var lastFrame = 0;
		var TOC = [0].concat(tracks.map(track => (lastFrame += Math.round(track.samples * 75 / track.sr))))
			.map(offset => 150 + offset);
		TOC.unshift(TOC.pop());
		return mbLookupByDiscID([1, tracks.length].concat(TOC));
	  }
	  function mbComputeDiscID(mbTOC) {
		if (!Array.isArray(mbTOC) || mbTOC.length != mbTOC[1] - mbTOC[0] + 4 || mbTOC[1] - mbTOC[0] > 98) return null;
		var tocStr = [mbTOC[0], mbTOC[1]].map(track => track.toString(16).padStart(2, '0'))
			.concat(mbTOC.slice(2).map(offset => offset.toString(16).padStart(8, '0'))).join('')
			.concat('0'.repeat(98 + mbTOC[0] - mbTOC[1] << 3)).toUpperCase();
		var digest = sha1.digest(tocStr);
		return btoa(String.fromCharCode(...digest)).replace(/\+/g, '.').replace(/\//g, '_').replace(/\=/g, '-');
	  }
	  function mbLookupByDiscID(mbTOC) {
		if (!Array.isArray(mbTOC) || mbTOC.length != mbTOC[1] - mbTOC[0] + 4)
		  return Promise.reject('MusicBrainz: missing or invalid TOC');
		var mbDscId = mbComputeDiscID(mbTOC);
		var params = { toc: mbTOC.join('+') };
		if (media != 'CD') params['media-format'] = 'all';
		return queryMusicBrainzAPI('discid/'.concat(mbDscId || '-'), params).then(function(result) {
		  if (Array.isArray(result.releases) && result.releases.length > 0) var matches = result.releases;
		  if (result.id && result.title) matches = [result];
		  if (!Array.isArray(matches)) return Promise.reject('MusicBrainz: no matches');
		  if (prefs.diag_mode) console.debug('MusicBrainz lookup by discId/TOC successfull:', mbDscId, '/', params, 'matches:', matches);
		  return matches;
		});
	  }
	  function mbLookup() {
		return queryMusicBrainzAPI('release', {
		  query: 'release:"' + release.album + '" AND artist:"' + (isVA ? VA : release.artist) + '"',
		}).then(function(result) {
		  if (result.count <= 0) return Promise.reject('MusicBrainz: no matches');
		  for (i = 0; i < 3; ++i) {
			var f = filter(i);
			if (f.length > 1) return Promise.reject('MusicBrainz: ambiguity');
			if (f.length == 1) break;
		  }
		  if (i >= 3) return Promise.reject('MusicBrainz: no matches');
		  if (prefs.diag_mode && i == 2) console.debug('MusicBrainz fuzzy match:', release, '==', f[0]);
		  return f[0];

		  function filter(level) {
			return result.releases.filter(function(release) {
			  return release.quality != 'low'
				  && (media ? [media] : tracks.some(notRedBook) ? ['WEB'] : ['CD', 'WEB'])
					  .some(_media => release.media.map(media => remapMedia(media.format)).includes(_media)
				  && releasesMatch(release['artist-credit'].map(artist => artist.name), release.title, level));
			});

			function remapMedia(MBmedia) {
			  return [
				['Digital Media', 'WEB'],
			  ].reduce((acc, subst) => acc.toLowerCase() == subst[0].toLowerCase() ? subst[1] : acc, MBmedia);
			}
		  }
		});
	  }
	  function dcLookup() {
		var query = { type: 'release' };
		if (barCode) query.barcode = barCode; else {
		  query.artist = '"' + release.artist + '"';
		  query.release_title = '"' + release.album + '"';
		  //if (release.catalogs.length > 0) query.catno = release.catalogs.join('; ');
		}
		return queryDiscogsAPI('database/search', query).then(function(result) {
		  if (result.results.length <= 0) return Promise.reject('Discogs: no matches');
		  if (barCode) {
			//if (result.results.length > 1) return Promise.reject('Discogs: ambiguity');
			if (prefs.diag_mode) console.debug('Discogs lookup by barcode successfull: ' +
				barCode + '; matches: ' + result.results.length);
			var f = result.results;
		  } else {
			for (i = 0; i < 3; ++i) {
			  f = filter(i);
			  if (f.length > 1) return Promise.reject('Discogs: ambiguity');
			  if (f.length == 1) break;
			}
			if (i >= 3) return Promise.reject('Discogs: no matches');
			if (prefs.diag_mode && i == 2) console.debug('Discogs fuzzy match:', release, '==', f[0]);
		  }
		  return f;

		  function filter(level) {
			return result.results.filter(function(album) {
			  if (media ? Array.isArray(album.format)
				  && !album.format.some(format => dcFmtToGazelle(format) == media)
				 : !album.format.some(format => ['CD', 'WEB'].includes(dcFmtToGazelle(format)))) return false;
			  if (/^(.*?)\s+\(\d+\) - (.*)$/.test(album.title) || !/^(.*?) - (.*)$/.test(album.title))
			  	return releasesMatch(RegExp.$1, RegExp.$2, level);
			  console.warn('Failed to parse Discogs title:', album.title);
			  return false;
			});
		  }
		});
	  }
	  function qbLookup() {
		var params = new URLSearchParams({
		  q: (isVA ? VA : release.artist) + ' ' + release.album,
		  //s: 'rdc', // descending sort by release date
		  i: 'boutique',
		});
		return globalFetch('https://www.qobuz.com/search?' + params).then(function(response) {
		  var results = response.document.querySelectorAll('div.search-results > div.product');
		  if (results.length <= 0) return Promise.reject('Qobuz: no matches');
		  for (i = 0; i < 3; ++i) {
			var f = filter(i);
			if (f.length > 1) return Promise.reject('Qobuz: ambiguity');
			if (f.length == 1) break;
		  }
		  if (i >= 3) return Promise.reject('Qobuz: no matches');
		  if (prefs.diag_mode && i == 2) console.debug('Qobuz fuzzy match:', release, '==', f[0]);
		  return f[0];

		  function filter(level) {
			var _results = [];
			results.forEach(function(result) {
			  var _result = {};
			  _result.artist = result.querySelector('div.artist-name > a');
			  if (_result.artist != null) _result.artist = _result.artist.textContent.trim();
			  _result.title = result.querySelector('div.album-title > a');
			  if (_result.title != null) {
				_result.id = _result.title.pathname.replace(/^.*\//, '');
				_result.href = 'https://www.qobuz.com' + _result.title.pathname;
				_result.title = _result.title.textContent.trim();
			  }
			  _result.imgUrl = result.querySelector('div.album-cover > a > img');
			  if (_result.imgUrl != null) _result.imgUrl = _result.imgUrl.dataset.src || _result.imgUrl.src;
			  if (_result.artist && _result.title && _result.imgUrl
				  && releasesMatch(_result.artist, _result.title, level, 0.75)) _results.push(_result);
			});
			return _results;
		  }
		});
	  }

	  function ruleLink(rule) {
		return ' (<a href="https://redacted.ch/rules.php?p=upload#r' + rule + '" target="_blank">' + rule + '</a>)';
	  }

	  function releasesMatch(remoteArtist, remoteTitle, relaxLevel = 0, minSimilarity = 0.75, minFullSimilarity) {
		if (typeof remoteArtist == 'string') {
		  if (isVA != vaParser.test(remoteArtist)) return false;
		  if (!isVA) remoteArtist = getArtists(remoteArtist)[0];
		} else if (!Array.isArray(remoteArtist)) return false;
		if (!isVA && !artists[0].equalCaselessTo(remoteArtist)
			&& !artists[0].map(name => name.toASCII()).equalCaselessTo(remoteArtist.map(name => name.toASCII())))
		  return false;
		if (!remoteTitle) return true;
		if (typeof remoteTitle != 'string') return false;
		var localTitle = release.album.toLowerCase();
		if (localTitle == (remoteTitle = remoteTitle.toLowerCase())) return true;
		if (editionTitle) var fullLocalTitle = localTitle.concat(' (', editionTitle.toLowerCase(), ')');
		if (fullLocalTitle === remoteTitle) return true;
		if (localTitle.toASCII() == remoteTitle.toASCII()
			|| fullLocalTitle && fullLocalTitle.toASCII() == remoteTitle.toASCII()) return true;
		if (relaxLevel <= 0) return false;
		if ([
		  /[\s\,\.\-\!\(\)\!\?]+/g,
		  /\s+\(([^\(\)]+)\)$/,
		  /\s+\[([^\[\]]+)\]$/,
		  /\s+\{([^\{\}]+)\}$/,
		].reduce(function(acc, rx) {
		  return acc || localTitle.replace(rx, '') == remoteTitle.replace(rx, '')
			|| fullLocalTitle && fullLocalTitle.replace(rx, '') == remoteTitle.replace(rx, '');
		}, false)) return true;
		if (relaxLevel <= 1) return false;
		var similarity = cosineSimilarity(localTitle, remoteTitle);
		if (similarity >= Math.min(minSimilarity, 1)) {
		  if (prefs.diag_mode) console.debug('Cosine similarity accepted: "' + localTitle + '"=="' + remoteTitle + '" (' + similarity+ ')');
		  return true;
		}
		similarity = cosineSimilarity(fullLocalTitle, remoteTitle);
		if (fullLocalTitle && similarity >= Math.min(minFullSimilarity || minSimilarity + 0.05, 1)) {
		  if (prefs.diag_mode) console.debug('Cosine similarity accepted: "' + fullLocalTitle + '"=="' + remoteTitle + '" (' + similarity+ ')');
		  return true;
		}
		if (relaxLevel <= 2) return false;
		if (localTitle.includes(remoteTitle) || remoteTitle.includes(localTitle)) return true;
		return false;
	  }

	  function trackComparer(a, b) {
		var cmp;
		if (release.totaldiscs > 1) {
		  cmp = a.discnumber - b.discnumber;
		  if (!isNaN(cmp) && cmp != 0) return cmp;
		} else {
		  cmp = (a.discsubtitle || '').localeCompare(b.discsubtitle || '');
		  //if (cmp != 0) return cmp;
		}
		cmp = parseInt(a.tracknumber) - parseInt(b.tracknumber);
		if (!isNaN(cmp)) return cmp;
		var m1 = vinyltrackParser.exec(a.tracknumber.toUpperCase());
		var m2 = vinyltrackParser.exec(b.tracknumber.toUpperCase());
		return m1 != null && m2 != null ?
		  m1[1].localeCompare(m2[1]) || parseFloat(m1[2]) - parseFloat(m2[2]) :
		a.tracknumber.toUpperCase().localeCompare(b.tracknumber.toUpperCase());
	  }

	  function reqSelectFormats(...vals) {
		vals.forEach(function(val) {
		  ['MP3', 'FLAC', 'AAC', 'AC3', 'DTS'].forEach(function(fmt, ndx) {
			if (val.toLowerCase() == fmt.toLowerCase() && (ref = document.getElementById('format_' + ndx)) != null) {
			  ref.checked = true;
			  ref.onchange();
			}
		  });
		});
	  }

	  function reqSelectBitrates(...vals) {
		const bitrateSet = !isOPS ? [
		  192, 'APS (VBR)', 'V2 (VBR)', 'V1 (VBR)', 256, 'APX (VBR)',
		  'V0 (VBR)', 320, 'Lossless', '24bit Lossless', 'Other',
		] : [
		  192, 'APS (VBR)', 'V2 (VBR)', 'V1 (VBR)', 256, 'APX (VBR)',
		  'V0 (VBR)', 'q8.x (VBR)', 320, 'Lossless', '24bit Lossless', 'Other',
		];
		vals.forEach(function(val) {
		  var ndx = 10;
		  bitrateSet.forEach((it, _ndx) => { if (val.toString().toLowerCase() == it.toString().toLowerCase()) ndx = _ndx });
		  if ((ref = document.getElementById('bitrate_' + ndx)) != null) {
			ref.checked = true;
			ref.onchange();
		  }
		});
	  }

	  function reqSelectMedias(...vals) {
		const mediaSet = !isOPS ? ['CD', 'DVD', 'Vinyl', 'Soundboard', 'SACD', 'DAT', 'Cassette', 'WEB', 'Blu-Ray']
			: ['CD', 'DVD', 'Vinyl', 'BD', 'Soundboard', 'SACD', 'DAT', 'Cassette', 'WEB'];
		vals.forEach(function(val) {
		  mediaSet.forEach(function(med, ndx) {
			if (val == med && (ref = document.getElementById('media_' + ndx)) != null) {
			  ref.checked = true;
			  ref.onchange();
			}
		  });
		  if (val == 'CD') {
			if ((ref = document.getElementById('needlog')) != null) {
			  ref.checked = true;
			  ref.onchange();
			  if ((ref = document.getElementById('minlogscore')) != null) ref.value = 100;
			}
			if ((ref = document.getElementById('needcue')) != null) ref.checked = true;
			//if ((ref = document.getElementById('needchecksum')) != null) ref.checked = true;
		  }
		});
	  }

	  function getReleaseIndex(str) {
		var ndx;
		[
		  ['Album', 1],
		  ['Soundtrack', 3],
		  ['EP', 5],
		  ['Anthology', 6],
		  ['Compilation', 7],
		  ['Single', 9],
		  ['Live album', 11],
		  ['Remix', 13],
		  ['Bootleg', 14],
		  ['Interview', 15],
		  ['Mixtape', 16],
		  [isOPS ? 'DJ Mix' : 'Demo', 17],
		  ['Concert Recording', 18],
		  ['DJ Mix', 19],
		  ['Unknown', 21],
		].forEach(k => { if (str.toLowerCase() == k[0].toLowerCase()) ndx = k[1] });
		return ndx || 21;
	  }

	  function getChanString(n) {
		if (!n) return null;
		const chanmap = [
		  'mono',
		  'stereo',
		  '2.1',
		  '4.0 surround sound',
		  '5.0 surround sound',
		  '5.1 surround sound',
		  '7.0 surround sound',
		  '7.1 surround sound',
		];
		return n >= 1 && n <= 8 ? chanmap[n - 1] : n + 'chn surround sound';
	  }

	  function fetchOnlineAdditions() {
		if (onlineSource) return Promise.reject('Not offline source');
		var url = sourceUrl || release.urls[0];
		if (!urlParser.test(url)) return Promise.reject('No valid URL to parse');
		if (url.toLowerCase().includes('highresaudio.com/'))
		 	return globalFetch(url).then(response => hraPdfBooklet(response) || Promise.reject('No PDF booklet'));
		return Promise.reject('No online source containing additions');
	  }

	  function processTrackArtists(track) {
		if (!track.artist && Array.isArray(track.artists) && track.artists.length > 0) {
		  track.artist = joinArtists(track.artists);
		  if (Array.isArray(track.featuring_artists) && track.featuring_artists.length > 0)
			track.artist += ' feat. '.concat(joinArtists(track.featuring_artists));
		}
		if (!track.track_artist && Array.isArray(track.track_artists) && track.track_artists.length > 0) {
		  track.track_artist = joinArtists(track.track_artists);
		  if (Array.isArray(track.track_guests) && track.track_guests.length > 0)
			track.track_artist += ' feat. '.concat(joinArtists(track.track_guests));
		}
		['performer', 'remixer', 'composer', 'conductor', 'compiler', 'producer'].forEach(function(role) {
		  var arrPropName = role.concat('s');
		  if (!track[role] && Array.isArray(track[arrPropName]) && track[arrPropName].length > 0)
			track[role] = track[arrPropName].join(', ');
		});
	  }
	} // parseTracks

	function hraPdfBooklet(response) {
	  var ref = response.document.querySelector('form#pdfjs-form-w2[action]');
	  if (ref == null) return undefined;
	  ref = new URLSearchParams(ref.action.replace(/^.*\?/, ''));
	  return '[url='.concat(ref.get('file'), '][img]https://ptpimg.me/ts0fy8.png[/img][/url]');
	}

	function fetchOnline_Music(url, weak = false) {
	  if (!urlParser.test(url)) return Promise.reject('Invalid URL');
	  const discParser = /^(?:CD|DIS[CK]\s+|VOLUME\s+|DISCO\s+|DISQUE\s+)(\d+)(?:\s+of\s+(\d+))?$/i;
	  var ref, artist, album, albumYear, releaseDate, channels, label, composer, bd, sr = 44100, description,
		  compiler, producer, totalTracks, discSubtitle, discNumber, trackNumber, totalDiscs, title, trackArtist,
		  catalogue, encoding, format, bitrate, duration, country, media = 'WEB', imgUrl,
		  genres = [], trs, tracks = [], identifiers = {}, trackIdentifiers = {};
	  if (url.toLowerCase().includes('qobuz.com/')) return globalFetch(url).then(function(response) {
		const error = new Error('Failed to parse Qobus release page');
		identifiers.QOBUZ_ID = response.finalUrl.replace(/^.*\//, '');
		if ((ref = response.document.querySelector('section.album-item[data-gtm]')) != null) try {
		  let gtm = JSON.parse(ref.dataset.gtm);
		  if (gtm.shop.category) genres.push(gtm.shop.category);
		  if (gtm.shop.subCategory && !genres.includes(gtm.shop.subCategory)) genres.push(gtm.shop.subCategory);
		} catch(e) { console.warn(e) }
		if ((ref = response.document.querySelector('div.album-meta > h2.album-meta__artist')) != null)
			artist = ref.title || ref.textContent.trim();
		isVA = vaParser.test(artist);
		album = (ref = response.document.querySelector('div.album-meta > h1.album-meta__title')) != null ?
			ref.title || ref.textContent.trim() : undefined;
		releaseDate = (ref = response.document.querySelector('div.album-meta > ul > li:first-of-type')) != null ?
			normalizeDate(ref.textContent) : undefined;
		var mainArtist = (ref = response.document.querySelector('div.album-meta > ul > li:nth-of-type(2) > a')) != null ?
			ref.title || ref.textContent.trim() : undefined;
		//ref = response.document.querySelector('p.album-about__copyright');
		//if (ref != null) albumYear = extractYear(ref.textContent);
		response.document.querySelectorAll('section#about > ul > li').forEach(function(it) {
		  function matchLabel(lbl) { return it.textContent.trimLeft().startsWith(lbl) }
		  if (/\b(\d+)\s*(?:dis[ck]|disco|disque)/i.test(it.textContent)) totalDiscs = parseInt(RegExp.$1);
		  if (/\b(\d+)\s*(?:track|pist[ae]|tracce|traccia)/i.test(it.textContent)) totalTracks = parseInt(RegExp.$1);
		  if (['Label', 'Etichetta', 'Sello'].some(l => it.textContent.trimLeft().startsWith(l))) {
			label = it.firstElementChild.textContent.replace(/\s+/g, ' ').trim();
		  }
		  else if (['Composer', 'Compositeur', 'Komponist', 'Compositore', 'Compositor'].some(matchLabel)) {
			composer = it.firstElementChild.textContent.trim();
			if (pseudoArtistParsers.some(rx => rx.test(composer))) composer = undefined;
		  } else if (['Genre', 'Genere', 'Género'].some(g => it.textContent.startsWith(g)) && it.childElementCount > 0
				&& genres.length <= 0) {
			genres = Array.from(it.querySelectorAll('a')).map(elem => elem.textContent.trim());
/*
			if (genres.length >= 1 && ['Pop/Rock'].includes(genres[0])) genres.shift();
			if (genres.length >= 2 && ['Alternative & Indie'].includes(genres[genres.length - 1])) genres.shift();
			if (genres.length >= 1 && ['Metal', 'Heavy Metal'].some(genre => genres.includes(genre))) {
			  while (genres.length > 1) genres.shift();
			}
*/
			while (genres.length > 1) genres.shift();
		  }
		});
		bd = 16; channels = 2; // defaults to CD quality
		response.document.querySelectorAll('span.album-quality__info').forEach(function(k) {
		  if (/\b([\d\.\,]+)\s*kHz\b/i.test(k.textContent) != null) sr = parseFloat(RegExp.$1.replace(',', '.')) * 1000;
		  if (/\b(\d+)[\-\s]*Bits?\b/i.test(k.textContent) != null) bd = parseInt(RegExp.$1);
		  if (/\b(?:Stereo)\b/i.test(k.textContent)) channels = 2;
		  if (/\b(\d)\.(\d)\b/.test(k.textContent)) channels = parseInt(RegExp.$1) + parseInt(RegExp.$2);
		});
		getDescription(response, 'section#description > p', true);
		if ((ref = response.document.querySelector('a[title="Qobuzissime"]')) != null) {
		  if (description) description += '\n';
		  description += '[align=center][url=https://www.qobuz.com'
			.concat(ref.pathname, '][img]https://ptpimg.me/4z35uj.png[/img][/url][/align]');
		}
		if ((ref = response.document.querySelector('div.album-cover > img')) != null)
			imgUrl = ref.src.replace(/_\d{3}(?=\.\w+$)/, '_max');
		addTracks(response.document);
		if (totalTracks <= 50) return tracks;
		var query = new URLSearchParams({
		  albumId: identifiers.QOBUZ_ID,
		  offset: 50,
		  limit: 999,
		  store: /\/(\w{2}-\w{2})\/album\//i.test(response.finalUrl) ? RegExp.$1 : 'fr-fr',
		});
		return globalFetch('https://www.qobuz.com/v4/ajax/album/load-tracks?'.concat(query), {
		  headers: { 'X-Requested-With': 'XMLHttpRequest' },
		}).then(response => { addTracks(response.document) }).catch(function(reason) {
		  console.error('globalFetch() failed:', reason);
		  addMessage('long album, only first 50 tracks were captured from Qobuz, which will result in incmplete release description', 'notice');
		}).then(function() { return tracks });

		function addTracks(document) {
		  Array.prototype.push.apply(tracks, Array.from(document.querySelectorAll('div.player__item > div.player__tracks > div.track > div.track__items')).map(function(tr) {
			trackIdentifiers = { TRACK_ID: tr.parentNode.dataset.track };
			var trackGuests = [], trackComposers = [], trackRemixers = [], trackConductors = [], trackProducers = [], trackPerformers = [];
			if ((ref = tr.parentNode.querySelector('p.track__info:first-of-type')) != null) {
			  trackArtist = [];
			  ref.textContent.trim().split(/\s+-\s+/).map(it => it.split(/\s*,\s*/)).forEach(function(it) {
				var roles = it.slice(1);
				if (roles.some(role => /^(?:(?:Main)?Artist)$/.test(role))) trackArtist.pushUnique(it[0]);
				if (roles.some(role => /^(?:FeaturedArtist|)$/.test(role))) trackGuests.pushUnique(it[0]);
				if (roles.some(role => /^(?:AssociatedPerformer)$/.test(role))) trackPerformers.pushUnique(it[0]);
				if (roles.some(role => /^(?:Composer(?:Lyricist)?)$/.test(role))) trackComposers.pushUnique(it[0]);
				if (roles.some(role => /^(?:Conductor)$/.test(role))) trackConductors.pushUnique(it[0]);
				if (roles.some(role => /^(?:Remixer)$/.test(role))) trackRemixers.pushUnique(it[0]);
				if (roles.some(role => /^(?:Producer)$/.test(role))) trackProducers.pushUnique(it[0]);
			  });
			  trackArtist = trackArtist.filter(artist => !trackConductors.includes(artist));
			  trackGuests = trackGuests.filter(artist => ![trackArtist, trackConductors].some(category => category.includes(artist)));
			  trackPerformers = trackPerformers.filter(artist => ![trackArtist, trackConductors, trackGuests].some(category => category.includes(artist)));
			  if ((trackArtist = joinArtists(trackArtist)) && trackGuests.length > 0)
				trackArtist += ' feat. '.concat(joinArtists(trackGuests));
			  if (trackArtist && !isVA && trackArtist == artist) trackArtist = undefined;
			} else trackArtist = undefined;
			if (tr.parentNode.dataset.gtm) try {
			  let gtm = JSON.parse(tr.parentNode.dataset.gtm);
			  if (gtm.product.id) trackIdentifiers.QOBUZ_ID = gtm.product.id;
			  //if (gtm.product.type) trackIdentifiers.RELEASETYPE = gtm.product.type;
			  if (gtm.product.subCategory) var subCategory = [gtm.product.subCategory];
			} catch(e) { console.warn(e) }
			if ((ref = tr.parentNode.parentNode.parentNode.querySelector('p.player__work:first-child')) != null) {
			  discSubtitle = ref.textContent.replace(/\s+/g, ' ').trim();
			  guessDiscNumber();
			}
			return {
			  artist: isVA ? VA : artist,
			  album: album,
			  album_year: albumYear,
			  release_date: releaseDate,
			  label: label,
			  encoding: 'lossless',
			  codec: 'FLAC',
			  bd: bd || undefined,
			  sr: sr || undefined,
			  channels: channels || undefined,
			  media: media,
			  genre: genres.join('; '),
			  discnumber: discNumber || 1,
			  totaldiscs: totalDiscs,
			  discsubtitle: discSubtitle,
			  tracknumber: parseInt(tr.querySelector('span[itemprop="position"]').textContent),
			  totaltracks: totalTracks,
			  title: (tr.querySelector('div.track__item--name > span') || tr.querySelector('span.track__item--name'))
			  	.textContent.trim().replace(/\s+/g, ' '),
			  track_artist: trackArtist,
			  composer: trackComposers.length <= 0 ? composer : undefined,
			  performers: trackPerformers.length > 0 ? trackPerformers : undefined,
			  composers: trackComposers.length > 0 ? trackComposers : undefined,
			  conductors: trackConductors.length > 0 ? trackConductors : undefined,
			  remixers: trackRemixers.length > 0 ? trackRemixers : undefined,
			  producers: trackProducers.length > 0 ? trackProducers : undefined,
			  duration: timeStringToTime(tr.querySelector('span.track__item--duration').textContent),
			  url: response.finalUrl,
			  description: description,
			  identifiers: mergeIds(),
			  cover_url: imgUrl,
			};
		  }));
		}
	  }); else if (url.toLowerCase().includes('highresaudio.com/')) return globalFetch(url).then(function(response) {
		if (/\/album\/view\/(\w+)\//i.test(response.finalUrl)) identifiers.HRA_ID = RegExp.$1;
		artist = (ref = response.document.querySelector('h1 > span.artist')) != null ? ref.textContent.trim() : undefined;
		isVA = vaParser.test(artist);
		album = (ref = response.document.getElementById('h1-album-title')) != null ? ref.firstChild.textContent.trim() : undefined;
		response.document.querySelectorAll('div.album-col-info-data > div > p').forEach(function(k) {
		  var key = k.firstChild.textContent, value = k.lastChild.textContent.trim();
		  if (/\b(?:Genre|Subgenre)\b/i.test(key)) genres.push(value);
		  else if (/\b(?:Label)\b/i.test(key)) label = value;
		  else if (/\b(?:Album[\s\-]Release)\b/i.test(key)) albumYear = extractYear(value);
		  else if (/\b(?:HRA[\s\-]Release)\b/i.test(key)) releaseDate = normalizeDate(value);
		});
		sr = undefined;
		response.document.querySelectorAll('tbody > tr > td.col-format').forEach(function(format) {
		  if (!/^(FLAC)\s*(\d+(?:[\.\,]\d+)?)\b/.test(format.textContent)) return;
		  if (sr) sr = NaN;
		  if (sr != undefined) return;
		  format = RegExp.$1;
		  sr = parseFloat(RegExp.$2.replace(',', '.')) * 1000;
		});
		getDescription(response, 'div#albumtab-info > p', false);
		if (i = hraPdfBooklet(response)) {
		  if (description) description += '\n\n'.concat(i); else description = i;
		}
		url = (ref = response.document.querySelector('meta[property="og:url"][content]')) != null && ref.content;
		if ((ref = response.document.querySelector('meta[property="og:image"][content]')) != null) imgUrl = ref.content;
		trs = response.document.querySelectorAll('ul.playlist > li.pltrack');
		return Array.from(trs).map(function(tr) {
		  discNumber = undefined; discSubtitle = tr;
		  while ((discSubtitle = discSubtitle.previousElementSibling) != null) {
			if (discSubtitle.nodeName == 'LI' && discSubtitle.className == 'plinfo') {
			  discSubtitle = discSubtitle.textContent.replace(/\s*:$/, '').trim();
			  guessDiscNumber();
			  break;
			}
		  }
		  return {
			artist: isVA ? VA : artist,
			album: album,
			album_year: albumYear,
			release_date: releaseDate,
			label: label,
			encoding: 'lossless',
			codec: 'FLAC',
			bd: 24,
			sr: sr || undefined,
			media: media,
			genre: genres.join('; '),
			discnumber: discNumber,
			totaldiscs: totalDiscs,
			discsubtitle: discSubtitle || undefined,
			tracknumber: parseInt(tr.querySelector('span.track').textContent) || tr.querySelector('span.track').textContent,
			totaltracks: trs.length,
			title: tr.querySelector('span.title').textContent.trim().replace(/\s+/g, ' '),
			duration: timeStringToTime(tr.querySelector('span.time').textContent),
			url: url || response.finalUrl,
			description: description,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  };
		});
	  }); else if (url.toLowerCase().includes('bandcamp.com/')) return globalFetch(url).then(function(response) {
		artist = Array.from(response.document.querySelectorAll('span[itemprop="byArtist"] > a')).map(a => a.textContent.trim());
		ref = response.document.querySelector('span.back-link-text > br');
		if (ref != null && ref.nextSibling != null) label = ref.nextSibling.textContent.trim(); else {
		  ref = response.document.querySelector('p#band-name-location > span.title');
		  if (ref != null) label = ref.textContent.trim();
		}
		var tags = new TagManager;
		response.document.querySelectorAll('div.tralbum-tags > a.tag').forEach(function(tag) {
		  if (!artist.some(artist => tag.textContent.trim().toLowerCase() == artist.toLowerCase())) tags.add(tag.textContent.trim());
		});
		if ((ref = response.document.querySelector('div#tralbumArt > a.popupImage')) != null) imgUrl = ref.href;
		try {
		  response.document.querySelectorAll('div#propOpenWrapper > div[id] > script').forEach(function(script) {
			if (!/\b(var\s+SiteData\s*=\s*\{[\S\s]+\};)/.test(script.text)) return;
			eval(RegExp.$1);
			if (typeof TralbumData != 'object') return;
			if (prefs.diag_mode) console.debug('BandCamp metadata loaded:', TralbumData);
			identifiers.BANDCAMP_ID = TralbumData.id;
			identifiers.RELEASETYPE = TralbumData.item_type;
			identifiers.BARCODE = TralbumData.current.upc/* || TralbumData.packages[0].upc*/;
			isVA = vaParser.test(TralbumData.artist);
			description = TralbumData.current.about;
			if (TralbumData.current.credits) if (description) description += '\n\n'.concat(TralbumData.current.credits);
				else description = TralbumData.current.credits;
			tracks = TralbumData.trackinfo.map(function(track) {
			  trackIdentifiers = {
				TRACK_ID: track.track_id,
				//HASLYRICS: Number(track.has_lyrics) || 0,
			  };
			  return {
				artist: isVA ? VA : TralbumData.artist,
				album: TralbumData.current.title,
				release_date: TralbumData.current.release_date || TralbumData.album_release_date,
				description: description,
				label: /*TralbumData.packages[0].label || */label,
				//catalog: TralbumData.packages[0].sku,
				genre: tags.toString(),
				duration: track.duration || undefined,
				lyrics: track.lyrics || undefined,
				title: track.title,
				tracknumber: track.track_num,
				totaltracks: TralbumData.trackinfo.length,
				media: 'WEB',
				url: TralbumData.url ? TralbumData.url.replace(/^http\b/, 'https') : response.finalUrl,
				cover_url: imgUrl,
				identifiers: mergeIds(),
			  };
			});
		  });
		  if (tracks.length <= 0) throw 'No tracks found';
		  return tracks;
		} catch(e) {
		  console.warn('BandCamp: falling back to HTML scraper for the reason:', e);
		  isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		  if ((ref = response.document.querySelector('h2[itemprop="name"]')) != null) album = ref.textContent.trim();
		  ref = response.document.querySelector('div.tralbum-credits');
		  if (ref != null && /\brelease[ds]\s+(.*?\b\d{4})\b/i.test(ref.textContent)) releaseDate = RegExp.$1;
		  description = [];
		  response.document.querySelectorAll('div.tralbumData').forEach(function(div) {
			if (!div.classList.contains('tralbum-tags')) description.push(html2php(div, response.finalUrl).trim());
		  });
		  description = description.filter(p => p).join('\n\n');
		  if (/\bShare\.initPanel\s*\(\s*\w+\s*,\s*\d+\s*,\s*"https?(:\/\/\S+?)"\s*\);/i.test(response.responseText))
			  var shareLink = 'https'.concat(RegExp.$1);
		  trs = response.document.querySelectorAll('table.track_list > tbody > tr[itemprop="tracks"]');
		  return Array.from(trs).map(tr => ({
			artist: isVA ? VA : undefined,
			artists: !isVA ? artist : undefined,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			media: media,
			genre: tags.toString(),
			discnumber: discNumber,
			totaldiscs: totalDiscs,
			tracknumber: parseInt(tr.querySelector('div.track_number').textContent) || tr.querySelector('div.track_number').textContent,
			totaltracks: trs.length,
			title: (tr.querySelector('div.title span.track-title')
			  || tr.querySelector('div.title span[itemprop="name"]')).textContent.trim().replace(/\s+/g, ' '),
			duration: durationFromMeta(tr) || (ref = tr.querySelector('span.time')) != null && timeStringToTime(ref.textContent) || undefined,
			url: shareLink || response.finalUrl,
			description: description,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  }));
		}
	  }); else if (url.toLowerCase().includes('prestomusic.com/')) return globalFetch(url).then(function(response) {
		const parenthesesStripper = /\s+\([^\(\)]*\)/g;
		identifiers.COMPOSEREMPHASIS = 1;
		artist = getArtists(response.document.querySelectorAll('div.c-product-block__contributors > p'));
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		ref = response.document.querySelector('h1.c-product-block__title');
		if (ref != null) album = ref.lastChild.wholeText.trim();
		response.document.querySelectorAll('div.c-product-block__metadata > ul > li').forEach(function(li) {
		  if (li.firstChild.textContent.includes('Release Date')) {
			releaseDate = li.lastChild.wholeText;
			if (/\b(\d+)\w*\s+(\w+)\s+(\d{4})\b/.test(releaseDate)) releaseDate = RegExp.$2 + ' ' + RegExp.$1 + ' ' + RegExp.$3;
		  } else if (li.firstChild.textContent.includes('Label')) {
			label = li.lastChild.wholeText.trim();
		  } else if (li.firstChild.textContent.includes('Catalogue No')) {
			catalogue = li.lastChild.wholeText.trim();
		  }
		});
		composer = [];
		response.document.querySelectorAll('div#related > div > ul > li').forEach(function(li) {
		  if (li.parentNode.previousElementSibling.textContent.includes('Composers')) {
			var _composer = li.firstChild.textContent.trim().replace(parenthesesStripper, '');
			composer.push(_composer.replace(/^(.*?)\s*,\s+(.*)$/, '$2 $1'));
		  }
		});
		genres = undefined;
		if (/\/jazz\//i.test(response.finalUrl)) genres = 'Jazz';
		if (/\/classical\//i.test(response.finalUrl)) genres = 'Classical';
		getDescription(response, 'div#about > div > p', true);
		var personnel = [];
		response.document.querySelectorAll('div.c-product-block__contributors > p').forEach(function(p) {
		  // TODO
		});
		if (personnel.length > 0) {
		  if (description) description += '\n\n';
		  description += personnel.join('\n');
		}
		ref = response.document.querySelector('div.c-product-block__aside > a');
		if (ref != null) imgUrl = ref.href.replace(/\?\d+$/, '');
		trs = response.document.querySelectorAll('div.has--sample');
		trackNumber = 0;
		return Array.from(trs).map(function(tr) {
		  discNumber = discSubtitle = undefined;
		  var parent = tr;
		  if (tr.classList.contains('c-track')) {
			parent = tr.parentNode.parentNode;
			if (parent.classList.contains('c-expander')) parent = parent.parentNode;
			if ((ref = parent.querySelector(':scope > div > div > div > p.c-track__title')) != null) {
			  discSubtitle = ref.textContent.trim().replace(/\s+/g, ' ');
			  guessDiscNumber();
			}
		  }
		  trackArtist = getArtists(parent.querySelectorAll(':scope > div.c-track__details > ul > li'));
		  return {
			artist: isVA ? VA : undefined,
			artists: !isVA ? artist : undefined,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			catalog: catalogue,
			media: media,
			genre: genres,
			discnumber: discNumber,
			totaldiscs: totalDiscs,
			discsubtitle: discSubtitle,
			tracknumber: ++trackNumber,
			totaltracks: trs.length,
			title: (ref = tr.querySelector('p.c-track__title')) ? ref.textContent.trim().replace(/\s+/g, ' ') : undefined,
			track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
			composers: composer,
			duration: timeStringToTime(tr.querySelector('div.c-track__duration').textContent),
			description: description,
			url: response.finalUrl,
			cover_url: imgUrl,
			identifiers: mergeIds(),
		  };
		});

		function getArtists(nodeList) {
		  var artists = [];
		  nodeList.forEach(function(_artists) {
			_artists = _artists.textContent.trim();
			if (_artists.startsWith('Record')) return;
			Array.prototype.push.apply(artists,
				splitArtists(_artists.replace(parenthesesStripper, '')).filter(artist => artist.length > 0));
		  });
		  return artists.filter(artist => artist.length > 0);
		}
	  }); else if (url.toLowerCase().includes('discogs.com/') && /\/(release|master|artist)s?\/(\d+)\b/i.test(url)) {
		if (RegExp.$1.toLowerCase() == 'artist') return Promise.reject('Discogs artists not parseable');
		if (RegExp.$1.toLowerCase() == 'master') return Promise.reject('Discogs masters as source aren\'t supported, pick a specific release');
		return queryDiscogsAPI('releases/' + RegExp.$2).then(function(release) {
		  if (prefs.diag_mode) console.debug('Discogs release', release.id, 'metadata received:', release);
		  const removeArtistNdx = /\s*\(\d+\)$/;
		  const editionTest = /^(?:.+?\s+Edition|Remaster(?:ed)|Remasterizado|Remasterisée|Reissue|.+?\s+Release|Enhanced|Promo)$/;
		  media = undefined;
		  identifiers.DISCOGS_ID = release.id;
		  var master = release.master_id ? queryDiscogsAPI('masters/' + release.master_id).then(function(master) {
			if (prefs.diag_mode) console.debug('Discogs master', master.id, 'metadata received:', master);
			return master;
		  }) : Promise.reject('master release not available');
		  var albumArtists = getArtists(release);
		  if (albumArtists[0].length > 0) {
			artist = albumArtists[0].join(', ');
			if (albumArtists[1].length > 0) artist += ' feat. ' + albumArtists[1].join(', ');
			isVA = albumArtists[0].length == 1 && vaParser.test(albumArtists[0][0]);
		  } else isVA = true;
		  album = release.title;
		  var editions = [];
		  label = []; catalogue = [];
		  release.labels.forEach(function(it) {
			if (it.entity_type != 1) return;
			if (it.name && !/^Not\s+On\s+Label\b/i.test(it.name)) label.pushUniqueCaseless(it.name.replace(removeArtistNdx, ''));
			if (it.catno) catalogue.pushUniqueCaseless(it.catno);
		  });
		  description = '';
		  if (release.companies && release.companies.length > 0) {
			description = '[b]Companies, etc.[/b]\n';
			let type_names = new Set(release.companies.map(it => it.entity_type_name));
			type_names.forEach(function(type_name) {
			  description += '\n' + type_name + ' – ' + release.companies
				.filter(it => it.entity_type_name == type_name)
				.map(function(it) {
				  var result = '[url=' + discogsOrigin + '/label/' + it.id + ']' +
					  it.name.replace(removeArtistNdx, '') + '[/url]';
				  if (it.catno) result += ' – ' + it.catno;
				  return result;
				})
				.join(', ');
			});
		  }
		  if (release.extraartists && release.extraartists.length > 0) {
			if (description) description += '\n\n';
			description += '[b]Credits[/b]\n';
			let roles = new Set(release.extraartists.map(it => it.role));
			roles.forEach(function(role) {
			  description += '\n' + role + ' – ' + release.extraartists
				.filter(artist => artist.role == role)
				.map(function(artist) {
				  var result = '[url=' + discogsOrigin + '/artist/' + artist.id + ']' +
					  (artist.anv || artist.name).replace(removeArtistNdx, '') + '[/url]';
				  if (artist.tracks) result += ' (tracks: ' + artist.tracks + ')';
				  return result;
				})
				.join(', ');
			});
		  }
		  if (release.notes) {
			if (description) description += '\n\n';
			description += '[b]Notes[/b]\n\n' + release.notes.trim();
		  }
		  if (Array.isArray(release.identifiers) && release.identifiers.length > 0) {
			if (description) description += '\n\n';
			description += '[b]Barcode and Other Identifiers[/b]\n';
			release.identifiers.forEach(function(it) {
			  description += '\n' + it.type;
			  if (it.description) description += ' (' + it.description + ')';
			  description += ': ' + it.value;
			});
		  }
		  [
			['Single', 'Single'],
			['EP', 'EP'],
			['Compilation', 'Compilation'],
			['Soundtrack', 'Soundtrack'],
		  ].forEach(function(k) {
			if (release.formats.every(it => Array.isArray(it.descriptions) && it.descriptions.includesCaseless(k[0]))) {
			  identifiers.RELEASETYPE = k[1];
			}
		  });
		  release.identifiers.forEach(function(id) {
			identifiers[id.type.toUpperCase().replace(/\s*\/\s*/g, '-').replace(/\W/g, '_')] = id.value;
		  });
		  release.formats.forEach(function(fmt) {
			if (editionTest.test(fmt.text)) editions.push(fmt.text);
			if (Array.isArray(fmt.descriptions)) fmt.descriptions.forEach(function(desc) {
			  if (editionTest.test(desc)) editions.push(desc);
			});
			if (media) return;
			if (/\bFile\b/.test(fmt.name)) {
			  media = 'WEB';
			  if (['FLAC', 'WAV', 'AIF', 'AIFF', 'AIFC', 'PCM', 'ALAC', 'APE', 'WavPack']
				  .some(k => fmt.descriptions.includes(k))) {
				encoding = 'lossless'; format = 'FLAC';
			  } else if (fmt.descriptions.includes('AAC')) {
				encoding = 'lossy'; format = 'AAC'; bd = undefined;
				if (/(\d+)\s*kbps\b/i.test(fmt.text)) bitrate = parseInt(RegExp.$1);
			  } else if (fmt.descriptions.includes('MP3')) {
				encoding = 'lossy'; format = 'MP3'; bd = undefined;
				if (/\b(\d+)\s*kbps\b/i.test(fmt.text)) bitrate = parseInt(RegExp.$1);
			  } else if (['DFF', 'DSD'].some(k => fmt.descriptions.includes(k))) {
				encoding = 'lossless';
			  } else if (['AMR', 'MP2', 'ogg-vorbis', 'Opus', 'SHN', 'WMA'].some(k => fmt.descriptions.includes(k))) {
				encoding = 'lossy';
			  }
			} else media = dcFmtToGazelle(fmt.name) || undefined;
		  });
		  if (editions.length > 0) album += ' (' + editions.join(' / ') + ')';
		  totalTracks = release.tracklist.filter(track => track.type_.toLowerCase() == 'track').length;
		  return master.catch(function(reason) {
			console.debug('Discogs master not received:', reason);
			if (prefs.messages_verbosity >= 1) addMessage(reason, 'notice');
		  }).then(function(master) {
			var tags = new TagManager();
			if (release.genres) tags.add(...release.genres);
			if (release.styles) tags.add(...release.styles);
			if (master) {
			  if (master.genres) tags.add(...master.genres);
			  if (master.styles) tags.add(...master.styles);
			}
			const imageFilter = image => urlParser.test(image.resource_url || image.uri) && ['primary', 'front'].includes(image.type);
			imgUrl = master && Array.isArray(master.images) ? master.images.filter(imageFilter) : [];
			if (imgUrl.length <= 0 && Array.isArray(release.images)) imgUrl = release.images.filter(imageFilter);
			release.tracklist.forEach(function(track) {
			  switch (track.type_.toLowerCase()) {
				case 'heading':
				  discSubtitle = track.title;
				  break;
				case 'track': {
				  trackIdentifiers = {};
				  if (/^([a-zA-Z]+)?(\d+)-(\w+)$/.test(track.position)) {
					if (RegExp.$1) trackIdentifiers.VOL_MEDIA = RegExp.$1;
					discNumber = RegExp.$2;
					trackNumber = RegExp.$3;
				  } else {
					discNumber = undefined;
					trackNumber = track.position;
				  }
				  let trackArtists = getArtists(track);
				  if (trackArtists[0].length > 0 && !trackArtists[0].equalCaselessTo(albumArtists[0])
					  || trackArtists[1].length > 0 && !trackArtists[1].equalCaselessTo(albumArtists[1])) {
					trackArtist = (trackArtists[0].length > 0 ? trackArtists : albumArtists)[0].join(', ');
					if (trackArtists[1].length > 0) trackArtist += ' feat. ' + trackArtists[1].join(', ');
				  } else trackArtist = undefined;
				  let performer = Array.isArray(track.extraartists) && track.extraartists
					.map(artist => (artist.anv || artist.name).replace(removeArtistNdx, ''))
					.filter(function(artist) {
					  return !albumArtists.slice(2).some(it => Array.isArray(it) && it.includes(artist))
					  	&& !trackArtists.slice(2).some(it => Array.isArray(it) && it.includes(artist))
					});
				  tracks.push({
					artist: isVA ? VA : artist,
					//artists: !isVA ? albumArtists[0] : undefined,
					album: album,
					album_year: master ? master.year : undefined,
					release_date: release.released,
					label: label.join(' / '),
					catalog: catalogue.join(' / '),
					country: release.country,
					encoding: encoding,
					codec: format,
					bitrate: bitrate,
					bd: bd,
					media: media,
					genre: tags.toString(),
					discnumber: discNumber,
					totaldiscs: release.format_quantity,
					discsubtitle: discSubtitle,
					tracknumber: trackNumber,
					totaltracks: totalTracks,
					title: track.title,
					track_artist: trackArtist,
					performer: Array.isArray(performer) && performer.join('; ') || undefined,
					composers: stringyfyRole(3),
					conductors: stringyfyRole(4),
					remixers: stringyfyRole(2),
					compilers: stringyfyRole(5),
					producers: stringyfyRole(6),
					duration: timeStringToTime(track.duration),
					description: description,
					identifiers: mergeIds(),
					cover_url: imgUrl.length > 0 ? imgUrl[0].resource_url || imgUrl[0].uri : undefined,
				  });

				  function stringyfyRole(ndx) {
					var arr = (Array.isArray(trackArtists[ndx]) && trackArtists[ndx].length > 0 ? trackArtists : albumArtists)[ndx];
					return arr.length > 0 ? arr : undefined;
				  }
				}
			  }
			});
			return tracks;
		  });

		  function getArtists(root) {
			function filterArtists(rx, anv = true) {
			  return Array.isArray(root.extraartists) && rx instanceof RegExp ?
				root.extraartists.filter(it => rx.test(it.role))
				  .map(it => (anv && it.anv || it.name || '').replace(removeArtistNdx, '')) : [];
			}
			var artists = [];
			for (var ndx = 0; ndx < 7; ++ndx) artists[ndx] = [];
			ndx = 0;
			if (root.artists) root.artists.forEach(function(it) {
			  artists[ndx].push((it.anv || it.name).replace(removeArtistNdx, ''));
			  if (/^feat/i.test(it.join)) ndx = 1;
			});
			return [
			  artists[0],
			  artists[1].concat(filterArtists(/^(?:featuring)$/i)),
			  artists[2].concat(filterArtists(/\b(?:Remixed[\s\-]By|Remixer)\b/i)),
			  artists[3].concat(filterArtists(/\b(?:(?:Written|Composed)[\s\-]By|Composer)\b/i, false)),
			  artists[4].concat(filterArtists(/\b(?:Conducted[\s\-]By|Conductor)\b/i)),
			  artists[5].concat(filterArtists(/\b(?:Compiled[\s\-]By|Compiler)\b/i)),
			  artists[6].concat(filterArtists(/\b(?:Produced[\s\-]By|Producer)\b/i)),
			  // filter off from performers
			  filterArtists(/\b(?:(?:Mixed)[\s\-]By|Mixer)\b/i),
			  filterArtists(/\b(?:(?:Written|Composed)[\s\-]By|Composer)\b/i, true),
			];
		  }
		});
	  } else if (url.toLowerCase().includes('supraphonline.cz/')) return globalFetch(url.replace(/\?.*$/, '')).then(function(response) {
		const copyrightParser = /^(?:\([PC]\)|℗|©)$/i;
		var ndx, conductor = [], origin = new URL(response.finalUrl).origin;
		genres = undefined; artist = [];
		response.document.querySelectorAll('h2.album-artist > a').forEach(function(it) {
		  artist.pushUnique(it.title);
		});
		if (artist.length == 0 && (ref = response.document.querySelector('h2.album-artist[title]')) != null) {
		  isVA = vaParser.test(ref.title);
		}
		ref = response.document.querySelector('span[itemprop="byArtist"] > meta[itemprop="name"]');
		if (ref != null && vaParser.test(ref.content)) isVA = true;
		if (isVA) artist = [];
		if ((ref = response.document.querySelector('h1[itemprop="name"]')) != null) album = ref.firstChild.data.trim();
		if ((ref = response.document.querySelector('meta[itemprop="numTracks"]')) != null) totalTracks = parseInt(ref.content);
		if ((ref = response.document.querySelector('meta[itemprop="genre"]')) != null) genres = ref.content;
		if ((ref = response.document.querySelector('li.album-version > div.selected > div')) != null) {
		  if (/\b(?:MP3)\b/.test(ref.textContent)) { media = 'WEB'; encoding = 'lossy'; format = 'MP3'; }
		  if (/\b(?:FLAC)\b/.test(ref.textContent)) { media = 'WEB'; encoding = 'lossless'; format = 'FLAC'; bd = 16; }
		  if (/\b(?:Hi[\s\-]*Res)\b/.test(ref.textContent)) { media = 'WEB'; encoding = 'lossless'; format = 'FLAC'; bd = 24; }
		  if (/\b(?:CD)\b/.test(ref.textContent)) { media = 'CD'; }
		  if (/\b(?:LP)\b/.test(ref.textContent)) { media = 'Vinyl'; }
		}
		response.document.querySelectorAll('ul.summary > li').forEach(function(it) {
		  if (it.childElementCount <= 0) return;
		  if (it.firstElementChild.textContent.includes('Nosič')) media = it.lastChild.textContent.trim();
		  if (it.firstElementChild.textContent.includes('Datum vydání')) releaseDate = normalizeDate(it.lastChild.textContent);
		  if (it.firstElementChild.textContent.includes('První vydání')) albumYear = extractYear(it.lastChild.data);
		  //if (it.firstElementChild.textContent.includes('Žánr')) genre = it.lastChild.textContent.trim();
		  if (it.firstElementChild.textContent.includes('Vydavatel')) label = it.lastChild.textContent.trim();
		  if (it.firstElementChild.textContent.includes('Katalogové číslo')) catalogue = it.lastChild.textContent.trim();
		  if (it.firstElementChild.textContent.includes('Formát')) {
			if (/\b(?:FLAC|WAV|AIFF?)\b/.test(it.lastChild.textContent)) { encoding = 'lossless'; format = 'FLAC'; }
			if (/\b(\d+)[\-\s]?bits?\b/i.test(it.lastChild.textContent)) bd = parseInt(RegExp.$1);
			if (/\b([\d\.\,]+)[\-\s]?kHz\b/.test(it.lastChild.textContent)) sr = parseFloat(RegExp.$1.replace(',', '.')) * 1000;
		  }
		  if (it.firstElementChild.textContent.includes('Celková stopáž')) totalTime = timeStringToTime(it.lastChild.textContent.trim());
		  if (copyrightParser.test(it.firstElementChild.textContent) && !albumYear) albumYear = extractYear(it.lastChild.data);
		});
		const creators = ['autoři', 'interpreti', 'tělesa', 'digitalizace'];
		artists = [];
		for (i = 0; i < 4; ++i) artists[i] = {};
		response.document.querySelectorAll('ul.sidebar-artist > li').forEach(function(it) {
		  if ((ref = it.querySelector('h3')) != null) {
			ndx = undefined;
			creators.forEach((it, _ndx) => { if (ref.textContent.includes(it)) ndx = _ndx });
		  } else {
			if (typeof ndx != 'number') return;
			let role;
			if (ndx == 2) role = 'ensemble';
			  else if ((ref = it.querySelector('span')) != null) role = translateRole(ref);
			if ((ref = it.querySelector('a')) != null) {
			  if (!Array.isArray(artists[ndx][role])) artists[ndx][role] = [];
			  var href = new URL(ref.href);
			  artists[ndx][role].pushUnique([ref.textContent.trim(), origin + href.pathname]);
			}
		  }
		});
		getDescription(response, 'div[itemprop="description"] p', true);
		composer = [];
		var performers = [], DJs = [];
		function dumpArtist(ndx, role) {
		  if (!role || role == 'undefined') return;
		  if (description.length > 0) description += '\n' ;
		  description += '[color=#9576b1]' + role + '[/color] – ';
		  //description += artists[ndx][role].map(artist => '[artist]' + artist[0] + '[/artist]').join(', ');
		  description += artists[ndx][role].map(artist => '[url=' + artist[1] + ']' + artist[0] + '[/url]').join(', ');
		}
		for (i = 1; i < 3; ++i) Object.keys(artists[i]).forEach(function(role) { // performers
		  var a = artists[i][role].map(a => a[0]);
		  artist.pushUnique(...a);
		  (['conductor', 'choirmaster'].includes(role) ? conductor : role == 'DJ' ? DJs : performers).pushUnique(...a);
		  if (i != 2) dumpArtist(i, role);
		});
		Object.keys(artists[0]).forEach(function(role) { // composers
		  composer.pushUnique(...artists[0][role].map(it => it[0])
			  .filter(it => !pseudoArtistParsers.some(rx => rx.test(it))));
		  dumpArtist(0, role);
		});
		Object.keys(artists[3]).forEach(role => { dumpArtist(3, role) }); // ADC & mastering
		ref = response.document.querySelector('meta[itemprop="image"]');
		if (ref != null)imgUrl = ref.content.replace(/\?.*$/, '');
		var promises = [];
		response.document.querySelectorAll('table.table-tracklist > tbody > tr').forEach(function(row) {
		  promises.push(row.id && (ref = row.querySelector('td > a.trackdetail')) != null ?
			 globalFetch(origin + ref.pathname + ref.search, { context: parseInt(row.id.replace(/^track-/i, '')) })
				.then(function(response) {
				  var track = response.document.getElementById('track-' + response.context);
				  if (track == null) return Promise.reject('Track detail not located');
				  return [track, response.document.querySelector('div[data-swap="trackdetail-' +
					response.context + '"] > div > div.row')];
				})
			: Promise.resolve([row, null]));
		});
		return Promise.all(promises).then(function(rows) {
		  rows.forEach(function(tr) {
			if (!(tr[0] instanceof HTMLElement)) throw new Error('Assertion failed: tr[0] != HTMLElement');
			if (tr[0].id && tr[0].classList.contains('track')) {
			  tr[2] = [];
			  for (i = 0; i < 8; ++i) tr[2][i] = [];
			  if (!(tr[1] instanceof HTMLElement)) return;
			  tr[1].querySelectorAll('div[class]:nth-of-type(2) > ul > li > span').forEach(function(li) {
				function oneOf(...arr) { return arr.some(role => key == role) }
				var key = translateRole(li);
				var val = li.nextElementSibling.textContent.trim();
				if (pseudoArtistParsers.some(rx => rx.test(val))) return;
				if (key.startsWith('remix')) {
				  tr[2][2].pushUnique(val);
				} else if (oneOf('music', 'lyrics', 'music+lyrics', 'original lyrics', 'czech lyrics', 'libreto', 'music improvisation', 'author')) {
				  tr[2][3].pushUnique(val);
				} else if (oneOf('conductor', 'choirmaster')) {
				  tr[2][4].pushUnique(val);
				} else if (key == 'DJ') {
				  tr[2][5].pushUnique(val);
				} else if (key == 'produced by') {
				  tr[2][6].pushUnique(val);
				} else if (key == 'recorded by') {
				} else {
				  tr[2][7].pushUnique(val);
				}
			  });
			}
		  });
		  var guests = rows.filter(tr => tr.length >= 3).map(it => it[2][7])
			  .reduce((acc, trpf) => trpf.filter(trpf => acc.includes(trpf)))
			  .filter(it => !artist.includes(it));
		  rows.forEach(function(tr) {
			if (tr[0].classList.contains('cd-header')) {
			  discNumber = /\b\d+\b/.test(tr[0].querySelector('h3').firstChild.data.trim())
				  && parseInt(RegExp.lastMatch) || undefined;
			}
			if (tr[0].classList.contains('song-header')) discSubtitle = tr[0].firstElementChild.title.trim() || undefined;
			if (tr[0].id && tr[0].classList.contains('track')) {
			  var copyright, trackGenre, trackYear, recordPlace, recordDate, trackIdentifiers = {};
			  if (/^track-(\d+)$/i.test(tr[0].id)) trackIdentifiers.TRACK_ID = RegExp.$1;
			  if (tr[1] instanceof HTMLElement) {
				tr[1].querySelectorAll('div[class]:nth-of-type(1) > ul > li > span').forEach(function(li) {
				  if (li.textContent.startsWith('Nahrávka dokončena')) {
					trackIdentifiers.RECYEAR = extractYear(recordDate = li.nextSibling.data.trim());
				  }
				  if (li.textContent.startsWith('Místo nahrání')) {
					recordPlace = li.nextSibling.data.trim();
				  }
				  if (li.textContent.startsWith('Rok prvního vydání')) {
					trackIdentifiers.PUBYEAR = (trackYear = parseInt(li.nextSibling.data));
				  }
				  //if (copyrightParser.test(li.textContent)) copyright = li.nextSibling.data.trim();
				  if (li.textContent.startsWith('Žánr')) trackGenre = li.nextSibling.data.trim();
				});
			  }
			  tracks.push({
				artist: isVA ? VA : undefined,
				artists: !isVA ? artist : undefined,
				album: album,
				album_year: /*trackYear || */albumYear || undefined,
				release_date: releaseDate,
				label: label,
				catalog: catalogue,
				encoding: encoding,
				codec: format,
				bd: bd,
				sr: sr || undefined,
				media: media,
				genre: translateGenre(genres) + ' | ' + translateGenre(trackGenre),
				discnumber: discNumber,
				totaldiscs: totalDiscs,
				discsubtitle: discSubtitle,
				tracknumber: /^\s*(\d+)\.?\s*$/.test(tr[0].firstElementChild.firstChild.textContent) ?
					parseInt(RegExp.$1) || RegExp.$1 : undefined,
				totaltracks: totalTracks,
				title: tr[0].querySelector('meta[itemprop="name"]').content,
				track_artists: tr[2][0].length > 0 && (isVA || !tr[2][0].equalCaselessTo(artist)) ? tr[2][0] : undefined,
				performers: tr[2][7].length > 0 ? tr[2][7] : performers,
				composers: tr[2][3].length > 0 ? tr[2][3] : composer,
				conductors: tr[2][4].length > 0 ? tr[2][4] : conductor,
				remixers: tr[2][2],
				compilers: tr[2][5].length > 0 ? tr[2][5] : DJs,
				producers: tr[2][6],
				duration: durationFromMeta(tr[0]),
				url: response.finalUrl,
				description: description,
				identifiers: mergeIds(),
				cover_url: imgUrl,
			  });
			}
		  });
		  return tracks;
		});

		function translateGenre(genre) {
		  if (!genre || typeof genre != 'string') return undefined;
		  [
			['Orchestrální hudba', 'Orchestral Music'],
			['Komorní hudba', 'Chamber Music'],
			['Vokální', 'Classical, Vocal'],
			['Klasická hudba', 'Classical'],
			['Melodram', 'Classical, Melodram'],
			['Symfonie', 'Symphony'],
			['Vánoční hudba', 'Christmas Music'],
			[/^(?:Alternativ(?:ní|a))$/i, 'Alternative'],
			['Dechová hudba', 'Brass Music'],
			['Elektronika', 'Electronic'],
			['Folklor', 'Folclore, World Music'],
			['Instrumentální hudba', 'Instrumental'],
			['Latinské rytmy', 'Latin'],
			['Meditační hudba', 'Meditative'],
			['Vojenská hudba', 'Military Music'],
			['Pro děti', 'Children'],
			['Pro dospělé', 'Adult'],
			['Mluvené slovo', 'Spoken Word'],
			['Audiokniha', 'audiobook'],
			['Humor', 'humour'],
			['Pohádka', 'Fairy-Tale'],
		  ].forEach(function(subst) {
			if (typeof subst[0] == 'string' && genre.toLowerCase() == subst[0].toLowerCase()
			   || subst[0] instanceof RegExp && subst[0].test(genre)) genre = subst[1];
		  });
		  return genre;
		}
		function translateRole(elem) {
		  if (!(elem instanceof HTMLElement)) return undefined;
		  var role = elem.textContent.trim().toLowerCase().replace(/\s*:.*$/, '');
		  [
			[/\b(?:klavír)\b/, 'piano'],
			[/\b(?:housle)\b/, 'violin'],
			[/\b(?:varhany)\b/, 'organ'],
			[/\b(?:cembalo)\b/, 'harpsichord'],
			[/\b(?:trubka)\b/, 'trumpet'],
			[/\b(?:soprán)\b/, 'soprano'],
			[/\b(?:alt)\b/, 'alto'],
			[/\b(?:baryton)\b/, 'baritone'],
			[/\b(?:bas)\b/, 'basso'],
			[/\b(?:syntezátor)\b/, 'synthesizer'],
			[/\b(?:zpěv)\b/, 'vocals'],
			[/^(?:čte|četba)$/, 'narration'],
			['vypravuje', 'narration'],
			['komentář', 'commentary'],
			['hovoří', 'spoken by'],
			['hovoří a zpívá', 'speaks and sings'],
			['improvizace', 'improvisation'],
			['hudební těleso', 'ensemble'],
			['hudba', 'music'],
			['text', 'lyrics'],
			['hudba+text', 'music+lyrics'],
			['původní text', 'original lyrics'],
			['český text', 'czech lyrics'],
			['hudební improvizace', 'music improvisation'],
			['autor', 'author'],
			['účinkuje', 'participating'],
			['řídí', 'conductor'],
			['dirigent', 'conductor'],
			['sbormistr', 'choirmaster'],
			['produkce', 'produced by'],
			['nahrál', 'recorded by'],
			['digitální přepis', 'A/D transfer'],
		  ].forEach(function(subst) {
			if (typeof subst[0] == 'string' && role.toLowerCase() == subst[0].toLowerCase()
			   || subst[0] instanceof RegExp && subst[0].test(role)) role = role.replace(subst[0], subst[1]);
		  });
		  return role;
		}
	  }); else if (url.toLowerCase().includes('bontonland.cz/')) return globalFetch(url).then(function(response) {
		ref = response.document.querySelector('div#detailheader > h1');
		if (ref != null && /^(.*?)\s*:\s*(.*)$/.test(ref.textContent.trim())) {
		  artist = RegExp.$1;
		  isVA = vaParser.test(artist);
		  album = RegExp.$2;
		}
		media = 'CD';
		response.document.querySelectorAll('table > tbody > tr > td.nazevparametru').forEach(function(it) {
		  if (it.textContent.includes('Datum vydání')) {
			releaseDate = normalizeDate(it.nextElementSibling.textContent);
			albumYear = extractYear(it.nextElementSibling.textContent);
		  } else if (it.textContent.includes('Nosič / počet')) {
			if (/^(.*?)\s*\/\s*(.*)$/.test(it.nextElementSibling.textContent)) {
			  media = RegExp.$1;
			  totalDiscs = RegExp.$2;
			}
		  } else if (it.textContent.includes('Interpret')) {
			artist = it.nextElementSibling.textContent.trim();
		  } else if (it.textContent.includes('EAN')) {
			identifiers.BARCODE = it.nextElementSibling.textContent.trim();
		  }
		});
		getDescription(response, 'div#detailtabpopis > div[class^="pravy"] > div > p:not(:last-of-type)', true);
		if ((ref = response.document.querySelector('a.detailzoom')) != null) imgUrl = ref.href;
		if ((ref = response.document.querySelector('div#detailtabpopis > div[class^="pravy"] > div > ol')) != null) {
		  return Array.from(ref.querySelectorAll('li')).map(function(track, ndx, arr) {
			title = track.innerText.trim();
			duration = undefined;
			if (/^(.*?)\s+\(((?:\d+:)?\d+:\d+)\)$/.test(title) || /^(.*?)\s+\(((?:\d+:)?\d+:\d+)\)$/.test(title)) {
			  title = RegExp.$1;
			  duration = timeStringToTime(RegExp.$2);
			}
			return {
			  artist: isVA ? VA : artist,
			  album: album,
			  //album_year: extractYear(releaseDate),
			  release_date: releaseDate,
			  label: label,
			  media: media,
			  tracknumber: ndx + 1,
			  totaltracks: arr.length,
			  title: title,
			  duration: duration,
			  url: response.finalUrl.replace(/\?.*$/, ''),
			  description: description,
			  identifiers: mergeIds(),
			  cover_url: imgUrl,
			};
		  });
		} else if ((ref = response.document.querySelector('div#detailtabpopis > div[class^="pravy"] > div > p:last-of-type')) != null) {
		  const plParser = /^(\d+)(?:\s*[\/\.\-\:\)])?\s+(.*?)(?:\s+((?:(?:\d+:)?\d+:)?\d+))?$/;
		  var trackList = html2php(ref, response.finalUrl).trim().split(/[\r\n]+/)
		  	.filter(it => plParser.test(it.trim())).map(it => plParser.exec(it.trim()));
		  return Array.from(trackList).map(track => ({
			artist: isVA ? VA : artist,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			media: media,
			tracknumber: track[1],
			totaltracks: trackList.length,
			title: track[2],
			duration: timeStringToTime(track[3]),
			url: response.finalUrl.replace(/\?.*$/, ''),
			description: description,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  }));
		} else throw 'Playlist could not be located';
	  }); else if (url.toLowerCase().includes('nativedsd.com/')) return globalFetch(url).then(function(response) {
		identifiers.COMPOSEREMPHASIS = 1;
		artist = (ref = response.document.querySelector('div.the-content > header > h2')) != null ?
		  ref.firstChild.data.trim() : undefined;
		isVA = !artist || vaParser.test(artist);
		if ((ref = response.document.querySelector('div.the-content > header > h1')) != null) album = ref.firstChild.data.trim();
		if ((ref = response.document.querySelector('div.the-content > header > h3')) != null) composer = ref.firstChild.data.trim();
		if ((ref = response.document.querySelector('div.the-content > header > h1 > small')) != null)
		  albumYear = extractYear(ref.firstChild.data);
		ref = response.document.querySelector('div#breadcrumbs > div[class] > a:nth-of-type(2)');
		if (ref != null) label = ref.firstChild.data.trim();
		if (label == 'Albums') label = undefined;
		if ((ref = response.document.querySelector('h2#sku')) != null) {
		  if (/^\s*(?:Catalog\sNumber):\s*(.*?)\s*$/im.test(ref.textContent)) catalogue = RegExp.$1;
		  if (/^\s*(?:Released\son\sNativeDSD):\s*(.*?)\s*$/im.test(ref.textContent)) releaseDate = RegExp.$1;
		  if (/^\s*(?:ID):\s*(.*?)\s*$/im.test(ref.textContent)) identifiers.NATIVEDSD_ID = RegExp.$1;
		}
		identifiers.ORIGINALFORMAT = 'DSD';
		getDescription(response, 'div.the-content > div.entry > p', false);
		if ((ref = response.document.querySelector('div#repertoire > div > p')) != null) {
		  let repertoire = html2php(ref, url);
		  if (description) description += '\n\n';
		  let ndx = repertoire.indexOf('\n[b]Track');
		  description += (ndx >= 0 ? repertoire.slice(0, ndx) : repertoire).trim().flatten();
		}
		ref = response.document.querySelectorAll('div#techspecs > table > tbody > tr');
		if (ref.length > 0) {
		  if (description) description += '\n\n';
		  description += '[b][u]Tech specs[/u][/b]';
		  ref.forEach(function(it) {
			description += '\n[b]'.concat(it.children[0].textContent.trim(), '[/b] ', it.children[1].textContent.trim());
		  });
		}
		if ((ref = response.document.querySelector('a#album-cover')) != null) imgUrl = ref.href;
		trs = response.document.querySelectorAll('div#track-list > table > tbody > tr[id^="track"]');
		return Array.from(trs).map(function(tr) {
		  title = undefined;
		  trackIdentifiers = { TRACK_ID: tr.id.replace(/^track-/i, '') };
		  var trackComposer;
		  if ((ref = tr.children[1]) != null) {
			title = ref.firstChild.textContent.trim();
			trackComposer = ref.childNodes[2] && ref.childNodes[2].textContent.trim() || undefined;
		  }
		  return {
			artist: isVA ? VA : artist,
			album: album,
			album_year: albumYear,
			release_date: releaseDate,
			label: label,
			catalog: catalogue,
			encoding: 'lossless',
			codec: 'FLAC',
			bd: 24,
			sr: 88200,
			media: media,
			genre: genres.join('; '), // 'Jazz'
			discnumber: discNumber,
			totaldiscs: totalDiscs,
			discsubtitle: discSubtitle,
			tracknumber: (ref = tr.firstElementChild.firstElementChild) != null ?
			  parseInt(ref.firstChild.data.trim().replace(/\..*$/, '')) : undefined,
			totaltracks: trs.length,
			title: title,
			composer: trackComposer || composer,
			duration: (ref = tr.children[2]) != null ? timeStringToTime(ref.firstChild.data) : undefined,
			url: response.finalUrl,
			description: description,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  };
		});
	  });/* else if (url.toLowerCase().includes('junodownload.com/') && /\/([\d\-]+)\/?$/.test(new URL(url).pathname)) {
		let productKey = RegExp.$1;
		return globalFetch('https://www.junodownload.com/api/1.2/playlist/getplaylistdetails/?product_key='.concat(productKey), {
		  responseType: 'xml',
		}).then(response => Array.from(response.document.querySelectorAll('playlist > trackList > track')).map(function(track, index, trackList) {
		  artist = Array.from(track.querySelectorAll('extension > release_artists > artist > name'))
			.map(artist => artist.textContent.trim());
		  isVA = artist.length == 1 && vaParser.test(artist[0]);
		  trackArtist = Array.from(track.querySelectorAll('extension > artists > artist > name'))
			.map(artist => artist.textContent.trim());
		  trackArtist = isVA || !trackArtist.equalCaselessTo(artist) ? joinArtists(trackArtist) : undefined;
		  title = getValue('extension > track_title');
		  if (getValue('extension > mix_title')) title += ' (' + getValue('extension > mix_title') + ')';
		  return {
			artist: isVA ? VA : artist.join(', '),
			album: getValue('album'),
			release_date: getValue('extension > relDate'),
			label: getValue('extension > label > name'),
			catalog: getValue('extension > catNumber'),
			media: media,
			genre: getValue('extension > genre'),
			tracknumber: parseInt(getValue('trackNum')),
			totaltracks: trackList.length,
			title: getValue('extension > track_title'),
			track_artist: trackArtist,
			duration: parseInt(getValue('extension > length')) || undefined,
			description: getValue('extension > rating_comment'),
			identifiers: { JUNODOWNLOAD_ID: productKey },
			cover_url: getValue('image'),
		  };

		  function getValue(selector) {
			var node = track.querySelector(selector);
			return node != null ? node.textContent.trim() : undefined;
		  }
		}));
	  }*/ else if (url.toLowerCase().includes('junodownload.com/')) return globalFetch(url).then(function(response) {
		if (/'id':'([\d\-]+)'/.test(response.responseText) || /\/([\d\-]+)\/?$/.test(new URL(response.finalUrl).pathname)) {
		  identifiers.JUNODOWNLOAD_ID = RegExp.$1;
		  var metaData = globalFetch('https://www.junodownload.com/api/1.2/playlist/getplaylistdetails/?product_key='
			.concat(identifiers.JUNODOWNLOAD_ID), { responseType: 'xml' }).then(function(response) {
			return Array.from(response.document.querySelectorAll('playlist > trackList > track'));
		  });
		} else metaData = Promise.reject('No Id');
		var productArtist;
		if ((ref = response.document.querySelectorAll('div.breadcrumb_text > span:not([class])')).length == 4) {
		  artist = Array.from(ref[ref.length - 1].querySelectorAll('a')).map(elem => elem.textContent.trim());
		  productArtist = ref[ref.length - 1].textContent.trim();
		} else if ((ref = response.document.querySelector('h2.product-artist')) != null) {
		  artist = Array.from(ref.querySelectorAll('a')).map(elem => elem.textContent.trim().titleCase());
		  productArtist = ref.textContent.trim().titleCase();
		}
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		if ((ref = response.document.querySelector('meta[itemprop="name"]')) != null) album = ref.content.trim();
		if ((ref = response.document.querySelector('meta[itemprop="author"]')) != null) label = ref.content.trim();
		if ((ref = response.document.querySelector('span[itemprop="datePublished"]')) != null)
		  releaseDate = ref.firstChild.data.trim();
		response.document.querySelectorAll('div.mb-3 > strong').forEach(function(it) {
		  if (it.textContent.startsWith('Genre')) {
			ref = it;
			while ((ref = ref.nextElementSibling) != null && ref.nodeName == 'A') genres.push(ref.textContent.trim());
		  } else if (it.textContent.startsWith('Cat')) {
			if ((ref = it.nextSibling) != null && ref.nodeType == Node.TEXT_NODE) catalogue = ref.wholeText;
		  }
		});
		getDescription(response, 'div[itemprop="review"]');
		if ((ref = response.document.querySelector('meta[property="og:image"][content]')) != null) imgUrl = ref.content;
		trs = response.document.querySelectorAll('div.product-tracklist > div[itemprop="track"]');
		return Array.from(trs).map(function(tr) {
		  trackIdentifiers = { BPM: tr.children[2].textContent.trim() };
		  trackNumber = undefined;
		  tr.querySelector('div.track-title').childNodes.forEach(function(n) {
			if (trackNumber || n.nodeType != Node.TEXT_NODE) return;
			trackNumber = n.data.trim().replace(/\s*\..*$/, '');
		  });
		  trackArtist = (ref = tr.querySelector('meta[itemprop="byArtist"]')) != null ? ref.content : undefined;
		  title = (ref = tr.querySelector('span[itemprop="name"]')) != null ? ref.textContent.trim() : undefined;
		  if (title && trackArtist && title.startsWith(trackArtist + ' - ')) title = title.slice(trackArtist.length + 3);
		  return {
			artist: isVA ? VA : productArtist,
			artists: !isVA ? artist : undefined,
			album: album,
			album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			catalog: catalogue,
			media: media,
			genre: genres.join('; '),
			discnumber: discNumber,
			totaldiscs: totalDiscs,
			discsubtitle: discSubtitle,
			tracknumber: trackNumber,
			totaltracks: trs.length,
			title: title,
			track_artist: trackArtist && (isVA || trackArtist.toLowerCase() != productArtist.toLowerCase()) ? trackArtist : undefined,
			duration: durationFromMeta(tr),
			url: !identifiers.JUNODOWNLOAD_ID ? response.finalUrl : undefined,
			description: description,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  };
		});
	  }); else if (/\b(?:hdtracks)(?:\.\w+)+\//i.test(url)) return globalFetch(url).then(function(response) {
		response.document.querySelectorAll('div.album-main-details > ul > li > span').forEach(function(it) {
		  if (it.textContent.startsWith('Title')) album = it.nextSibling.data.trim();
		  if (it.textContent.startsWith('Artist')) artist = it.nextElementSibling.textContent.trim();
		  if (it.textContent.startsWith('Genre')) {
			ref = it;
			while ((ref = ref.nextElementSibling) != null) genres.push(ref.textContent.trim());
		  }
		  if (it.textContent.startsWith('Label')) label = it.nextElementSibling.textContent.trim();
		  if (it.textContent.startsWith('Release Date')) releaseDate = normalizeDate(it.nextSibling.data.trim());
		});
		isVA = vaParser.test(artist);
		if ((ref = response.document.querySelector('p.product-image > img')) != null) imgUrl = ref.src;
		trs = response.document.querySelectorAll('table#track-table > tbody > tr[id^="track"]');
		return Array.from(trs).map(function(tr) {
		  format = tr.querySelector('td:nth-of-type(4) > span').textContent.trim();
		  sr = tr.querySelector('td:nth-of-type(5)').textContent.trim().replace(/\/.*/, '');
		  if (/^([\d\.\,]+)\s*\/\s*(\d+)$/.test(sr)) {
			sr = Math.round(parseFloat(RegExp.$1.replace(',', '.')) * 1000);
			bd = parseInt(RegExp.$2);
		  } else sr = Math.round(parseFloat(sr) * 1000);
		  return {
			artist: isVA ? VA : artist,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			catalog: catalogue,
			encoding: 'lossless',
			be: bd || 24,
			sr: sr || undefined,
			media: media,
			genre: genres.join('; '),
			//discnumber: discNumber,
			//totaldiscs: totaldiscs,
			//discsubtitle: discSubtitle,
			tracknumber: (ref = tr.querySelector('td:first-of-type')) != null ?
				parseInt(ref.textContent) || ref.textContent.trim() : undefined,
			totaltracks: trs.length,
			title: (ref = tr.querySelector('td.track-name')) != null ? ref.textContent.trim() : undefined,
			duration: (ref = tr.querySelector('td:nth-of-type(3)')) != null ? timeStringToTime(ref.textContent.trim()) : undefined,
			url: response.finalUrl,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  }
		});
	  }); else if (/^https?:\/\/(?:\w+\.)?deezer\.com\/(?:\w+\/)*album\/(\d+)/i.test(url)) {
		return queryDeezerAPI('album/' + RegExp.$1).then(function(release) {
		  isVA = vaParser.test(release.artist.name);
		  identifiers.DEEZER_ID = release.id;
		  identifiers.RELEASETYPE = release.record_type;
		  if (release.upc) identifiers.BARCODE = release.upc;
		  if (release.cover_xl) imgUrl = release.cover_xl.replace('1000x1000-000000-80-0-0', '1400x1400-000000-100-0-0');
		  return release.tracks.data.map(function(track, ndx) {
			trackIdentifiers = { TRACK_ID: track.id };
			return {
			  artist: isVA ? VA : release.artist.name,
			  album: release.title,
			  release_date: release.release_date,
			  label: release.label,
			  media: media,
			  genre: release.genres.data.map(it => it.name).join('; '),
			  tracknumber: ndx + 1,
			  totaltracks: release.nb_tracks,
			  title: track.title,
			  track_artist: track.artist.name && (isVA || track.artist.name != release.artist.name) ? track.artist.name : undefined,
			  duration: track.duration,
			  //url: deezerAlbumPrefix + release.id,
			  identifiers: mergeIds(),
			  cover_url: imgUrl,
			};
		  });
		});
	  } else if (url.toLowerCase().includes('spotify.com/')) {
		if (!/\/albums?\/(\w+)$/i.test(url)) return Promise.reject('This resource is not supported, pick a real album');
		return querySpotifyAPI('albums/' + RegExp.$1).then(function(release) {
		  artist = release.artists.map(artist => artist.name);
		  isVA = release.artists.length <= 0 || release.artists.length == 1 && vaParser.test(release.artists[0].name);
		  totalDiscs = release.tracks.items.reduce((acc, track) => Math.max(acc, track.disc_number), 0);
		  identifiers.SPOTIFY_ID = release.id;
		  identifiers.RELEASETYPE = release.album_type;
		  identifiers.BARCODE = release.external_ids.upc;
		  var image = release.images.reduce((acc, image) => image.width * image.height > acc.width * acc.height ? image : acc);
		  return release.tracks.items.map(function(track, ndx) {
			trackIdentifiers = {
			  TRACK_ID: track.id,
			  EXPLICIT: Number(track.explicit),
			};
			trackArtist = track.artists.map(artist => artist.name);
			return {
			  artist: isVA ? VA : undefined,
			  artists: !isVA ? artist : undefined,
			  album: release.name,
			  release_date: release.release_date,
			  label: release.label,
			  media: media,
			  genre: release.genres.join('; '),
			  discnumber: track.disc_number,
			  totaldiscs: totalDiscs,
			  discsubtitle: discSubtitle,
			  tracknumber: track.track_number,
			  totaltracks: release.total_tracks,
			  title: track.name,
			  track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
			  duration: track.duration_ms / 1000,
			  //url: 'https://open.spotify.com/album/' + release.id,
			  identifiers: mergeIds(),
			  cover_url: image ? image.url : undefined,
			};
		  });
		});
	  } else if (url.toLowerCase().includes('prostudiomasters.com/')) return globalFetch(url).then(function(response) {
		if ((ref = response.document.querySelector('img.album-art')) != null) imgUrl = ref.currentSrc || ref.src;
		try {
		  if ((ref = response.document.querySelector('body > script[charset]')) == null
			  || !/\bPSM\.album\s*=\s*(\{[\S\s]+\});(?=\s*PSM\b)/.test(ref.text)) throw 'Metadata not found';
		  let album = JSON.parse(RegExp.$1);
		  if (prefs.diag_mode) console.debug('PSM metadata received:', album);
		  const artistSplitter = /\s*;+\s*/;
		  artist = album.ArtistName.split(artistSplitter);
		  isVA = vaParser.test(album.ArtistName);
		  if (album.id) identifiers.PROSTUDIOMASTERS_ID = parseInt(album.id) || album.id;
		  if (album.GenreName) genres.push(album.GenreName);
		  if (album.SubGenreName) genres.push(album.SubGenreName);
		  if (album.genres) genres.push(album.genres);
		  if (/^[℗©]\s*(\d{4})\b/.test(album.PLine) || /^[℗©]\s*(\d{4})\b/.test(album.CLine)) releaseDate = RegExp.$1;
		  if (album.ICPN) identifiers.BARCODE = album.ICPN;
		  if (/\b(\d+(?:\.\d+)?)\s*kHz\s*\/\s*(\d+)[\-\s]?bit\s+(\w+)\b/i.test(album.recording_info)) {
			sr = parseFloat(RegExp.$1) * 1000 || undefined;
			bd = parseInt(RegExp.$2) || undefined;
			format = RegExp.$3;
			if (['FLAC', 'AIFF', 'WAV', 'PCM'].includes(format)) encoding = 'lossless';
		  }
		  if (album.album_info) {
			description = html2php(domParser.parseFromString(album.album_info, 'text/html').body, response.finalUrl);
			if (description) description = '[quote]'.concat(description, '[/quote]');
		  }
		  return album.tracks.map(function(track) {
			trackIdentifiers = {
			  EXPLICIT: Number(track.ExplicitLyrics == 1),
			  ISRC: track.ISRC,
			  TRACK_ID: parseInt(track.id) || track.id,
			};
			trackArtist = track.ArtistName.split(artistSplitter);
			return {
			  artist: isVA ? VA : undefined,
			  artists: !isVA ? artist : undefined,
			  album: album.AlbumName,
			  genre: genres.join('; '),
			  release_date: releaseDate,
			  label: label,
			  catalog: album.CatalogNumber,
			  codec: format,
			  encoding: encoding,
			  bd: bd,
			  sr: sr,
			  media: media,
			  discnumber: parseInt(track.DiscSeq) || undefined,
			  discsubtitle: track.GroupingTitle,
			  tracknumber: parseInt(track.TrackSeq) || undefined,
			  totaltracks: album.tracks.length,
			  title: track.TrackName,
			  track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalTo(artist)) ? trackArtist : undefined,
			  composers: track.composers.split(artistSplitter),
			  duration: parseInt(track.duration) || undefined,
			  url: !identifiers.PROSTUDIOMASTERS_ID ? response.finalUrl : undefined,
			  description: description,
			  identifiers: mergeIds(),
			  cover_url: imgUrl,
			};
		  });
		} catch(e) {
		  console.warn('ProStudioMasters: falling back to HTML scraper for the reason', e);
		  if (/\/page\/(\d+)$/i.test(response.finalUrl)) identifiers.PROSTUDIOMASTERS_ID = RegExp.$1;
		  artist = Array.from(response.document.querySelectorAll('h2.ArtistName > a')).map(node => node.textContent.trim());
		  isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		  if (isVA) artist = [];
		  if ((ref = response.document.querySelector('h3.AlbumName')) != null) album = ref.textContent.trim();
		  if ((ref = response.document.querySelector('div.pline')) != null
			  && /^(?:[℗©]\s*)+(\d{4})\s+(.+)/.test(ref.textContent.trim())) {
			releaseDate = RegExp.$1;
			label = RegExp.$2;
		  }
		  getDescription(response, 'div.album-info', false);
		  trs = response.document.querySelectorAll('div.album-tracks > div.tracks > table > tbody > tr');
		  totalTracks = Array.from(trs).filter(tr => tr.classList.contains('track-playable')).length;
		  discNumber = 0;
		  trs.forEach(function(tr) {
			if (tr.classList.contains('track-playable')) {
			  trackArtist = sr = bd = format = title = undefined; trackIdentifiers = {};
			  if (ref = tr.getAttribute('data-track-id')) trackIdentifiers.TRACK_ID = ref;
			  trackNumber = (ref = tr.querySelector('div.num')) != null ? parseInt(ref.firstChild.textContent.trim()) : undefined;
			  if (trackNumber == 1) ++discNumber;
			  if ((ref = tr.querySelector('td.track-name > div.name')) != null) {
				title = ref.firstChild.textContent.trim();
				if ((ref = ref.querySelector(':scope small')) != null) trackArtist = ref.firstChild.textContent;
			  };
			  if ((ref = tr.querySelector('span.track-format')) != null && /^(\d+(?:[,\.]\d+)?)\s*([kMG]?Hz)(?:\s+(\d+)-bit)?\s*\|\s*(\S+)$/i.test(ref.textContent.trim())) {
				sr = parseFloat(RegExp.$1);
				['hz', 'khz', 'mhz', 'ghz'].forEach((unit, ndx) => { if (RegExp.$2.toLowerCase() == unit) sr *= 1000 ** ndx });
				sr = Math.round(sr) || undefined;
				bd = parseInt(RegExp.$3) || undefined;
				format = RegExp.$4;
			  }
			  tracks.push({
				artist: isVA ? VA : undefined,
				artists: !isVA ? artist : undefined,
				album: album,
				//album_year: extractYear(releaseDate),
				release_date: releaseDate,
				label: label,
				catalog: catalogue,
				codec: format,
				bd: bd,
				sr: sr,
				media: media,
				discnumber: discNumber,
				totaldiscs: totalDiscs,
				discsubtitle: discSubtitle,
				tracknumber: trackNumber,
				totaltracks: totalTracks,
				title: title,
				track_artist: trackArtist && (isVA || trackArtist != joinArtists(artist)) ? trackArtist : undefined,
				duration: (ref = tr.querySelector('td:last-of-type')) != null ? timeStringToTime(ref.firstChild.data) : undefined,
				url: !identifiers.PROSTUDIOMASTERS_ID ? response.finalUrl : undefined,
				description: description,
				identifiers: mergeIds(),
				cover_url: imgUrl,
			  });
			} else if ((ref = tr.querySelector('div.grouping-title')) != null) {
			  discSubtitle = ref.textContent.trim();
			  guessDiscNumber();
			}
		  });
		  return tracks;
		}
	  }); else if (url.toLowerCase().includes('play.google.com/store/music/album/')) {
		let _url = new URL(url), _query = new URLSearchParams(_url.search);
		_query.set('hl', 'en');
		_url.search = _query;
		return globalFetch(_url).then(function(response) {
		  var search = new URLSearchParams(new URL(response.finalUrl).search);
		  var ID = search.get('id'), trackID, aggregateRating;
		  if (ID) identifiers.GOOGLE_ID = ID;
		  var root = response.document.querySelector('div[itemtype="https://schema.org/MusicAlbum"]');
		  if (root == null) throw new Error('Unexpected Google Play metadata structure');
		  if ((ref = root.querySelector('div[itemprop="byArtist"]')) != null) {
			artist = Array.from(ref.querySelectorAll('meta[itemprop="name"]')).map(it => it.content);
			isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		  }
		  if ((ref = root.querySelector('meta[itemprop="name"]')) != null) album = ref.content;
		  genres = Array.from(root.querySelectorAll('meta[itemprop="genre"]')).map(elem => elem.content);
		  if ((ref = root.querySelector('meta[itemprop="datePublished"]')) != null) releaseDate = ref.content;
		  if ((ref = root.querySelector('meta[itemprop="numTracks"]')) != null) totalTracks = parseInt(ref.content);
		  if ((ref = root.querySelector('meta[itemprop="ratingValue"]')) != null) aggregateRating = parseFloat(ref.content);
		  if ((ref = response.document.querySelector('h1[class][itemprop="name"] > span')) != null
			 && (ref = ref.parentNode.parentNode.querySelector('div[class] > span[class]')) != null
			 && /\b(?:Explicit)\b/i.test(ref.textContent)) identifiers.EXPLICIT = 1;
		  if ((ref = response.document.querySelector('meta[itemprop="image"]')) != null) imgUrl = ref.content;
		  try {
			let _objs = loadGoogleData(response);
			let _albumInfo = _objs.filter(obj => { try { return obj[0].length == 22 } catch(e) { return false } });
			let _tracks = _objs.filter(function(obj) {
			  try {
				return typeof obj[0][0][1] == 'boolean' && typeof obj[0][0][2] == 'boolean'
				   && typeof obj[0][0][3] == 'string' && typeof obj[0][0][4] == 'string';
			  } catch(e) { return false }
			});
			if (_albumInfo.length == 1) _albumInfo = _albumInfo[0][0]; else throw 'Album metadata not found';
			if (_tracks.length == 1) _tracks = _tracks[0][0][0]; else throw 'Tracks metadata not found';
			if (prefs.diag_mode) console.debug('Google Play objects extracted successfully:', _albumInfo, _tracks);
			try {
			  artist = _albumInfo[18][0][8].map(artist => artist[1]);
			  isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
			} catch(e) { }
			try { description = _albumInfo[10][0][1] } catch(e) { }
			try { genres = _albumInfo[18][0][6].map(genre => genre[2]) } catch(e) { }
			_tracks[0].forEach(function(volume) {
			  Array.prototype.push.apply(tracks, volume[0].map(function(track) {
				trackArtist = track[0][8].map(ta => ta[1]);
				trackIdentifiers = { TRACK_ID: track[12][0] };
				return {
				  artist: isVA ? VA : undefined,
				  artists: !isVA ? artist : undefined,
				  album: _albumInfo[0][0] || track[9][0],
				  album_year: extractYear(_albumInfo[18][0][7][1] /*track[0][7][0]*/),
				  release_date: _albumInfo[18][0][7][2],
				  label: _albumInfo[18][0][4] || track[0][4],
				  media: media,
				  genre: genres.join('; '),
				  discnumber: volume[1],
				  totaldiscs: _tracks[0].length,
				  tracknumber: track[1],
				  totaltracks: _albumInfo[18][4],
				  title: track[8][0],
				  track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
				  duration: track[0][1],
				  description: description,
				  url: !identifiers.GOOGLE_ID ? response.finalUrl : undefined,
				  identifiers: mergeIds(),
				  cover_url: imgUrl,
				};
			  }));
			});
		  } catch(e) {
			console.warn('Google Play music: falling back to HTML scraper (' + e + ')');
			tracks = [];
			if ((ref = response.document.querySelector('span > a[itemprop="genre"]')) != null) try {
			  label = ref.parentNode.nextElementSibling.textContent.trim().replace(/^(?:[©℗]|\([cCpP]\))\s*\d{4}\s+/, '');
			} catch(e) { console.warn('Unexpected HTML structure (' + e + ')') }
			//getDescription(response, '???', false);
			var volumes = response.document.querySelectorAll('c-wiz > div > h2');
			if (volumes.length <= 0) {
			  //response.document.querySelectorAll('c-wiz > div > table > tbody > tr[class]').forEach(scanPlaylist);
			  trackNumber = 0;
			  root.querySelectorAll('div[itemprop="track"]').forEach(function(tr) {
				trackIdentifiers = {};
				if ((ref = tr.querySelector('meta[itemprop="url"]')) != null) {
				  search = new URLSearchParams(new URL(ref.content).search);
				  let trackID = search.get('tid');
				  if (trackID) trackIdentifiers.TRACK_ID = trackID;
				}
				++trackNumber;
				title = (ref = tr.querySelector('meta[itemprop="name"]')) != null ? ref.content : undefined;
				trackArtist = (ref = tr.querySelector('div[itemprop="byArtist"]')) != null ?
				  Array.from(ref.querySelectorAll('meta[itemprop="name"]')).map(it => it.content) : [];
				duration = durationFromMeta(tr);
				addTrack();
			  });
			} else volumes.forEach(function(volume) {
			  discNumber = undefined; discSubtitle = volume.textContent.trim();
			  guessDiscNumber();
			  volume.nextElementSibling.querySelectorAll('tbody > tr[class]').forEach(scanPlaylist);
			});

			function scanPlaylist(tr) {
			  trackNumber = (ref = tr.querySelector('td:nth-of-type(1) > div')) != null ?
				parseInt(ref.textContent) || ref.textContent.trim() : undefined;
			  title = (ref = tr.querySelector('td[itemprop="name"]')) != null ? ref.textContent.trim() : undefined;
			  duration = (ref = tr.querySelector('td:nth-of-type(3)')) != null ? timeStringToTime(ref.textContent) : undefined;
			  trackArtist = Array.from(tr.querySelectorAll('td:nth-of-type(4) > a')).map(it => it.textContent.trim());
			  addTrack();
			}
			function addTrack() {
			  tracks.push({
				artist: isVA ? VA : undefined,
				artists: !isVA ? artist : undefined,
				album: album,
				//album_year: extractYear(releaseDate),
				release_date: releaseDate,
				label: label,
				catalog: catalogue,
				media: media,
				genre: genres.join('; '),
				discnumber: discNumber,
				totaldiscs: totalDiscs,
				discsubtitle: discSubtitle,
				tracknumber: trackNumber,
				totaltracks: totalTracks,
				title: title,
				track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
				duration: duration,
				url: identifiers.GOOGLE_ID ? undefined : response.finalUrl,
				description: description,
				identifiers: mergeIds(),
				cover_url: imgUrl,
			  });
			}
		  }
		  return tracks;
		});
	  } else if (url.toLowerCase().includes('7digital.com/')) return globalFetch(url).then(function(response) {
		if ((ref = response.document.querySelector('table.release-track-list')) != null)
		  identifiers['7DIGITAL_ID'] = parseInt(ref.dataset.releaseid) || ref.dataset.releaseid;
		artist = Array.from(response.document.querySelectorAll('h2.release-info-artist > span[itemprop="byArtist"] > meta[itemprop="name"]'))
		  .map(node => node.content);
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		if ((ref = response.document.querySelector('h1.release-info-title')) != null) album = ref.textContent.trim();
		if ((ref = response.document.querySelector('div.release-date-info > p')) != null) releaseDate = normalizeDate(ref.textContent);
		if ((ref = response.document.querySelector('div.release-label-info > p')) != null) label = ref.textContent.trim();
		response.document.querySelectorAll('dl.release-data > dt.release-data-label').forEach(function(dt) {
		  if (/\bGenres?:/.test(dt.textContent)) genres = Array.from(dt.nextElementSibling.querySelectorAll('a')).map(a => a.textContent.trim());
		});
		//getDescription(response, 'div.album-info', false);
		if ((ref = response.document.querySelector('span.release-packshot-image > img[itemprop="image"]')) != null)
		  imgUrl = ref.src;
		totalTracks = response.document.querySelectorAll('table.release-track-list > tbody > tr.release-track').length;
		response.document.querySelectorAll('table.release-track-list').forEach(function(table) {
		  discSubtitle = discNumber = undefined;
		  if ((ref = table.querySelector('caption > h4.release-disc-info')) != null) {
			discSubtitle = ref.textContent.trim();
			guessDiscNumber();
		  }
		  table.querySelectorAll('tbody > tr.release-track').forEach(function(tr) {
			trackIdentifiers = {};
			if (tr.dataset.trackid) trackIdentifiers.TRACK_ID = parseInt(tr.dataset.trackid) || tr.dataset.trackid;
			tracks.push({
			  artist: isVA ? VA : undefined,
			  artists: !isVA ? artist : undefined,
			  album: album,
			  //album_year: extractYear(releaseDate),
			  release_date: releaseDate,
			  label: label,
			  catalog: catalogue,
			  media: media,
			  genre: genres.join('; '),
			  discnumber: discNumber,
			  totaldiscs: totalDiscs,
			  discsubtitle: discSubtitle,
			  tracknumber: (ref = tr.querySelector('td.release-track-preview > em.release-track-preview-text')) != null ?
				ref.textContent.trim() : undefined,
			  totaltracks: totalTracks,
			  title: (ref = tr.querySelector('td.release-track-name > meta[itemprop="name"]')) != null ? ref.content : undefined,
			  duration: durationFromMeta(tr),
			  url: (ref = response.document.querySelector('head > meta[property="og:url"]')) != null ?
			  	ref.content : response.finalUrl.replace(/\?.*$/, ''),
			  description: description,
			  identifiers: mergeIds(),
			  cover_url: imgUrl,
			});
		  });
		});
		return tracks;
	  }); else if (url.toLowerCase().includes('e-onkyo.com/')) return globalFetch(url).then(function(response) {
		if (/\/album\/(\w+)\/?$/.test(response.finalUrl)) identifiers.EONKYO_ID = RegExp.$1;
		artist = Array.from(response.document.querySelectorAll('div.jacketDetailArea p.artistsName > a'))
		  .map(node => node.textContent.trim());
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		if ((ref = response.document.querySelector('div.jacketDetailArea p.packageTtl')) != null) album = ref.textContent.trim();
		if ((ref = response.document.querySelector('div.jacketDetailArea p.recordlabelName > a')) != null) label = ref.textContent.trim();
		if ((ref = response.document.querySelector('div.jacketDetailArea p.releaseDay > a')) != null) releaseDate = normalizeDate(ref.textContent);
		if ((ref = response.document.querySelector('div.jacketDetailArea p.packageNoteDetail')) != null
			&& /^\s*(?:\(C\)|©)\s+(\d{4})\b/i.test(ref.lastChild.textContent)) albumYear = parseInt(RegExp.$1);
		//getDescription(response, 'div#credit', true);
		if (/\s+\(\s*(?:(\d+)[\-\s]*bit)?\s*\/?\s*(?:(\d+(?:\.\d+)?)\s*kHz)?\s*\)\s*$/i.test(album)) {
		  album = RegExp.leftContext;
		  bd = parseInt(RegExp.$1) || undefined;
		  sr = parseFloat(RegExp.$2) * 1000;
		}
		if ((ref = response.document.querySelector('figure > a.colorbox')) != null)
		  imgUrl = new URL(response.finalUrl).origin + ref.pathname;
		trs = response.document.querySelectorAll('dl.musicList > dd.musicBox');
		return Array.from(trs).map(tr => ({
		  //var trackId = tr.dataset.trackid;
		  //if (trackId) trackId = 'TRACK_ID=' + trackId;
		  //trackArtist = tr.children[5].textContent.trim();
		  //if (trackArtist == artist.join(', ')) trackArtist = undefined;
		  artist: isVA ? VA : undefined,
		  artists: !isVA ? artist : undefined,
		  album: album,
		  album_year: albumYear,
		  release_date: releaseDate,
		  label: label,
		  catalog: catalogue,
		  encoding: 'lossless',
		  codec: 'FLAC',
		  bd: bd,
		  sr: sr || undefined,
		  media: media,
		  //discnumber: discNumber,
		  //totaldiscs: totalDiscs,
		  //discsubtitle: discSubtitle,
		  tracknumber: (ref = tr.querySelector('div.musicListNo')) != null ? ref.textContent.trim() : undefined,
		  totaltracks: trs.length,
		  title: (ref = tr.querySelector('div.musicTtl > span')) != null ? ref.title || ref.textContent.trim() : undefined,
		  duration: (ref = tr.querySelector('div.musicTime')) != null ? timeStringToTime(ref.textContent.trim()) : undefined,
		  url: !identifiers.EONKYO_ID ? response.finalUrl : undefined,
		  description: description,
		  identifiers: mergeIds(),
		  cover_url: imgUrl,
		}));
	  }); else if (url.toLowerCase().includes('store.acousticsounds.com/')) return globalFetch(url).then(function(response) {
		if (/\/(\d+)\/$/.test(response.finalUrl)) identifiers.ACOUSTICSOUNDS_ID = RegExp.$1;
		artist = Array.from(response.document.querySelectorAll('div > h1 > a')).map(node => node.textContent.trim());
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		if (isVA) artist = [];
		if ((ref = response.document.querySelector('div > h1')) != null) album = ref.lastChild.wholeText.trim().replace(/\s*-\s*/, '');
		response.document.querySelectorAll('div > p > table > tbody > tr > td:first-of-type').forEach(function(td) {
		  if (/^(?:Label):/i.test(td.textContent)) label = td.nextElementSibling.textContent.trim();
		  if (/^(?:Genre):/i.test(td.textContent)) genres[0] = td.nextElementSibling.textContent.trim();
		  if (/^(?:Product\s+No):/i.test(td.textContent)) catalogue = td.nextElementSibling.textContent.trim();
		  if (/^(?:Category):/i.test(td.textContent)
			  && /^(.+)\s+(\d+(?:\.\d+)?)\s*kHz(?:\s*\/\s*(\d+)[\s\-]?bit)?\s+Download\b/.test(td.nextElementSibling.textContent.trim())) {
			format = RegExp.$1;
			sr = parseFloat(RegExp.$2) * 1000;
			bd = parseInt(RegExp.$3);
		  }
		});
		getDescription(response, 'div#description > p', true);
		if ((ref = response.document.querySelector('div#detail > link[rel="image_src"]')) != null) {
		  imgUrl = ref.href.replace(/\/medium\//i, '/large/');
		}
		trs = response.document.querySelectorAll('div#tracks > table > tbody > tr');
		trackNumber = 0;
		return Array.from(trs).map(tr => ({
		  artist: isVA ? VA : undefined,
		  artists: !isVA ? artist : undefined,
		  album: album,
		  //album_year: extractYear(releaseDate),
		  release_date: releaseDate,
		  label: label,
		  catalog: catalogue,
		  encoding: ['FLAC', 'DSD'].includes(format) ? 'lossless' : undefined,
		  codec: format,
		  bd: bd,
		  sr: sr || undefined,
		  media: media,
		  genre: genres.join('; '),
		  //discnumber: discNumber,
		  //totaldiscs: totalDiscs,
		  //discsubtitle: discSubtitle,
		  tracknumber: ++trackNumber,
		  totaltracks: trs.length,
		  title: (ref = tr.querySelector('td[nowrap]')) != null ? ref.textContent.trim() : undefined,
		  url: !identifiers.ACOUSTICSOUNDS_ID ? response.finalUrl : undefined,
		  description: description,
		  identifiers: mergeIds(),
		  cover_url: imgUrl,
		}));
	  }); else if (url.toLowerCase().includes('indies.eu/')) return globalFetch(url).then(function(response) {
		if (/\/alba\/(\d+)\//.test(response.finalUrl)) identifiers.INDIESSCOPE_ID = parseInt(RegExp.$1);
		ref = response.document.querySelector(':root > body > div > div > div > h2');
		if (ref != null) artist = Array.from(ref.childNodes).map(node => node.textContent.trim());
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		if ((ref = response.document.querySelector(':root > body > div > div > div > h1')) != null)
		  album = ref.textContent.trim();
		if ((ref = response.document.querySelector('div.infoBox')) != null) {
		  let ndx = 0;
		  ref.childNodes.forEach(function(child) {
			if (child.nodeName == 'BR') { ++ndx; return; }
			switch (ndx) {
			  case 0:
				if (child.nodeType == Node.TEXT_NODE) {
				  label = child.wholeText.trim();
				  if (/^(.*)\s+\/\s+(\d{4})$/.test(label)) {
					label = RegExp.$1;
					releaseDate = RegExp.$2;
				  }
				}
				break;
			  case 1:
				if (child.nodeType == Node.ELEMENT_NODE) genres.push(child.textContent.trim());
				break;
			  case 2:
				if (child.nodeType == Node.ELEMENT_NODE) catalogue = child.textContent.trim();
				break;
			}
		  });
		}
		getDescription(response, 'div.popis > section', true);
		if ((ref = response.document.querySelector('div.obrazekDetail > img')) != null) imgUrl = ref.src;
		trs = response.document.querySelectorAll('table.skladby > tbody > tr');
		return Array.from(trs).map(function(tr) {
		  title = undefined;
		  if ((ref = tr.querySelector('td.nazev')) != null) {
			trackNumber = parseInt(ref.firstChild.wholeText);
			title = ref.querySelector('strong').textContent.trim();
		  }
		  return {
			artist: isVA ? VA : undefined,
			artists: !isVA ? artist : undefined,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			catalog: catalogue,
			codec: format,
			media: media,
			genre: genres.join('; '),
			//discnumber: discNumber,
			//totaldiscs: totalDiscs,
			//discsubtitle: discSubtitle,
			tracknumber: trackNumber,
			totaltracks: trs.length,
			title: title,
			duration: (ref = tr.querySelector('td:nth-of-type(4)')) != null ? timeStringToTime(ref.textContent) : undefined,
			identifiers: !identifiers.INDIESSCOPE_ID ? response.finalUrl : undefined,
			description: description,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  };
		});
	  }); else if (url.toLowerCase().includes('beatport.com/')) return globalFetch(url).then(function(response) {
		if (/\/release\/(?:\d\/)?(?:\S+-)?(\d+)\b/i.test(response.finalUrl)) identifiers.BEATPORT_ID = RegExp.$1;
		artist = Array.from(response.document.querySelectorAll('span > a[data-artist]')).map(node => node.textContent.trim());
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		if ((ref = response.document.querySelector('div > h1')) != null) album = ref.textContent.trim();
		response.document.querySelectorAll('ul > li > span.category').forEach(function(span) {
		  if (/^(?:Release\s+Date)/i.test(span.textContent)) releaseDate = span.nextElementSibling.textContent.trim();
		  if (/^(?:Label)/i.test(span.textContent)) label = span.nextElementSibling.textContent.trim();
		  if (/^(?:Catalog)/i.test(span.textContent)) catalogue = span.nextElementSibling.textContent.trim();
		});
		getDescription(response, 'div.interior-expandable', true);
		if ((ref = response.document.querySelector('div > img.interior-release-chart-artwork')) != null) imgUrl = ref.src;
		trs = response.document.querySelectorAll('div.tracks > ul > li.track');
		return Array.from(trs).map(function(tr) {
		  trackIdentifiers = { TRACK_ID: parseInt(tr.dataset.ecId) || tr.dataset.ecId };
		  title = (ref = tr.querySelector('span.buk-track-primary-title')) != null ?
			ref.title || ref.textContent.trim() : tr.dataset.ecName;
		  if (title && (ref = tr.querySelector('span.buk-track-remixed')) != null) title += ' (' + ref.textContent.trim() + ')';
		  trackArtist = Array.from(tr.querySelectorAll('p.buk-track-artists > a')).map(a => a.textContent.trim());
		  if ((ref = tr.querySelector('p.buk-track-bpm')) != null) trackIdentifiers.BPM = parseInt(ref.textContent);
		  return {
			artist: isVA ? VA : undefined,
			artists: !isVA ? artist : undefined,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: tr.dataset.ecBrand || ((ref = tr.querySelector('p.buk-track-labels')) != null ? ref.textContent.trim() : label),
			catalog: catalogue,
			codec: format,
			media: media,
			genre: Array.from(tr.querySelectorAll('p.buk-track-genre > a')).map(a => a.textContent).join('; '),
			//discnumber: discNumber,
			//totaldiscs: totalDiscs,
			//discsubtitle: discSubtitle,
			tracknumber: tr.dataset.ecPosition || ((ref = tr.querySelector('div.buk-track-num')) != null ?
				ref.textContent.trim() : undefined),
			totaltracks: trs.length,
			title: title,
			track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
			remixers: Array.from(tr.querySelectorAll('p.buk-track-remixers > a')).map(a => a.textContent.trim()),
			duration: (ref = tr.querySelector('p.buk-track-length')) != null ? timeStringToTime(ref.textContent) : undefined,
			description: description,
			url: !identifiers.BEATPORT_ID ? response.finalUrl : undefined,
			cover_url: imgUrl,
			identifiers: mergeIds(),
		  };
		});
	  }); else if (url.toLowerCase().includes('traxsource.com/')) return globalFetch(url).then(function(response) {
		if (/\/title\/(\d+)(?=\/|$)/i.test(response.finalUrl)) identifiers.TRAXSOURCE_ID = RegExp.$1;
		artist = Array.from(response.document.querySelectorAll('h1.artists > a.com-artists')).map(node => node.textContent.trim());
		if (artist.length <= 0 && (ref = response.document.querySelector('h1.artists')) != null) artist = [ref.textContent.trim()];
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		if ((ref = response.document.querySelector('h1.title')) != null) album = ref.textContent.trim();
		if ((ref = response.document.querySelector('a.com-label')) != null) label = ref.textContent.trim();
		if ((ref = response.document.querySelector('div.cat-rdate')) != null && /^(.*)\s*\|\s*(.*)$/.test(ref.textContent.trim())) {
		  catalogue = RegExp.$1;
		  releaseDate = normalizeDate(RegExp.$2);
		}
		getDescription(response, 'div.desc', true);
		if ((ref = response.document.querySelector('meta[property="og:image"][content]')) != null) imgUrl = ref.content;
		trs = response.document.querySelectorAll('div.trklist > div.trk-row');
		return Array.from(trs).map(function(tr) {
		  trackIdentifiers = {};
		  title = (ref = tr.querySelector('div.title > a')) != null && ref.textContent.trim() || undefined;
		  if (title && (ref = tr.querySelector('span.version')) != null ) {
			if (ref.firstChild.nodeType == Node.TEXT_NODE
				&& (i = ref.firstChild.wholeText.trim()).length > 0) title += ' ('.concat(i, ')');
		  }
		  trackArtist = Array.from(tr.querySelectorAll('div.artists a.com-artists')).map(a => a.textContent.trim());
		  return {
			artist: isVA ? VA : undefined,
			artists: !isVA ? artist : undefined,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			catalog: catalogue,
			media: media,
			genre: Array.from(tr.querySelectorAll('div.genre > a')).map(a => a.textContent.trim()).join('; '),
			//discnumber: discNumber,
			//totaldiscs: totalDiscs,
			//discsubtitle: discSubtitle,
			tracknumber: (ref = tr.querySelector('div.tnum')) != null ? ref.textContent.trim() : undefined,
			totaltracks: trs.length,
			title: title,
			track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
			remixers: Array.from(tr.querySelectorAll('div.artists a.com-remixers')).map(a => a.textContent.trim()),
			duration: (ref = tr.querySelector('span.duration')) != null ? timeStringToTime(ref.textContent) : undefined,
			url: !identifiers.TRAXSOURCE_ID ? response.finalUrl : undefined,
			description: description,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  };
		});
	  }); else if (url.toLowerCase().includes('music.apple.com/')) return loadItunesMetadata(url).then(function(album) {
		identifiers.APPLE_ID = parseInt(album.id) || album.id;
		isVA = vaParser.test(album.attributes.artistName);
		genres = album.attributes.genreNames.filter(genre => genre != 'Music');
		label = album.attributes.recordLabel;
		if (!label) label = album.attributes.copyright.replace(/^((?:[©℗]|\([PC]\))\s+)?(?:(\d{4})\s+)?/i, '');
		//identifiers.EXPLICIT = Number(/^(?:explicit)$/i.test(album.attributes.contentRating));
		if ('isCompilation' in album.attributes) identifiers.COMPILATION = Number(album.attributes.isCompilation);
		if (album.attributes.isSingle) identifiers.RELEASETYPE = 'Single';
		if (album.description) description = html2php(album.description, album.attributes.url);
		if (!description && album.attributes.editorialNotes)
		  description = html2php(domParser.parseFromString(album.attributes.editorialNotes.standard
			|| album.attributes.editorialNotes.short, 'text/html').body, album.attributes.url).replace(/\n/g, '\n\n');
		if (description && !description.includes('[/quote]')) description = '[quote]'.concat(description, '[/quote]');
		//if (description && !description.includes('[quote]')) description = '[quote]' + description.collapseGaps() + '[/quote]';
		if (album.attributes.artwork && prefs.apple_offer_alt_cover)
		  addMessage(new HTML('<a target="_blank" href="' + album.attributes.artwork.realUrl + '">Alternate cover URL</a>'), 'info');
		return album.relationships.tracks.data.filter(track => track.type == 'songs').map(function(track) {
		  trackIdentifiers = {
			TRACK_ID: parseInt(track.id),
			ISRC: track.attributes.isrc,
			EXPLICIT: Number(/^(?:explicit)$/i.test(track.attributes.contentRating)),
			HASLYRICS: Number(track.attributes.hasLyrics || false),
		  };
		  var trackGenres = track.attributes.genreNames.filter(genre => genre != 'Music');
		  return {
			artist: isVA ? VA : album.attributes.artistName,
			artists: album.relationships.artists.data.map(artist => artist.attributes.name),
			album: album.attributes.name,
			release_date: album.attributes.releaseDate,
			label: label,
			media: media,
			genre: (trackGenres.length > 0 ? trackGenres : genres).join('; '),
			discnumber: track.attributes.discNumber,
			discsubtitle: track.attributes.workName,
			tracknumber: track.attributes.trackNumber,
			totaltracks: album.attributes.trackCount,
			title: track.attributes.name,
			track_artist: track.attributes.artistName && (isVA || track.attributes.artistName != album.attributes.artistName) ?
				track.attributes.artistName : undefined,
			composer: track.attributes.composerName,
			duration: track.attributes.durationInMillis / 1000 || undefined,
			description: description,
			url: !identifiers.APPLE_ID ? album.attributes.url : undefined,
			identifiers: mergeIds(),
			//cover_url: album.attributes.artwork ? album.attributes.artwork.realUrl : undefined,
		  };
		});
	  }); else if (mbrRlsParser.test(url)) { // MusicBrainz
		var entities = [
		  'aliases', 'annotation', 'artist-credits', 'artists', 'collections', 'discids', 'genres',
		  'isrcs', 'labels', 'media', 'ratings', 'recordings', 'release-groups', 'tags', 'url-rels',
		];
		return queryMusicBrainzAPI('release/' + RegExp.$1, { inc: entities.join('+') }).then(function(release) {
		  if (release.error) return Promise.reject(release.error);
		  if (prefs.diag_mode) console.debug('MusicBrainz release metadata received:', release);
		  if (release.id) identifiers.MBID = release.id;
		  if (release.barcode) identifiers.BARCODE = release.barcode;
		  if (release.asin) identifiers.ASIN = release.asin;
		  if (release['release-group']['primary-type']) identifiers.RELEASETYPE = release['release-group']['primary-type'];
		  artist = Array.isArray(release['artist-credit']) ? release['artist-credit'].map(artist => artist.name) : [];
		  isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		  if (Array.isArray(release.genres)) genres = release.genres.map(genre => genre.name);
		  if (Array.isArray(release.tags)) Array.prototype.push.apply(genres, release.tags.map(tag => tag.name));
		  if (genres.length <= 0) {
			if (Array.isArray(release['release-group'].genres)) {
			  Array.prototype.push.apply(genres, release['release-group'].genres.map(tag => tag.name));
			}
			if (Array.isArray(release['release-group'].tags)) {
			  Array.prototype.push.apply(genres, release['release-group'].tags.map(tag => tag.name));
			}
		  }
		  label = release['label-info'].map(label => label.label.name);
		  catalogue = release['label-info'].map(label => label['catalog-number']);
		  if (release['release-group'].status && !/^(?:Official)$/i.test(release['release-group'].status))
			addMessage('Not an official release ('.concat(release['release-group'].status, ')'), 'warning');
		  release.media.forEach(function(medium, ndx) {
			medium.tracks.forEach(function(track, ndx) {
			  trackIdentifiers = { TRACK_ID: track.id };
			  if (Array.isArray(track['artist-credit'])) {
				trackArtist = track['artist-credit'].map(artist => artist.name);
				trackArtist = trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist));
			  } else trackArtist = false;
			  tracks.push({
				artist: isVA ? VA : undefined,
				artists: !isVA ? artist : undefined,
				album: /*release['release-group'].title || */release.title,
				album_year: extractYear(release['release-group']['first-release-date']),
				release_date: release.date,
				genre: genres.join('; '),
				label: label.filter(label => label).join(' / '),
				catalog: catalogue.filter(catno => catno).join(' / '),
				media: medium.format,
				discnumber: medium.position,
				discsubtitle: medium.title,
				totaldiscs: release.media.length,
				tracknumber: track.number,
				title: track.title,
				track_artist: trackArtist ?
					track['artist-credit'].map(artist => artist.name.concat(artist.joinphrase)).join('') : undefined,
				duration: track.length != null ? track.length / 1000 : undefined,
				//country: release.country,
				description: release.annotation,
				identifiers: mergeIds(),
			  });
			});
		  });
		  return tracks;
		});
	  } else if (url.toLowerCase().includes('vgmdb.net/')) return globalFetch(url).then(function(response) {
		if (/\/album\/(\d+)(?=\/|$)/i.test(response.finalUrl)) identifiers.VGMDB_ID = RegExp.$1;
		if ((ref = response.document.querySelector('h1 > span.albumtitle[style="display:inline"]')) != null) {
		  album = ref.innerText.trim();
		  if (ref.lang == 'en'
				&& (ref = response.document.querySelector('div > span.albumtitle[style="display:inline"]')) != null
				&& ref.firstChild != null && ref.firstChild.nodeType == Node.TEXT_NODE)
			album += ' ('.concat(ref.firstChild.wholeText.trim(), ')');
		}
		composer = [];
		response.document.querySelectorAll('table#album_infobit_large > tbody > tr > td > span.label > b').forEach(function(key) {
		  var value = key.parentNode.parentNode.nextElementSibling;
		  switch (key.innerText.trim().toLowerCase()) {
			case 'catalog number':
			  catalogue = value.textContent.trim().replace(/\s*\([^\(\)]+\)$/, '');
			  break;
			case 'release date':
			  if (value.firstElementChild != null) releaseDate = value.firstElementChild.innerText.trim();
			  break;
			case 'media format':
			  media = value.textContent.trim();
			  break;
			case 'classification':
			  genres = value.textContent.trim().split(/\s*,\*/);
			  break;
			case 'published by':
			  label = Array.from(value.querySelectorAll('a > span.productname:first-of-type'))
				.map(span => span.innerText.trim()).join(' / ');
			  break;
			case 'composed by':
			case 'lyrics by':
			  getArtists(value).forEach(artist => { composer.pushUniqueCaseless(artist) });
			  break;
			case 'performed by':
			  artist = getArtists(value);
			  break;
			case 'arranged by':
			  var arrangers = getArtists(value);
			  break;
		  }
		});
		if (!artist || artist.length <= 0) artist = composer;
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		genres.pushUniqueCaseless('Soundtrack');
		response.document.querySelectorAll('td#rightcolumn > div > div > div > b.label').forEach(function(key) {
		  var value = key.parentNode.lastChild;
		  if (key.innerText.toLowerCase() == 'category' && value != null)
			genres.pushUniqueCaseless(value.textContent.trim());
		});
		getDescription(response, 'div#notes', false);
		if ((ref = response.document.querySelector('div#coverart')) != null
		   && /\burl\s*\(\"(.*)"\)/i.test(ref.style['background-image'])) imgUrl = RegExp.$1;
		response.document.querySelectorAll('div#tracklist > span > span > b').forEach(function(node) {
		  discSubtitle = node.innerText.trim();
		  guessDiscNumber();
		  node = node.parentNode;
		  while (node != null && node.nodeName != 'TABLE') node = node.nextElementSibling;
		  if (node != null) addVolume(node);
		});
		var tl = Array.from(response.document.querySelectorAll('ul#tlnav > li > a'));
		if (tl.length <= 1) return tracks;
		if ((i = tracks.length / tl.length) != Math.floor(i)) {
		  console.warn('Unexpected vgmdb.net tracklist length:', i, tracks);
		  return tracks;
		}
		let enIndex = tl.findIndex(l => /^English\b/i.test(l.innerText.trim()));
		if (enIndex < 0) enIndex = tl.findIndex(l => /^Romaji\b/i.test(l.innerText.trim()));
		if (enIndex < 0) return tracks.slice(0, i);
		let jpIndex = tl.findIndex(l => /^Japanese\b/i.test(l.innerText.trim()));
		if (jpIndex < 0) jpIndex = enIndex > 0 ? 0 : 1;
		return tracks.slice(enIndex * i, (enIndex + 1) * i).map(function(track, ndx) {
		  const rx = /^(.+?)(?:\s+\(([^\(\)]+)\))?$/;
		  if (!track.title) track.title = tracks[jpIndex * i + ndx].title;
		  else if ((jpTitle = tracks[jpIndex * i + ndx].title) != track.title) {
			track.title += ' (';
			var enTitle = rx.exec(track.title), jpTitle = rx.exec(jpTitle);
			if (jpTitle[1] != enTitle[1]) {
			  track.title += jpTitle[1];
			  if (jpTitle[2] && jpTitle[2] != enTitle[2]) track.title += ' ('.concat(jpTitle[2], ')');
			} else track.title += jpTitle[2];
			track.title += ')';
		  }
		  return track;
		});

		function addVolume(node) {
		  Array.prototype.push.apply(tracks, Array.from(node.querySelectorAll('tbody > tr')).map(tr => ({
			artist: isVA ? VA : undefined,
			artists: !isVA ? artist : undefined,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			catalog: catalogue,
			media: media,
			genre: genres.join('; '),
			discnumber: discNumber,
			//totaldiscs: totalDiscs,
			discsubtitle: discSubtitle,
			tracknumber: (ref = tr.querySelector('span.label')) != null ? parseInt(ref.innerText) : undefined,
			//totaltracks: trs.length,
			title: tr.children[1].innerText.trim(),
			//track_artist: joinArtists(trackArtist),
			composers: composer,
			duration: (ref = tr.querySelector('span.time')) != null ? timeStringToTime(ref.innerText) : undefined,
			url: !identifiers.VGMDB_ID ? response.finalUrl : undefined,
			description: description,
			identifiers: mergeIds(),
			cover_url: imgUrl,
		  })));
		}

		function getArtists(node) {
		  var artists = [];
		  node.childNodes.forEach(function(node) {
			switch (node.nodeType) {
			  case Node.ELEMENT_NODE:
				if ((i = node.querySelectorAll('span.artistname')).length > 0) {
				  var artist = i[0].innerText.trim();
				  if (i.length > 1 && i[0].lang == 'en') artist += ' ('.concat(i[1].innerText.trim(), ')');
				} else artist = node.innerText.trim();
				if (artist) artists.push(artist);
				break;
			  case Node.TEXT_NODE:
				artist = node.wholeText.trim().replace(/^\s*,\s*|\s*,\s*$/g, '');
				if (/^[\(\)]+$/.test(artist)) return;
				if (artist) Array.prototype.push.apply(artists, artist.split(/\s*,\s*/));
				break;
			}
		  });
		  return artists;
		}
	  }); else if (url.toLowerCase().includes('tidal.com/')) {
		if (!(/\/album\/(\d+)(?:\/|$)/i.test(url) && !/\b(?:albumId)=(\d+)\b/i.test(url)))
			return Promise.reject('Fetching from this page is not supported');
		return queryTidalAPI('album', { albumId: RegExp.$1 }).then(function(album) {
		  var albumHeader = findModule('ALBUM_HEADER');
		  if (albumHeader == null) return Promise.reject('Album header not found');
		  var albumItems = findModule('ALBUM_ITEMS');
		  if (albumItems == null) return Promise.reject('Album items not found');
		  artist = albumHeader.album.artists.map(artist => artist.name);
		  isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0].name);
		  identifiers.TIDAL_ID = albumHeader.album.id;
		  identifiers.RELEASETYPE = albumHeader.album.type;
		  if (/^(?:(?:\([PC]\)|©|℗)\s+)?(?:(\d{4})\s+)?(.*)/.test(albumHeader.album.copyright)) {
			//if (RegExp.$1) albumYear = parseInt(RegExp.$1);
			label = RegExp.$2;
		  }
		  var channels;
		  description = albumHeader.description;
		  if (albumHeader.review.text) {
			if (description) description += '\n\n';
			if (!albumHeader.review.source) description += '[b]Album Review[/b]\n\n';
			description += '[quote';
			if (albumHeader.review.source) description += '=Album review from '.concat(albumHeader.review.source);
			description += ']'.concat(albumHeader.review.text, '[/quote]');
			description = description
			  .replace(/\[wimpLink\s+artistId="(\d+)"\]/g, '[url=https://listen.tidal.com/artist/$1]')
			  .replace(/\[wimpLink\s+albumId="(\d+)"\]/g, '[url=https://listen.tidal.com/album/$1]')
			  .replace(/\[\/wimpLink\]/g, '[/url]');
		  }
		  if (Array.isArray(albumHeader.credits.items) && albumHeader.credits.items.length > 0) {
			let ac = '';
			albumHeader.credits.items.forEach(function(credit) {
			  if (/^Primary Artist$/i.test(credit.type)) return;
// 			  if (/^Record label$/i.test(credit.type)) {
// 				label = credit.contributors.map(contributor => contributor.name).join(' / ');
// 				return;
// 			  }
			  ac += '\n'.concat(credit.type, ' – ', joinArtists(credit.contributors.map(contributor =>
				!contributor.id ? contributor.name :
					'[url=https://listen.tidal.com/artist/'.concat(contributor.id, ']', contributor.name, '[/url]'))));
			});
			if (ac.length > 0) {
			  if (description) {
				if (!albumHeader.review.text) description += '\n';
				description += '\n';
			  }
			  description += '[b]Additional Credits[/b]\n'.concat(ac);
			}
		  }
		  if (albumHeader.album.cover)
			imgUrl = 'https://resources.tidal.com/images/'.concat(albumHeader.album.cover.replace(/-/g, '/'), '/1280x1280.jpg');
		  return albumItems.pagedList.items.map(function(track, ndx) {
			if (track.type != 'track') return;
			trackIdentifiers = {
			  TRACK_ID: track.item.id,
			  EXPLICIT: Number(track.item.explicit),
			};
			trackArtist = track.item.artists.map(artist => artist.name);
			channels = undefined;
			track.item.audioModes.forEach(function(audioMode) {
			  switch (audioMode.toLowerCase()) {
				case 'stereo': channels = 2; break;
				default: if (/\b(\d+)\.(\d+)\b/.test(audioMode)) channels = parseInt(RegExp.$1) + parseInt(RegExp.$2);
			  }
			});
			return {
			  artist: isVA ? VA : undefined,
			  artists: !isVA ? artist : undefined,
			  album: albumHeader.album.title,
			  album_year: albumYear,
			  release_date: albumHeader.album.releaseDate,
			  label: label,
			  media: media,
			  discnumber: track.item.volumeNumber,
			  totaldiscs: albumHeader.album.numberOfVolumes,
			  //discsubtitle: discSubtitle,
			  tracknumber: track.item.trackNumber,
			  totaltracks: albumHeader.album.numberOfTracks,
			  title: track.item.title,
			  track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
			  encoding: ['HI_RES', 'LOSSLESS'].includes(track.item.audioQuality) ? 'lossless' : undefined,
			  duration: track.item.duration,
			  channels: channels,
			  description: description,
			  url: !identifiers.TIDAL_ID ? albumHeader.album.url : undefined,
			  identifiers: mergeIds(),
			  cover_url: imgUrl,
			  ag: track.replayGain ? track.replayGain.toString().concat(' dB') : undefined,
			};
		  });

		  function findModule(type) {
			for (var row of album.rows) {
			  var result = row.modules.find(module => module.type == type);
			  if (result != undefined) return result;
			}
			return null;
		  }
		});
	  } else if (url.toLowerCase().includes('ototoy.jp/')) return globalFetch(url).then(function(response) {
		if (/\/p\/(\d+)(?=\/|\?|$)/i.test(response.finalUrl)) identifiers.OTOTOY_ID = parseInt(RegExp.$1);
		artist = Array.from(response.document.querySelectorAll('span.album-artist > *')).map(node => node.textContent.trim());
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		if ((ref = response.document.querySelector('h1.album-title')) != null) album = ref.textContent.trim();
		if ((ref = response.document.querySelector('p.hqd-logo > span')) != null
			&& /Audio Format:\s*(\d+)\s*bit\s*\/\s*(\d+(?:\.\d+)?)\s*kHz\b/i.test(ref.textContent.trim())) {
		  bd = parseInt(RegExp.$1);
		  sr = parseFloat(RegExp.$2) * 1000;
		}
		if ((ref = response.document.querySelector('p.hqd-logo > a.lossless')) != null) encoding = 'lossless';
		if ((ref = response.document.querySelector('p.release-day')) != null && /\b(\d{4})-(\d{2})-(\d{2})\b/.test(ref.textContent))
		  releaseDate = RegExp.lastMatch;
		label = Array.from(response.document.querySelectorAll('p.label-name > a')).map(a => a.textContent.trim()).join(' / ');
		if ((ref = response.document.querySelector('p.catalog-id')) != null && /\b(?:Catalog\s+number):\s*(.*)$/i.test(ref.textContent.trim()))
		  catalogue = RegExp.$1;
		genres = Array.from(response.document.querySelectorAll('ul.tag-cloud > li > a.oty-btn-tag'))
		  .map(a => a.textContent.trim()).filter(genre => genre.length > 0);
		getDescription(response, 'div.album-addendum', false);
		if ((ref = response.document.querySelector('div#jacket-full-wrapper > img')) != null) imgUrl = ref.dataset.src || ref.src;
		trs = response.document.querySelectorAll('table#tracklist > tbody > tr[class^="bg"]');
		return Array.from(trs).map(function(tr, ndx) {
		  trackIdentifiers = {};
		  title = (ref = tr.querySelector('td.item > span[id^="title-"]')) != null ? ref.textContent.trim() : undefined;
		  if (ref != null && /^title-(\d+)$/.test(ref.id)) trackIdentifiers.TRACK_ID = parseInt(RegExp.$1);
		  trackArtist = Array.from(tr.querySelectorAll('td.item > span > a.artist')).map(a => a.textContent.trim());
		  return {
			artist: isVA ? VA : undefined,
			artists: !isVA ? artist : undefined,
			album: album,
			album_year: extractYear(releaseDate),
			release_date: releaseDate,
			label: label,
			catalog: catalogue,
			media: media,
			genre: genres.join('; '),
			discnumber: discNumber,
			tracknumber: ndx + 1,
			totaltracks: trs.length,
			sr: sr || undefined,
			bd: bd,
			encoding: encoding,
			title: title,
			track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
			duration: (ref = tr.querySelector(':scope > td.item:nth-of-type(3)')) != null ? timeStringToTime(ref.textContent) : undefined,
			description: description,
			url: !identifiers.OTOTOY_ID ? response.finalUrl : undefined,
			cover_url: imgUrl,
			identifiers: mergeIds(),
		  };
		});
	  }); else if (url.toLowerCase().includes('music.yandex.ru/') && (/\/album\/(\d+)\b/i.test(url)
			|| /\b(?:album)=(\d+)\b/i.test(url))) return globalFetch('https://music.yandex.ru/handlers/album.jsx?album=' + RegExp.$1, { responseType: 'json' }).then(function(response) {
		if (prefs.diag_mode) console.debug('Yandex Music metadata received:', response.response);
		if (response.response.metaType && response.response.metaType != 'music') throw 'Not a music release';
		identifiers.YANDEX_ID = response.response.id;
		if (response.response.type) identifiers.RELEASETYPE = response.response.type;
		artist = response.response.artists.filter(artist => !artist.composer).map(artist => artist.name);
		composer = response.response.artists.filter(artist => artist.composer).map(artist => artist.name);
		isVA = response.response.artists.length <= 0
			|| response.response.artists.length == 1 && response.response.artists.some(artist => artist.various);
		album = response.response.title;
		if (response.response.version) album += ' ('.concat(response.response.version, ')');
		response.response.volumes.forEach(function(volume, discNumber) {
		  Array.prototype.push.apply(tracks, volume.filter(track => track.type == 'music').map(function(track, trackNumber) {
			trackIdentifiers = { TRACK_ID: parseInt(/*track.realId || */track.id) };
			title = track.title;
			if (track.version) title += ' ('.concat(track.version, ')');
			trackArtist = track.artists.filter(artist => !artist.composer).map(artist => artist.name);
			var trackComposer = track.artists.filter(artist => artist.composer).map(artist => artist.name);
			return {
			  artist: isVA ? VA : undefined,
			  artists: !isVA ? artist : undefined,
			  album: album,
			  album_year: response.response.year,
			  release_date: response.response.releaseDate.replace(/T.*$/, ''),
			  label: response.response.labels.map(label => label.name).join(' / '),
			  media: media,
			  genre: response.response.genre,
			  tracknumber: trackNumber + 1,
			  totaltracks: response.response.trackCount,
			  composers: trackComposer.length > 0 ? trackComposer : composer,
			  discnumber: discNumber + 1,
			  totaldiscs: response.response.volumes.length,
			  title: title,
			  track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist)) ? trackArtist : undefined,
			  duration: track.durationMs / 1000,
			  cover_url: 'https://'.concat(response.response.coverUri.replace('/%%', '/m1000x1000')),
			  identifiers: mergeIds(),
			  tg: track.normalization ? track.normalization.gain.toString().concat(' dB') : undefined,
			  tp: track.normalization ? track.normalization.peak.toString().concat(' dB') : undefined,
			};
		  }));
		});
		return tracks;
	  }); else if (url.toLowerCase().includes('mora.jp/') ) return loadMoraMetadata(url).then(function(packageMeta) {
		if (prefs.diag_mode) console.debug('Mora.jp metadata loaded:', packageMeta);
		if ([7].includes(packageMeta.mediaType)) throw 'Not music release (' + packageMeta.mediaType + ')';
		artist = fmtKanaProp(packageMeta, 'artistName');
		isVA = vaParser.test(artist);
		album = fmtKanaProp(packageMeta, 'title');
		if (packageMeta.bitPerSample) bd = parseInt(packageMeta.bitPerSample);
		if (packageMeta.samplingFreq) sr = parseInt(packageMeta.samplingFreq);
		if (packageMeta.channelConf) channels = parseInt(packageMeta.channelConf);
		if (packageMeta.materialNo) identifiers.MORA_ID = parseInt(packageMeta.materialNo);
		if (packageMeta.msin) identifiers.MSIN = packageMeta.msin;
		if (packageMeta.distPartNo) identifiers.DISTPARTNO = packageMeta.distPartNo;
		if (packageMeta.fullsizeimage) imgUrl = packageMeta.packageUrl + packageMeta.fullsizeimage;
		return packageMeta.trackList.map(function(track) {
		  trackIdentifiers = { TACK_ID: track.musicId, MSIN: track.msin, DISTPARTNO: track.distPartNo };
		  if (track.labelId) trackIdentifiers.LABEL_ID = track.labelId;
		  trackArtist = fmtKanaProp(track, 'artistName');
		  composer = fmtKanaProp(track, 'composer');
		  var trackLyricist = fmtKanaProp(track, 'lyrics');
		  if (trackLyricist) if (composer) composer += ' / ' + trackLyricist; else composer = trackLyricist;
		  switch (track.mediaFormatNo) {
			case 10: format = 'AAC'; encoding = 'lossy'; var codecProfile = 'AAC-LC'; bitrate = 320; break;
			//case 11: format = 'FLAC'; encoding = 'lossless'; codecProfile = undefined; bitrate = undefined; break;
			case 12: format = 'FLAC'; encoding = 'lossless'; codecProfile = undefined; bitrate = undefined; break;
			case 13: format = 'DSD'; encoding = 'lossless'; codecProfile = undefined; bitrate = undefined; break;
			default: format = undefined; encoding = undefined; codecProfile = undefined; bitrate = undefined;
		  }
		  return {
			artist: isVA ? VA : artist,
			album: album,
			//album_year: extractYear(releaseDate),
			release_date: packageMeta.dispStartDate || packageMeta.dispStartDateStr || packageMeta.startDate,
			label: packageMeta.labelcompanyname || packageMeta.displayLabelname || packageMeta.labelname,
			catalog: packageMeta.cdPartNo || packageMeta.packageId || packageMeta.distPartNo,
			media: media,
			genre: genres.join('; '),
			codec: format,
			codec_profile: codecProfile,
			encoding: encoding,
			bitrate: /*track.bitPerSample * 1000 || */bitrate,
			bd: parseInt(track.bitPerSample) || bd,
			sr: parseInt(track.samplingFreq) || sr,
			channels: parseInt(track.channelConf) || channels,
			tracknumber: track.trackNo,
			totaltracks: packageMeta.trackList.length,
			composer: composer,
			producer: fmtKanaProp(track, 'producer'),
			arranger: fmtKanaProp(track, 'arranger'),
			title: fmtKanaProp(track, 'title'),
			track_artist: trackArtist && (isVA || trackArtist.toLowerCase() != artist.toLowerCase()) ? trackArtist : undefined,
			duration: track.duration,
			description: packageMeta.metaDescription,
			url: packageMeta.webUrl,
			cover_url: imgUrl,
			identifiers: mergeIds(),
			master: packageMeta.master,
		  };
		});

		function fmtKanaProp(obj, propName) {
		  var result = (obj[propName] || '').trim(), kana = (obj[propName.concat('Kana')] || '').trim();
		  if (kana && prefs.use_kana) if (result) result += ' ('.concat(kana, ')'); else result = kana;
		  return result || undefined;
		}
	  }); else if (url.toLowerCase().includes('allmusic.com/album/')) return globalFetch(url.replace(/\b(m[wr]\d{10})\b.+$/, '$1')).then(function(response) {
		ref = response.document.querySelector('section.main-album a.album-title');
		var mainAlbum = (ref != null ? globalFetch(ref.href).then((response, ref) => ({
		  artist: Array.from(response.document.querySelectorAll('h2[class$="album-artist"] > span[itemprop="name"]'))
		  	.map(span => span.textContent.trim()),
		  album: (ref = response.document.querySelector('h1.album-title')) != null ? ref.textContent.trim() : undefined,
		  albumYear: (ref = response.document.querySelector('div.release-date > span')) != null ?
		  	new Date(ref.textContent).getFullYear() || parseInt(ref.textContent) : undefined,
		  genres: Array.from(response.document.querySelectorAll('div.genre a')).map(a => a.textContent.trim()),
		  styles: Array.from(response.document.querySelectorAll('div.styles a')).map(a => a.textContent.trim()),
		  coverUrl: (ref = response.document.querySelector('meta[property="og:image"]')) != null ?
		  	ref.content.replace(/\bf=\d+\b/, 'f=0') : undefined,
		  id: /\b(mw\d{10})\b/.test(response.finalUrl) && RegExp.$1 || undefined,
		})) : Promise.reject(null)).catch(reason => ({}));
		var _credits = { mainArtists: [], featured: [], credits: {} };
		var credits = globalFetch(response.finalUrl.concat('/credits')).then(function(response) {
		  response.document.querySelectorAll('section.credits > table > tbody > tr').forEach(function(tr) {
			var name = tr.children[0].textContent.trim(), role = tr.children[1].textContent.trim();
			if (role == 'Primary Artist') _credits.mainArtists.push(name);
				else if (role == 'Featured Artist') _credits.featured.push(name);
					else _credits.credits[name] = role;
		  });
		  return _credits;
		}).catch(reason => _credits);
		if (/\b(m[wr]\d{10})\b/.test(response.finalUrl)) identifiers.ALLMUSIC_ID = RegExp.$1;
		artist = Array.from(response.document.querySelectorAll('h2[class$="-artist"] > span[itemprop="name"]'))
		  .map(span => span.textContent.trim());
		isVA = artist.length <= 0 || artist.length == 1 && vaParser.test(artist[0]);
		album = (ref = response.document.querySelector('h1.release-title')
			|| response.document.querySelector('h1.album-title')) != null ? ref.textContent.trim() : undefined;
		albumYear = (ref = response.document.querySelector('div.year')) != null ? parseInt(ref.textContent) : undefined;
		ref = response.document.querySelector('div.release-date > span');
		if (identifiers.ALLMUSIC_ID && identifiers.ALLMUSIC_ID.startsWith('mr')) {
		  releaseDate = ref.textContent.trim();
		} else if (identifiers.ALLMUSIC_ID && identifiers.ALLMUSIC_ID.startsWith('mw')) {
		  albumYear = new Date(ref.textContent).getFullYear() || parseInt(ref.textContent) || albumYear;
		}
		label = Array.from(response.document.querySelectorAll('div.label a')).map(a => a.textContent.trim()).join(' / ');
		catalogue = (ref = response.document.querySelector('div.catalog-number > span')) != null ? ref.textContent.trim() : undefined;
		if ((ref = response.document.querySelector('div.format > span')) != null) media = ref.textContent.trim();
		genres = Array.from(response.document.querySelectorAll('div.genre a')).map(a => a.textContent.trim());
		var styles = Array.from(response.document.querySelectorAll('div.styles a')).map(a => a.textContent.trim());
		getDescription(response, 'section.review', false);
		var releaseInfo = [];
		if ((ref = response.document.querySelector('div.recording-date > div')) != null)
		  releaseInfo.push('Recording date: '.concat(ref.textContent.trim()));
		var locations = Array.from(response.document.querySelectorAll('div.recording-location > ul > li')).map(li => li.textContent.trim());
		if (locations.length > 0) releaseInfo.push('Recording location: '.concat(locations.join(' / ')));
		locations = Array.from(response.document.querySelectorAll('div.release-info > ul > li')).map(li => li.textContent.trim());
		if (locations.length > 0) releaseInfo.push('Release info: '.concat(locations.join(', ')));
		if (releaseInfo.length > 0) {
		  if (description) description += '\n\n';
		  description += releaseInfo.join('\n');
		}
		imgUrl = (ref = response.document.querySelector('meta[property="og:image"]')) != null ?
		  ref.content.replace(/\bf=\d+\b/, 'f=0') : undefined;
		trs = response.document.querySelectorAll('section.track-listing table > tbody > tr.track');
		return Promise.all([mainAlbum, credits]).then(function(workers) {
		  if (Object.keys(workers[1].credits).length > 0) {
			if (description) description += '\n\n';
			description = description.concat('[b]Credits:[/b]\n', Object.keys(workers[1].credits)
			  .map(artist => '\n'.concat(artist, ' - ', workers[1].credits[artist])));
		  }
		  return Array.from(trs).map(function(tr, ndx) {
			trackArtist = Array.from(tr.querySelectorAll('td.performer div.primary > a')).map(a => a.textContent.trim());
			var trackGuests = Array.from(tr.querySelectorAll('td.performer div.featuring > a')).map(a => a.textContent.trim());
			if ((ref = tr.querySelector('div.title > a')) != null && ref.dataset.tooltip) try {
			  trackIdentifiers = { TRACK_ID: JSON.parse(ref.dataset.tooltip).id };
			} catch(e) { trackIdentifiers = {} }
			return {
			  artist: isVA ? VA : undefined,
			  artists: !isVA ? artist : undefined,
			  album: album,
			  release_date: releaseDate,
			  album_year: workers[0].albumYear || albumYear,
			  genre: (workers[0].genres || []).concat((workers[0].styles || []), genres, styles).join('; '),
			  label: label,
			  catalog: catalogue,
			  media: media,
			  discnumber: (ref = tr.parentNode.parentNode.parentNode.querySelector('h3')) != null
			  	&& /\b(?:Disc)\s+(\d+)\b/i.test(ref.textContent.trim()) ? parseInt(RegExp.$1) : undefined,
			  discsubtitle: (ref = tr.parentNode.querySelector('tr.performance-title')) != null ?
			  	ref.textContent.trim() : undefined,
			  tracknumber: (ref = tr.querySelector('td.tracknum')) != null ? ref.textContent.trim() : undefined,
			  totaltracks: trs.length,
			  title: (ref = tr.querySelector('div.title')) != null ? ref.textContent.trim() : undefined,
			  track_artists: trackArtist.length > 0 && (isVA || !trackArtist.equalCaselessTo(artist) || trackGuests.length > 0) ?
			  	trackArtist.concat(trackGuests) : undefined,
			  composers: Array.from(tr.querySelectorAll('div.composer > *')).map(node => node.textContent.trim()) || undefined,
			  duration: (ref = tr.querySelector('td.time')) != null && timeStringToTime(ref.textContent) || undefined,
			  description: description || undefined,
			  url: !identifiers.ALLMUSIC_ID ?
			  	(ref = tr.querySelector('meta[property="og:url"]')) != null ? ref.content : response.finalUrl : undefined,
			  cover_url: workers[0].coverUrl || imgUrl,
			  identifiers: mergeIds(),
			};
		  });
		});
	  });
	  if (!weak) clipBoard.value = '';
	  return Promise.reject(new URL(url).hostname + ' not supported');

	  function mergeIds() {
		var r = Object.assign({}, identifiers, trackIdentifiers);
		trackIdentifiers = {};
		return r;
	  }

	  function getDescription(response, selector, quote = false) {
		description = [];
		response.document.querySelectorAll(selector).forEach(function(node) {
		  var p = html2php(node, response.finalUrl).trim();
		  if (p) description.push(p);
		});
		description = description.join('\n\n').collapseGaps();
		if (quote && description.length > 0 && !description.includes('[quote]')) {
		  description = '[quote]' + description + '[/quote]';
		}
	  }

	  function durationFromMeta(elem) {
		var m = elem.querySelector('meta[itemprop="duration"]');
		if (m == null) return undefined;
		if (/^PT?(?:(?:(\d+)H)?(\d+)M)?(\d+)S$/i.test(m.content))
		  return (parseInt(RegExp.$1) || 0) * 60**2 + (parseInt(RegExp.$2) || 0) * 60 + (parseInt(RegExp.$3) || 0);
		m = timeStringToTime(m.content);
		return m != null ? m : undefined;
	  }

	  function guessDiscNumber() {
		if (discParser.test(discSubtitle)) {
		  discSubtitle = undefined;
		  discNumber = parseInt(RegExp.$1);
		}
	  }
	} // fetchOnline_Music

	function parseLastFm(album) {
	  if (typeof album != 'object') return Promise.reject('invalid object')
	  var identifiers = {}, description = [];
	  if (album.id) identifiers.LASTFM_ID = album.id;
	  if (album.mbid) identifiers.MBID = album.mbid;
	  if (album.wiki && album.wiki.summary) description.push(album.wiki.summary);
	  if (album.wiki && album.wiki.content) description.push(album.wiki.content);
	  var genres = album.tags.tag.map(tag => tag.name);
	  description = description.join('\n\n');
	  var imgUrl = ['mega', 'extralarge', '', 'large', 'medium', 'small'].reduce(function(acc, size) {
		return acc || album.image.find(image => image.size == size && urlParser.test(image['#text']));
	  }, undefined);
	  if (imgUrl) imgUrl = imgUrl['#text'].replace(/\/\d+(?:x\d+|s)\//i, '/');
	  return album.tracks.track.map((track, ndx) => ({
		artist: album.artist,
		album: album.name,
		genre: genres.join('; ') || undefined,
		title: track.name,
		tracknumber: ndx + 1,
		track_artist: track.artist.name != album.artist ? track.artist.name : undefined,
		duration: parseFloat(track.duration) || undefined,
		url: album.url,
		description: description || undefined,
		identifiers: identifiers,
		cover_url: imgUrl,
	  }));
	}

	function joinArtists(arr, decorator = artist => artist) {
	  if (!Array.isArray(arr)) return null;
	  if (arr.some(artist => artist.includes('&'))) return arr.map(decorator).join(', ');
	  if (arr.length < 3) return arr.map(decorator).join(' & ');
	  return arr.slice(0, -1).map(decorator).join(', ') + ' & ' + decorator(arr.slice(-1).pop());
	}

	function loadMoraMetadata(webUrl) {
	  return /^(?:https?):\/\/(?:\w+\.)*mora\.jp\/package\//i.test(webUrl) ? globalFetch(webUrl).then(function(response1) {
		var appArguments = response1.document.querySelector('meta[name="msApplication-Arguments"][content]');
		if (appArguments == null) return Promise.reject('Mora.jp: unexpected page format');
		appArguments = JSON.parse(appArguments.content);
		var materialNo = appArguments.materialNo.toString().padStart(10, '0'), offset = 0;
		var packageUrl = 'https://cf.mora.jp/contents/'.concat([
		  appArguments.type, appArguments.mountPoint, appArguments.labelId,
		].concat([4, 3, 3].map(length => materialNo.slice(offset, offset += length))).join('/'), '/');
		return globalFetch(packageUrl.concat('packageMeta.jsonp'), { responseType: 'text' }).then(function(response2) {
		  return /^\s*\w+\(\s*(\{[\S\s]+\})\s*\);\s*$/.test(response2.responseText) ? Object.assign(JSON.parse(RegExp.$1), {
			mountPoint: appArguments.mountPoint,
			webUrl: response1.finalUrl.replace(/[\?\#].*$/, ''),
		  }) : Promise.reject('Mora.jp: Unexpected package meta format');
		});
	  }) : Promise.reject('Not mora.jp site URL');
	}

	function splitArtists(str, parsers = multiArtistParsers) {
	  var result = [str];
	  parsers.forEach(function(parser) {
		for (var i = result.length; i > 0; --i) {
		  var j = result[i - 1].split(parser).map(strip);
		  if (j.length > 1 && j.every(twoOrMore) && !j.some(artist => pseudoArtistParsers.some(rx => rx.test(artist)))
			  && !getSiteArtist(result[i - 1])) result.splice(i - 1, 1, ...j);
		}
	  });
	  return result;
	}

	function twoOrMore(artist) { return artist.length >= 2 && !pseudoArtistParsers.some(rx => rx.test(artist)) };
	function looksLikeTrueName(artist, index = 0) {
	  return twoOrMore(artist)
		&& (index == 0 || !/^(?:his\b|her\b|Friends$|Strings$)/i.test(artist))
		&& artist.split(/\s+/).length >= 2
		&& !pseudoArtistParsers.some(rx => rx.test(artist)) || typeof getSiteArtist(artist) == 'object';
	}

	function strip(art) {
	  return artistStrips.reduce(function(acc, rx, ndx) {
		return ndx != 1 || rx.test(acc) && !notMonospaced(RegExp.$1) ? acc.replace(rx, '') : acc;
	  }, art);
	}

	function getSiteArtist(artist) {
	  //if (isOPS) return undefined;
	  if (!artist || notSiteArtistsCache.includesCaseless(artist)) return null;
	  var key = Object.keys(siteArtistsCache).find(it => it.toLowerCase() == artist.toLowerCase());
	  if (key) return siteArtistsCache[key];
	  var now = Date.now();
	  try { var apiTimeFrame = JSON.parse(window.localStorage[siteApiTimeframeStorageKey]) } catch(e) { apiTimeFrame = {} }
	  if (!apiTimeFrame.timeStamp || now > apiTimeFrame.timeStamp + gazelleApiFrame) {
		apiTimeFrame.timeStamp = now;
		apiTimeFrame.requestCounter = 1;
	  } else ++apiTimeFrame.requestCounter;
	  window.localStorage[siteApiTimeframeStorageKey] = JSON.stringify(apiTimeFrame);
	  if (apiTimeFrame.requestCounter > 5) {
		console.debug('getSiteArtist() request exceeding AJAX API time frame: /ajax.php?action=artist&artistname="' +
		  artist + '" (' + apiTimeFrame.requestCounter + ')');
		if (prefs.messages_verbosity >= 2) addMessage('AJAX API request exceeding time frame: artistname="' +
		  artist + '" (' + apiTimeFrame.requestCounter + ')', 'notice');
		++ajaxRejects;
		return undefined;
	  }
	  try {
		var requestUrl = '/ajax.php?action=artist&artistname='.concat(encodeURIComponent(artist));
		xhr.open('GET', requestUrl, false);
		if (isRED && prefs.redacted_api_key) xhr.setRequestHeader('Authorization', prefs.redacted_api_key);
		xhr.send();
		if (xhr.status == 404) {
		  notSiteArtistsCache.pushUniqueCaseless(artist);
		  return null;
		}
		if (xhr.readyState != XMLHttpRequest.DONE || xhr.status < 200 || xhr.status >= 400) {
		  console.log('getSiteArtist("' + artist + '") status:', xhr.status, 'url:', document.location.origin.concat(requestUrl));
		  return undefined; // error
		}
		let response = JSON.parse(xhr.responseText);
		if (response.status != 'success') {
		  notSiteArtistsCache.pushUniqueCaseless(artist);
		  return null;
		}
		return (siteArtistsCache[artist] = response.response);
	  } catch(e) {
		console.error('UA::getSiteArtist("' + artist + '"):', e, xhr);
		return undefined;
	  }
	}
  } // fillFromText_Music

  function fillFromText_Apps(weak = false) {
	if (messages != null) messages.parentNode.removeChild(messages);
	if (!urlParser.test(clipBoard.value)) {
	  addMessage('valid URL accepted for this category', 'critical');
	  return false;
	}
	sourceUrl = RegExp.$1;
	var description, tags = new TagManager();
	if (sourceUrl.toLowerCase().includes('://sanet')) return globalFetch(sourceUrl).then(function(response) {
	  i = response.document.querySelector('h1.item_title > span');
	  var title = i == null ? undefined : i.textContent
		.replace(/\s+\((?:x|ia|em)(?:64)\)/ig, ' (64-bit)')
		.replace(/\s+\(x(?:86|32)\)/ig, ' (32-bit)')
		.replace(/\s+(?:Build)\s+(\d+)\b/g, ' build $1')
		.replace(/\s+(?:Multilingual|Multi(?:-|\s)*lang(?:uage)?)\b/g, ' multilingual');
	  description = html2php(response.document.querySelector('section.descr'), response.finalUrl).trim();
	  if (/\s*^[ \t]*(?:\[i\]\[\/i\])?Homepage\s*$.*/im.test(description)) description = RegExp.leftContext;
	  description = description.split(/[ \t]*\r?\n/).slice(6).map(line => line.trim()).join('\n')
		.replace(/^[ \t]*(?:\[i\]\[\/i\])?Screenshots:?\s*/igm, '')
		.replace(/^[ \t]*(?:\[i\]\[\/i\])?(\[b\]Release\s+Notes:?\[\/b\])(?:[ \t]*\r?\n)+/igm, '$1\n')
		.replace(/\[hr\]/ig, '\n');
	  ref = response.document.querySelector('section.descr > div.release-info');
	  var releaseInfo = ref != null && ref.textContent.trim();
	  if (/\b(?:Languages?)\s*:\s*(.*?)\s*(?:$|\|)/i.exec(releaseInfo) != null)
		description += '\n\n[b]Languages:[/b]\n' + RegExp.$1;
	  if ((ref = response.document.querySelector('div.txtleft > a')) != null) {
		description += '\n\n[b]Product page:[/b]\n[url]' +
		  removeRedirect(ref.pathname.toLowerCase().startsWith('/confirm/url/') && urlParser.test(ref.textContent) ?
			ref.textContent.trim() : ref.href) + '[/url]';
	  }
	  writeDescription(description.collapseGaps());
	  if ((ref = response.document.querySelector('section.descr > div.center > a.mfp-image')) != null) {
		setCover(ref.href);
	  } else {
		ref = response.document.querySelector('section.descr > div.center > img[data-src]');
		if (ref != null) setCover(ref.dataset.src);
	  }
	  var internalTags = Array.from(response.document.querySelectorAll('ul.item_tags_list > li > a[rel="tag"]'))
	  	.map(elem => elem.textContent.toLowerCase().trim());
	  if ((ref = response.document.querySelector('a.cat:last-of-type > span')) != null) {
		if (ref.textContent.toLowerCase() == 'windows') {
		  tags.add('apps.windows');
		  if (/\b(?:(?:x|ia|em)64)\b/i.test(releaseInfo) || /\(64[-\s]*bit\)/i.test(title)) tags.add('win64');
		  if (/\b(?:x86|x32)\b/i.test(releaseInfo) || /\(32[-\s]*bit\)/i.test(title)) tags.add('win32');
		}
		if (ref.textContent.toLowerCase() == 'macos') tags.add('apps.mac');
		if (ref.textContent.toLowerCase() == 'linux' || ref.textContent.toLowerCase() == 'unix') tags.add('apps.linux');
		if (ref.textContent.toLowerCase() == 'android') tags.add('apps.android');
		if (ref.textContent.toLowerCase() == 'ios') tags.add('apps.ios');
	  }
	  if (tags.length > 0 && elementWritable(ref = document.getElementById('tags'))) ref.value = tags.toString();
	  if (title && !/\(\d+-?bit\)/i.test(title)) {
		if (tags.includes('win64') && !tags.includes('win32')) title += ' (64-bit)';
		if (tags.includes('win32') && !tags.includes('win64')) title += ' (32-bit)';
	  }
	  if (elementWritable(ref = document.getElementById('title'))) ref.value = title || '';
	});
	if (!weak) {
	  addMessage('this domain not supported', 'critical');
	  clipBoard.value = '';
	}
	return Promise.reject('this domain not supported');
  } // fillFromText_Apps

  function fillFromText_Ebooks(weak = false) {
	if (messages != null) messages.parentNode.removeChild(messages);
	if (!urlParser.test(clipBoard.value)) {
	  addMessage('only URL accepted for this category', 'critical');
	  return Promise.reject('only URL accepted for this category');
	}
	sourceUrl = RegExp.$1;
	var description, tags = new TagManager();
	if (sourceUrl.toLowerCase().includes('martinus.cz') || sourceUrl.toLowerCase().includes('martinus.sk'))
	  return globalFetch(sourceUrl).then(function(response) {
		function get_detail(x, y) {
		  var ref = response.document.querySelector('section#details > div > div > div:first-of-type > div:nth-child(' +
			x + ') > dl:nth-child(' + y + ') > dd');
		  return ref != null ? ref.textContent.trim() : null;
		}

		i = response.document.querySelectorAll('article > ul > li > a');
		if (i.length > 0 && elementWritable(ref = document.getElementById('title') || document.querySelector('input[name="title"]'))) {
		  description = joinAuthors(i);
		  if ((i = response.document.querySelector('article > h1')) != null) description += ' – ' + i.textContent.trim();
		  i = response.document.querySelector('div.bar.mb-medium > div:nth-child(1) > dl > dd > span');
		  if (i != null && (i = extractYear(i.textContent))) description += ' (' + i + ')';
		  ref.value = description;
		}

		ref = response.document.querySelector('section#description > div');
		if (ref != null) description = html2php(ref).replace(/^\s*\[img\].*?\[\/img\]\s*/i, '').trim();
		if (description.length > 0 && !description.includes('[quote]')) description = '[quote]' + description + '[/quote]';
		const translation_map = [
		  [/\b(?:originál)/i, 'Original title'],
		  [/\b(?:datum|dátum|rok)\b/i, 'Release date'],
		  [/\b(?:katalog|katalóg)/i, 'Catalogue #'],
		  [/\b(?:stran|strán)\b/i, 'Page count'],
		  [/\bjazyk/i, 'Language'],
		  [/\b(?:nakladatel|vydavatel)/i, 'Publisher'],
		  [/\b(?:doporuč|ODPORÚČ)/i, 'Age rating'],
		];
		response.document.querySelectorAll('section#details > div > div > div:first-of-type > div > dl').forEach(function(detail) {
		  var lbl = detail.children[0].textContent.trim();
		  var val = detail.children[1].textContent.trim();
		  if (/\b(?:rozm)/i.test(lbl) || /\b(?:vazba|vázba)\b/i.test(lbl)) return;
		  translation_map.forEach(k => { if (k[0].test(lbl)) lbl = k[1] });
		  if (/\b(?:ISBN)\b/i.test(lbl)) {
			sourceUrl = new URL('https://www.worldcat.org/isbn/'.concat(detail.children[1].textContent.trim()));
			val = '[url=' + sourceUrl.href + ']' + detail.children[1].textContent.trim() + '[/url]';
			findOCLC(sourceUrl);
// 		  } else if (/\b(?:ISBN)\b/i.test(lbl)) {
// 			val = '[url=https://www.goodreads.com/search/search?q=' + detail.children[1].textContent.trim() +
// 			  '&search_type=books]' + detail.children[1].textContent.trim() + '[/url]';
		  }
		  description += '\n[b]' + lbl + ':[/b] ' + val;
		});
		sourceUrl = new URL(response.finalUrl);
		description += '\n\n[b]More info:[/b]\n[url]' + sourceUrl.href + '[/url]';
		writeDescription(description.collapseGaps());

		if ((i = response.document.querySelector('a.mj-product-preview > img')) != null) {
		  setCover(i.src.replace(/\?.*/, ''));
		} else if ((i = response.document.querySelector('head > meta[property="og:image"][content]')) != null) {
		  setCover(i.content.replace(/\?.*/, ''));
		}

		response.document.querySelectorAll('dd > ul > li > a').forEach(x => { tags.add(x.textContent) });
		if (tags.length > 0 && elementWritable(ref = document.getElementById('tags'))) {
		  ref.value = tags.toString();
		}
	  });
	else if (sourceUrl.toLowerCase().includes('goodreads.com')) return globalFetch(sourceUrl).then(function(response) {
	  i = response.document.querySelectorAll('a.authorName > span');
	  if (i.length > 0 && elementWritable(ref = document.getElementById('title') || document.querySelector('input[name="title"]'))) {
		description = joinAuthors(i);
		if ((i = response.document.querySelector('h1#bookTitle')) != null) description += ' – ' + i.textContent.trim();
		if ((i = response.document.querySelector('div#details > div.row:nth-of-type(2)')) != null
			&& (i = extractYear(i.textContent))) description += ' (' + i + ')';
		ref.value = description;
	  }

	  var description = [];
	  response.document.querySelectorAll('div#description span:last-of-type').forEach(function(node) {
		description = html2php(node, sourceUrl).trim();
	  });
	  if (description.length > 0 && !description.includes('[quote]')) {
		description = '[quote]' + description.trim() + '[/quote]';
	  }

	  function strip(str) {
		return typeof str == 'string' ?
		  str.replace(/\s{2,}/g, ' ').replace(/[\n\r]+/, '').replace(/\s*\.{3}(?:less|more)\b/g, '').trim() : null;
	  }

	  response.document.querySelectorAll('div#details > div.row').forEach(k => { description += '\n' + strip(k.innerText) });
	  description += '\n';

	  response.document.querySelectorAll('div#bookDataBox > div.clearFloats').forEach(function(detail) {
		var lbl = detail.children[0].textContent.trim();
		var val = strip(detail.children[1].textContent);
		if (/\b(?:ISBN)\b/i.test(lbl) && (/\b(\d{13})\b/.test(val) || /\b(\d{10})\b/.test(val))) {
		  sourceUrl = new URL('https://www.worldcat.org/isbn/'.concat(RegExp.$1));
		  val = '[url=' + sourceUrl.href + ']' + strip(detail.children[1].textContent) + '[/url]';
		  findOCLC(sourceUrl);
		}
		description += '\n[b]' + lbl + ':[/b] ' + val;
	  });
	  if ((ref = response.document.querySelector('span[itemprop="ratingValue"]')) != null) {
		description += '\n[b]Rating:[/b] ' + Math.round(parseFloat(ref.firstChild.textContent) * 20) + '%';
	  }
	  sourceUrl = new URL(response.finalUrl);
// 		if ((ref = response.document.querySelector('div#buyButtonContainer > ul > li > a.buttonBar')) != null) {
// 		  let u = new URL(ref.href);
// 		  description += '\n[url=' + sourceUrl.origin + u.pathname + '?' + u.search + ']Libraries[/url]';
// 		}
	  description += '\n\n[b]More info and reviews:[/b]\n[url]' + sourceUrl.origin + sourceUrl.pathname + '[/url]';
	  response.document.querySelectorAll('div.clearFloats.bigBox').forEach(function(bigBox) {
		if (bigBox.id == 'aboutAuthor' && (ref = bigBox.querySelector('h2 > a')) != null) {
		  description += '\n\n[b][url=' + ref.href + ']' + ref.textContent.trim() + '[/url][/b]';
		  if ((ref = bigBox.querySelector('div.bigBoxBody a > div[style*="background-image"]')) != null) {
		  }
		  if ((ref = bigBox.querySelector('div.bookAuthorProfile__about > span[id]:last-of-type')) != null) {
			description += '\n' + html2php(ref, sourceUrl).trim().replace(/^\[i\]Librarian\s+Note:.*?\[\/i\]\s+/i, '');
		  }
		} else if ((ref = bigBox.querySelector('h2 > a[href^="/trivia/"]')) != null) {
		  description += '\n\n[b][url=' + ref.href + ']' + ref.textContent.trim() + '[/url][/b]';
		  if ((ref = bigBox.querySelector('div.bigBoxContent > div.mediumText')) != null) {
			description += '\n' + ref.firstChild.textContent.trim();
		  }
// 		  } else if ((ref = bigBox.querySelector('h2 > a[href^="/work/quotes/"]')) != null) {
// 			description += '\n\n[b][url=' + ref.href + ']' + ref.textContent.trim() + '[/url][/b]';
// 			bigBox.querySelectorAll('div.bigBoxContent > div.stacked > span.readable').forEach(function(quote) {
// 			  description += '\n' + ref.firstChild.textContent.trim();
// 			});
		}
	  });
	  writeDescription(description.collapseGaps());
	  if ((ref = response.document.querySelector('div.editionCover > img')) != null) setCover(ref.src.replace(/\?.*/, ''));
	  response.document.querySelectorAll('div.elementList > div.left').forEach(tag => { tags.add(tag.textContent.trim()) });
	  if (tags.length > 0 && elementWritable(ref = document.getElementById('tags'))) ref.value = tags.toString();
	}); else if (sourceUrl.toLowerCase().includes('databazeknih.cz')) {
	  if (!sourceUrl.toLowerCase().includes('show=alldesc')) {
		if (!sourceUrl.includes('?')) { sourceUrl += '?show=alldesc' } else { sourceUrl += '&show=alldesc' }
	  }
	  return globalFetch(sourceUrl).then(function(response) {
		i = response.document.querySelectorAll('span[itemprop="author"] > a');
		if (i != null && elementWritable(ref = document.getElementById('title') || document.querySelector('input[name="title"]'))) {
		  description = joinAuthors(i);
		  if ((i = response.document.querySelector('h1[itemprop="name"]')) != null)
			description += ' – ' + i.textContent.trim();
		  i = response.document.querySelector('span[itemprop="datePublished"]');
		  if (i != null && (i = extractYear(i.textContent))) description += ' (' + i + ')';
		  ref.value = description;
		}

		ref = response.document.querySelector('p[itemprop="description"]');
		if (ref != null) description = html2php(ref, sourceUrl).trim();
		if (description.length > 0 && !description.includes('[quote]')) description = '[quote]' + description + '[/quote]';
		const translation_map = [
		  [/\b(?:orig)/i, 'Original title'],
		  [/\b(?:série)\b/i, 'Series'],
		  [/\b(?:vydáno)\b/i, 'Released'],
		  [/\b(?:stran)\b/i, 'Page count'],
		  [/\b(?:jazyk)\b/i, 'Language'],
		  [/\b(?:překlad)/i, 'Translation'],
		  [/\b(?:autor obálky)\b/i, 'Cover author'],
		];
		response.document.querySelectorAll('table.bdetail tr').forEach(function(detail) {
		  var lbl = detail.children[0].textContent.trim();
		  var val = detail.children[1].textContent.trim();
		  if (/(?:žánr|\bvazba)\b/i.test(lbl)) return;
		  translation_map.forEach(k => { if (k[0].test(lbl)) lbl = k[1] });
		  if (/\b(?:ISBN)\b/i.test(lbl) && /\b(\d+(?:-\d+)*)\b/.exec(val) != null) {
			sourceUrl = new URL('https://www.worldcat.org/isbn/'.concat(RegExp.$1.replace(/-/g, '')));
			val = '[url=' + sourceUrl.href + ']' + detail.children[1].textContent.trim() + '[/url]';
			findOCLC(sourceUrl);
		  }
		  description += '\n[b]' + lbl + '[/b] ' + val;
		});

		sourceUrl = new URL(response.finalUrl);
		description += '\n\n[b]More info:[/b]\n[url]' + sourceUrl.origin + sourceUrl.pathname + '[/url]';
		writeDescription(description.collapseGaps());

		if ((ref = response.document.querySelector('div#icover_mid > a')) != null) setCover(ref.href.replace(/\?.*/, ''));
		if ((ref = response.document.querySelector('div#lbImage')) != null && /\burl\("(.*)"\)/i.test(i.style.backgroundImage)) {
		  setCover(RegExp.$1.replace(/\?.*/, ''));
		}

		response.document.querySelectorAll('h5[itemprop="genre"] > a').forEach(tag => { tags.add(tag.textContent.trim()) });
		response.document.querySelectorAll('a.tag').forEach(tag => { tags.add(tag.textContent.trim()) });
		if (tags.length > 0 && elementWritable(ref = document.getElementById('tags'))) ref.value = tags.toString();
	  });
	}
	if (!weak) {
	  addMessage('domain not supported', 'critical');
	  clipBoard.value = '';
	}
	return Promise.reject('domain not supported');

	function joinAuthors(nodeList) {
	  if (typeof nodeList != 'object') return null;
	  return Array.from(nodeList).map(it => it.textContent.trim()).join(' & ');
	}

	function findOCLC(url) {
	  if (!url) return false;
	  var oclc = document.querySelector('input[name="oclc"]');
	  if (!elementWritable(oclc)) return false;
	  globalFetch(url).then(function(dom) {
		var ref = dom.querySelector('tr#details-oclcno > td:last-of-type');
		if (ref != null) oclc.value = ref.textContent.trim();
	  });
	  return true;
	}
  } // fillFromText_Ebooks

  function preview(n) {
	if (!prefs.auto_preview) return;
	var btn = document.querySelector('input.button_preview_' + n + '[type="button"][value="Preview"]');
	if (btn != null) btn.click();
  }

  function writeDescription(desc) {
	if (typeof desc != 'string') return;
	if (elementWritable(ref = document.querySelector('textarea#desc')
		|| document.querySelector('textarea#description') || document.querySelector('textarea#album_desc'))) ref.value = desc;
	if ((ref = document.getElementById('body')) != null && !ref.disabled) {
	  if (ref.value.length > 0) ref.value += '\n\n';
	  ref.value += desc;
	}
  }

  function loadGoogleData(response) {
	const initDataParser = /\b(?:AF_initDataCallback)\s*\(\s*\{\s*key:\s*'ds:(\d+)'.*data:\s*function\(\)\s*{\s*return\s*([\S\s]+)\}\s*\}\s*\);/;
	return Array.from(response.document.querySelectorAll('script[nonce]'))
	  .map(function(script) { try { return eval(initDataParser.exec(script.text)[2]) } catch(e) { return false } })
	  .filter(obj => obj && typeof obj == 'object');
  }

  function queryItunesAPI(key, params) {
	return queryGenericAPI('itunes.apple.com', key, params);
  }
  function queryDeezerAPI(key, params) {
	return queryGenericAPI('api.deezer.com', key, params);
  }
  function queryDiscogsAPI(key, params) {
	if (prefs.discogs_key && prefs.discogs_secret) {
	  var hdr = { Authorization: 'Discogs key=' + prefs.discogs_key + ', secret=' + prefs.discogs_secret };
	} else if (discogs_token) hdr = { Authorization: 'Discogs token=' + discogs_token };
	return queryGenericAPI('api.discogs.com', key, params, hdr);
  }
  function queryMusicBrainzAPI(key, params) {
	return queryGenericAPI('musicbrainz.org', 'ws/2/' + key + '/', Object.assign({ fmt: 'json' }, params));
  }
  function querySpotifyAPI(key, params) {
	return key ? setOauth2Token().then(credentials => queryGenericAPI('api.spotify.com', 'v1/' + key, params, {
	  Authorization: credentials.token_type + ' ' + credentials.access_token,
	})) : Promise.reject('No API keyword');

	function setOauth2Token() {
	  try { var accessToken = JSON.parse(window.localStorage.spotifyAccessToken) } catch(e) { }
	  if (isTokenValid(accessToken)) {
		if (prefs.diag_mode) console.debug('Re-used Spotify access token:', accessToken, new Date(accessToken.expires).toLocaleTimeString());
		return Promise.resolve(accessToken);
	  }
	  if (!spotify_clientid || !spotify_clientsecret) return Promise.reject('Spotify credentials not configured');
	  const data = new URLSearchParams({
		'grant_type': 'client_credentials',
	  });
	  return globalFetch('https://accounts.spotify.com/api/token', { responseType: 'json', headers: {
		Authorization: 'Basic ' + btoa(spotify_clientid + ':' + spotify_clientsecret),
	  } }, data).then(function(response) {
		accessToken = response.response;
		accessToken.expires = Date.now() + accessToken.expires_in * 1000;
		if (!isTokenValid(accessToken)) return Promise.reject('Invalid token received');
		delete accessToken.expires_in;
		window.localStorage.spotifyAccessToken = JSON.stringify(accessToken);
		if (prefs.diag_mode) console.debug('Spotify access token successfully set:', accessToken);
		return accessToken;
	  });
	}

	function isTokenValid(accessToken) {
	  return typeof accessToken == 'object' && accessToken.token_type && accessToken.access_token
	  	&& accessToken.expires >= Date.now() + 30000;
	}
  }
  function queryLastFmAPI(method, params) {
	return lastfm_api_key ? queryGenericAPI('ws.audioscrobbler.com', '2.0/', Object.assign({
	  method: method,
	  api_key: lastfm_api_key,
	  format: 'json',
	}, params || {})) : Promise.reject('Last.fm API key not configured');
  }
  function queryTidalAPI(key, params, countryCode) {
	if (!key) return Promise.reject('API action not defined');
	if (typeof params != 'object') params = {};
	params.deviceType = 'BROWSER';
	params.countryCode = countryCode;
	return setOauth2Token().then(function(token) {
	  if (!params.countryCode) params.countryCode = token.user.countryCode || 'US';
	  return { Authorization: token.token_type + ' ' + token.access_token };
	}).catch(function(reason) {
	  console.warn('Tidal Oauth2 failed:', reason);
	  return setSession().then(function(session) {
		if (!params.countryCode) params.countryCode = session.countryCode || 'US';
		return { 'X-Tidal-SessionId': session.sessionId };
	  });
	}).then(header => queryGenericAPI('listen.tidal.com', 'v1/pages/'.concat(key), params, header));

	function setOauth2Token() {
	  try { var accessToken = JSON.parse(window.localStorage.tidalAccessToken) } catch(e) { }
	  if (isTokenValid(accessToken)) {
		if (prefs.diag_mode) console.debug('Re-used Tidal access token:', accessToken, new Date(accessToken.expires).toLocaleTimeString());
		return Promise.resolve(accessToken);
	  }
	  return Promise.reject('Not implemented');
	  // TODO
	}
	function setSession() {
	  try { var session = JSON.parse(window.sessionStorage.tidalSession) } catch(e) { }
	  if (isSessionValid(session)) {
		if (prefs.diag_mode) console.debug('Re-used Tidal session:', session);
		return Promise.resolve(session);
	  }
	  if (!prefs.tidal_userid || !prefs.tidal_userpassword) return Promise.reject('Tidal user credentials not configured');
	  const deviceTokens = [
		'BI218mwp9ERZ3PFI', // browser | Streams lossless quality
		'wdgaB1CilGA-S_s2', // browser | Streams HIGH/LOW Quality over RTMP, FLAC and Videos over HTTP, but many Lossless Streams are encrypted.
		'4zx46pyr9o8qZNRw', // browser(?) | other quality
		'kgsOOmYk3zShYrNP', // Android | All Streams are HTTP Streams. Correct numberOfVideos in Playlists (best Token to use)
		'GvFhCVAYp3n43EN3', // iOS | Same as Android Token, but uses ALAC instead of FLAC
		'_DSTon1kC8pABnTw', // iOS | Same as Android Token, but uses ALAC instead of FLAC
		'4zx46pyr9o8qZNRw', // native | Same as Android Token, but FLAC streams are encrypted
		'BI218mwp9ERZ3PFI', // audirvana | Like Android Token, supports MQA, but returns 'numberOfVideos = 0' in Playlists
		'wc8j_yBJd20zOmx0', // amarra | Like Android Token, but returns 'numberOfVideos = 0' in Playlists
		'P5Xbeo5LFvESeDy6', // Like Android Token, but returns 'numberOfVideos = 0' in Playlists
		'_KM2HixcUBZtmktH', // Same as previous
		'oIaGpqT_vQPnTr0Q', // Same, but uses RTMP for HIGH/LOW Quality
	  ];
	  return getClientToken().then(function(token) {
		const data = new URLSearchParams({
		  username: prefs.tidal_userid,
		  password: prefs.tidal_userpassword,
		  clientUniqueKey: getClientKey(),
		  clientVersion: '1.0',
		  token: deviceTokens[2],
		});
		return globalFetch('https://api.tidalhifi.com/v1/login/username', {
		  responseType: 'json',
		  headers: token ? { 'X-Tidal-Token': token } : undefined,
		}, data);
	  }).then(function(response) {
		if (!isSessionValid(session = response.response)) return Promise.reject('Invalid session');
		window.sessionStorage.tidalSession = JSON.stringify(session);
		if (prefs.diag_mode) console.debug('Tidal session successfully established:', session);
		return session;
	  });
	}
	function uuidv4() {
	  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
		return v.toString(16);
	  });
	}
	function randomString(length) {
	  const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
	  var text = "";
	  for (var i = 0; i < length; ++i) text += possible.charAt(Math.floor(Math.random() * possible.length));
	  return text;
	}
	function generateCodeChallenge(code_verifier) {
	  return code_challenge = base64URL(CryptoJS.SHA256(code_verifier))
	}
	function base64URL(string) {
	  return string.toString(CryptoJS.enc.Base64).replace(/=/g, '').replace(/\+/g, '-').replace(/\//g, '_')
	}
	function getClientId() {
	  if (prefs.tidal_clientid || (prefs.tidal_clientid = GM_getValue('tidal_clientid'))) return Promise.resolve(prefs.tidal_clientid);
	  return getTidalSecrets().then(function(response) {
		const rx = /"(\w{40})":"(\w{16})"/g;
		if ((i = response.responseText.match(rx)) == null || !rx.test(i.shift())) return Promise.reject('not found');
		GM_setValue('tidal_clientid', prefs.tidal_clientid = RegExp.$2);
		if (prefs.diag_mode) console.debug('Successfully configured Tidal client Id:', prefs.tidal_clientid);
		return prefs.tidal_clientid;
	  }).catch(function(reason) {
		reason = 'Client Id auto detection failed ('.concat(reason, '), set it manually (tidal_clientid)');
		alert(reason);
		return Promise.reject(reason);
	  });
	}
	function getClientKey() {
	  if (!prefs.tidal_clientkey && !(prefs.tidal_clientkey = GM_getValue('tidal_clientkey')))
		GM_setValue('tidal_clientkey', prefs.tidal_clientkey = uuidv4());
	  return prefs.tidal_clientkey;
	}
	function getClientToken() {
	  if (prefs.tidal_token || (prefs.tidal_token = GM_getValue('tidal_token'))) return Promise.resolve(prefs.tidal_token);
	  return getTidalSecrets().then(function(response) {
		if (!/"(\w{40})":"(\w{40})"/.test(response.responseText)) return Promise.reject('not found');
		GM_setValue('tidal_token', prefs.tidal_token = RegExp.$2);
		if (prefs.diag_mode) console.debug('Successfully configured Tidal token:', prefs.tidal_token);
		return prefs.tidal_token;
	  }).catch(function(reason) {
		console.warn('Tidal token detection fail (' + reason + ')');
		return undefined;
	  });
	}
	function getTidalSecrets() {
	  return globalFetch('https://listen.tidal.com/app.61d2e10fa09e5075c5ad.chunk.js', { responseType: 'text' });
	}
	function isTokenValid(accessToken) {
	  return typeof accessToken == 'object' && accessToken.token_type && accessToken.access_token
	  	&& accessToken.expires >= Date.now() + 30000;
	}
	function isSessionValid(session) {
	  return typeof session == 'object' && session.userId > 0 && session.sessionId;
	}
  }

  function queryGenericAPI(domain, key, params, headers) {
	//if (!key) return Promise.reject(new Error('Keyword missing'));
	var retryCount = 0;
	return new Promise(function(resolve, reject) {
	  var url = 'https://' + domain + '/' + key;
	  var query = new URLSearchParams(params || undefined).toString();
	  if (query.length > 0) url += '?' + query;
	  if (typeof headers != 'object') headers = {};
	  headers.Accept = 'application/json';
	  queryInternal();

	  function queryInternal() {
		GM_xmlhttpRequest({
		  method: 'GET',
		  url: url,
		  responseType: 'json',
		  headers: headers,
		  onload: function(response) {
			if (response.status >= 200 && response.status < 400) resolve(response.response);
				else reject(defaultErrorHandler(response));
		  },
		  onerror: response => { reject(defaultErrorHandler(response)) },
		  ontimeout: response => { reject(defaultTimeoutHandler(response)) },
		});
	  }
	});
  }

  function loadItunesMetadata(urlOrId) {
	return (function() {
	  return /^https:\/\/apple\.co\//i.test(urlOrId) ? urlResolver(urlOrId).then(url => getAppleId(url)) : getAppleId(urlOrId);

	  function getAppleId(urlOrId) {
		var appleId = parseInt(urlOrId) || itunesRlsParser.test(urlOrId) && parseInt(RegExp.$1);
		return appleId ? Promise.resolve(appleId) : Promise.reject('Aplpe Id cannot be determined');
	  }
	})().then(appleId => globalFetch('https://music.apple.com/album/'.concat(appleId)).then(function(response) {
	  var params = response.document.querySelector('meta[name="desktop-music-app/config/environment"][content]');
	  if (params == null) return Promise.reject('Desktop environment not located');
	  params = JSON.parse(decodeURIComponent(params.content));
	  if (prefs.diag_mode) console.debug('Got Apple Music desktop environment:', params);
	  if (!params.MEDIA_API.token) return Promise.reject('Apple access token not found');
	  var query = new URLSearchParams({
		include: 'tracks,artists',
		l: 'en-US',
	  });
	  return globalFetch(params.MUSIC.BASE_URL.concat('/catalog/us/albums/', appleId, '?', query), { responseType: 'json', headers: {
		'Referer': response.finalUrl,
		'Authorization': 'Bearer '.concat(params.MEDIA_API.token),
	  } }).then(function(response2) {
		var album = response2.response.data[0];
		album.description = response.document.querySelector('div.content-modal__content-container')
		  || response.document.querySelector('div.product-page-header__notes span'),
		album.url = response.finalUrl;
		if (album.attributes.artwork) album.attributes.artwork.realUrl = album.attributes.artwork.url
			.replace('{w}', album.attributes.artwork.width).replace('{h}', album.attributes.artwork.height);
		if (prefs.diag_mode) console.debug('Apple Music metadata received:', album);
// 		query.set('include', 'artists,albums');
// 		Promise.all(album.relationships.tracks.data.map(track => globalFetch(params.MUSIC.BASE_URL.concat('/catalog/us/songs/', track.id, '?', query), { responseType: 'json', headers: {
// 		  'Referer': response.finalUrl,
// 		  'Authorization': 'Bearer '.concat(params.MEDIA_API.token),
// 		} }).then(response => response.response))).then(tracks => { console.debug('Apple Music tracks received:', tracks) })
// 		.catch(reason => { console.error(reason) });
		return album;
	  });
	}));
  }

  function getMusicBrainzCovers(mbid) {
	return searchInternal('release', mbid).then(covers => covers || searchMaster(), searchMaster);

	function searchInternal(entity, mbid) {
	  return new Promise((resolve, reject) => GM_xmlhttpRequest({
		method: 'GET',
		url: 'https://coverartarchive.org/'.concat(entity, '/', mbid),
		responseType: 'json',
		onload: function(response) {
		  if (response.status == 404) return resolve(null);
		  if (response.status < 200 || response.status >= 400) return reject(defaultErrorHandler(response));
		  var images = response.response.images
		  	.filter(image => urlParser.test(image.image) && image.isfront
				|| Array.isArray(image.types) && image.types.includesCaseless('Front'))
		  	.map(image => image.image);
		  resolve(images.length > 0 ? [response.response.release, images] : null);
		},
		onerror: error => reject(defaultErrorHandler(error)),
		ontimeout: timeout => reject(defaultTimeoutHandler(timeout)),
	  }));
	}
	function searchMaster() {
	  return queryMusicBrainzAPI('release/' + mbid, { inc: 'release-groups' })
		.then(release => searchInternal('release-group', release['release-group'].id));
	}
  }

  function setCover(url) {
	if (!urlParser.test(url)) return Promise.reject('Image url not valid');
	var image = document.getElementById('image') || document.querySelector('input[name="image"]');
	if (!elementWritable(image)) return Promise.reject('Image input not available');
	return verifyImageUrl(url).then(function(imageUrl) {
	  if (!isNWCD) {
		image.value = imageUrl;
		coverPreview(image, imageUrl);
		if (prefs.auto_rehost_cover && !imageUrl.toLowerCase().startsWith(ptpimgOrigin)) {
		  image.disabled = true;
		  return rehost2PTPIMG([imageUrl])
			.then(urls => urls.length > 0 ? (image.value = urls[0]) : imageUrl)
			.catch(reason => { /*alert(reason)*/ addMessage(reason.concat(' (not rehosted)'), 'warning') })
			.then(url => { image.disabled = false; return url });
		}
		return imageUrl;
	  } else return uploadToImagehost(imageUrl).then(function(result) {
		image.value = result.url;
		setTimeout(function() { coverPreview(image, result.url) }, 2000);
		return result.url;
	  });
	});
  }

  function elementWritable(elem) {
	return elem != null && !elem.disabled && (overwrite || elem.value == '' || !isRED && elem.value == '---');
  }
} // fillFromText

function addMessage(text, cls) {
  switch (cls) {
	case 'info': var prefix = 'Info'; break;
	case 'notice': prefix = 'Notice'; break;
	case 'warning': prefix = 'Warning'; break;
	case 'critical': prefix = 'FATAL'; break;
	default: return null;
  }
  if ((messages = document.getElementById('UA-messages')) == null) {
	let ua = document.getElementById('upload assistant');
	if (ua == null) return null;
	let tr = document.createElement('TR');
	tr.id = 'UA-messages';
	ua.firstElementChild.append(tr);
	var td = document.createElement('TD');
	td.colSpan = 2;
	td.className = 'ua-messages-bg';
	tr.append(td);
  } else {
	td = messages.firstElementChild;
	if (td == null) return null;
  }
  var div = document.createElement('DIV');
  div.classList.add('ua-messages', 'ua-'.concat(cls));
  div[text instanceof HTML ? 'innerHTML' : 'textContent'] = prefix.concat(': ', text);
  return td.appendChild(div);
}

function setHandlers() {
  if (prefs.cleanup_descriptions) ['form.create_form', 'form.edit_form', 'form#request_form'].forEach(function(sel) {
	if ((ref = document.querySelector(sel)) != null) ref.addEventListener('submit', cleanupDescriptions);
  });

  if ((ref = document.getElementById('yadg_input')) != null) ref.ondrop = clear0;

  if (!isNWCD) {
	if ((ref = document.getElementById('image') || document.querySelector('input[name="image"]')) != null) {
	  ref.ondragover = voidDragHandler0;
	  ref.ondblclick = imageClear;
	  ref.ondrop = imageDropHandler;
	  ref.onpaste = imagePasteHandler;
	  ref.placeholder = 'Paste/drop local or remote image';
	}
  }
  // Now rape OPS upload form, but only gently
  if (isOPS && isUpload && (ref = document.getElementById('remaster')) != null) {
	ref.checked = true;
	if (!isAddFormat && prefs.ops_always_edition) {
	  elem = ref.parentNode.parentNode;
	  elem.style.display = 'none';
	  if ((ref = document.querySelector('span#year_label_not_remaster')) != null) ref.textContent = 'Initial year:';
	  if ((ref = document.querySelector('tr#edition_year > td.label')) != null) ref.textContent = 'Edition year:';
	  if ((ref = document.querySelector('tr#edition_title > td.label')) != null) ref.textContent = 'Edition title:';
	  if ((ref = document.getElementById('label_tr')) != null) /*ref.style.display = 'none'; */ref.remove();
	  if ((ref = document.getElementById('catalogue_tr')) != null) /*ref.style.display = 'none'; */ref.remove();
	  document.querySelectorAll('table#edition_information > tbody > tr')
		.forEach(tr => { elem.parentNode.insertBefore(tr, elem) });
	} else Remaster();
  }

  Array.from(document.getElementsByTagName('textarea')).forEach(function(textArea) {
	if (textArea.className == 'ua-input') return;
	textArea.ondragover = voidDragHandler0;
	textArea.ondrop = descDropHandler;
	textArea.onpaste = descPasteHandler;
  });
}

function html2php(node, url, tagChain = []) {
  if (!node || typeof node != 'object') return null;
  switch (node.nodeType) {
	case Node.ELEMENT_NODE: {
	  let tags = [], _tags = [], text = [];
	  for (let i = 0; i < 5; ++i) text[i] = '';
	  switch (node.nodeName) {
		case 'P':
		  text[0] = '\n'; text[4] = '\n';
		  break;
		case 'DIV':
		  text[0] = '\n\n'; text[4] = '\n\n';
		  break;
		case 'DT':
		  text[4] = '\n';
		  break;
		case 'DD':
		  text[4] = '\n';
		  if (isRED) addTag('pad=0|0|0|30'); else text[0] = '     ';
		  break;
		case 'LABEL':
		  addTag('b');
		  text[0] = '\n\n';
		  break;
		case 'BR':
		  return '\n';
		case 'HR':
		  return isRED ? '[hr]' : '\n';
		case 'B': case 'STRONG':
		  addTag('b');
		  break;
		case 'I': case 'EM': case 'DFN': case 'CITE': case 'VAR':
		  addTag('i');
		  break;
		case 'U': case 'INS':
		  addTag('u');
		  break;
		case 'DEL':
		  addTag('s');
		  break;
		case 'CODE': case 'SAMP': case 'KBD':
		  addTag('code');
		  text[2] = node.textContent;
		  break;
		case 'PRE':
		  addTag('pre');
		  text[2] = node.textContent;
		  break;
		case 'BLOCKQUOTE': case 'QUOTE':
		  addTag('quote');
		  break;
		case 'Q':
		  text[1] = '"'; text[3] = '"';
		  break;
		case 'H1':
		  addTag('size=5'); addTag('b');
		  text[0] = '\n\n'; text[4] = '\n\n';
		  break;
		case 'H2':
		  addTag('size=4'); addTag('b');
		  text[0] = '\n\n'; text[4] = '\n\n';
		  break;
		case 'H3':
		  addTag('size=3'); addTag('b');
		  text[0] = '\n\n'; text[4] = '\n\n';
		  break;
		case 'H4': case 'H5': case 'H6':
		  addTag('b');
		  text[0] = '\n\n'; text[4] = '\n\n';
		  break;
		case 'SMALL':
		  addTag('size=1');
		  break;
		case 'OL': case 'UL':
		  _tags.push(node.nodeName.toLowerCase());
		  break;
		case 'DL':
		  _tags.push(node.nodeName.toLowerCase());
		  break;
		case 'LI':
		  switch (tagChain.reverse().find(tag => /^[ou]l$/.test(tag))) {
			case 'ol': text[0] = '[#] '; text[4] = '\n'; break;
			case 'ul': text[0] = '[*] '; text[4] = '\n'; break;
			default: return '';
		  }
		  break;
		case 'A': {
		  if (/^https?:$/i.test(node.protocol)) addTag('url=' + removeRedirect(node.href));
		  break;
		}
		case 'IMG':
		  addTag('img');
		  text[2] = node.dataset.src || node.src;
		  break;
		case 'DETAILS': {
		  let summary = node.querySelector('summary');
		  summary = summary != null ? '='.concat(summary.textContent.trim()) : '';
		  addTag('hide' + summary);
		  break;
		}
		case 'AUDIO': case 'BASE': case 'BUTTON': case 'CANVAS': case 'COL': case 'COLGROUP': case 'DATALIST':
		case 'DIALOG': case 'EMBED': case 'FIELDSET': case 'FORM': case 'HEAD': case 'INPUT': case 'LEGEND':
		case 'LINK': case 'MAP': case 'META': case 'METER': case 'NOSCRIPT': case 'OBJECT': case 'OPTGROUP':
		case 'OPTION': case 'PARAM': case 'PROGRESS': case 'SELECT': case 'SOURCE': case 'STYLE': case 'SUMMARY':
		case 'SVG': case 'TEMPLATE': case 'TEXTAREA': case 'TITLE': case 'TRACK': case 'VIDEO':
		  return '';
	  }
	  if (['left', 'center', 'right'].some(al => node.style.textAlign.toLowerCase() == al)) {
		addTag('align=' + node.style.textAlign.toLowerCase());
	  }
	  if (node.style.fontWeight >= 700) addTag('b');
	  switch (node.style.fontStyle.toLowerCase()) {
		case 'italic': addTag('i'); break;
	  }
	  switch (node.style.textDecorationLine.toLowerCase()) {
		case 'underline': addTag('u'); break;
		case 'line-through': addTag('s'); break;
	  }
	  if (node.style.color) {
		ctxt.fillStyle = elem.style.color;
		if (ctxt.fillStyle != '#000000' && /^#(?:[a-f0-8]{2}){3,4}$/i.test(ctxt.fillStyle)) {
		  addTag('color=' + ctxt.fillStyle);
		}
	  }
	  if (!text[2]) node.childNodes.forEach(function(node) {
		text[2] += html2php(node, url, tagChain.concat(tags.concat(_tags).map(tag => tag.replace(/=.*$/, ''))));
	  });
	  if (node.nodeName == 'A' && text[2].trim().length <= 0) {
		if (/^(?:https?):$/i.test(node.protocol)) {
		  text[2] = removeRedirect(node.href);
		  tags.splice(-1, 1, 'url');
		} else text[2] = node.href.slice(node.protocol.length);
	  }
	  return text[0].concat((text[1] || text[2] || text[3] ? tags.map(tag => '[' + tag + ']').join('').concat(text[1],
		text[2], text[3], tags.reverse().map(tag => '[/' + tag.replace(/=.*$/, '') + ']').join('')) : ''), text[4]);

	  function addTag(tag) {
		if (tagChain.concat(tags.map(tag => tag.replace(/=.*$/, ''))).includesCaseless(tag.replace(/=.*$/, ''))) return;
		tags.push(tag);
	  }
	}
	case Node.TEXT_NODE:
	  return node.wholeText.replace(/\s+/g, ' ');
	case Node.DOCUMENT_NODE:
	  return html2php(node.body, url);
  }
  return '';
}

function coverPreview(input, imgUrl, size) {
  if (!prefs.auto_preview_cover) return;
  if ((child = document.getElementById('cover-preview')) == null) {
	if (!(input instanceof HTMLElement) || input.parentNode.previousElementSibling == null) return;
	elem = document.createElement('div');
	elem.style = 'padding-top: 10px; float: right; width: 90%;';
	child = document.createElement('img');
	child.id = 'cover-preview';
	elem.append(child);
	var div = document.createElement('div');
	div.id = 'cover-size';
	if (isRequestNew || isRequestEdit) div.style.fontSize = '7.5pt';
	elem.append(div);
	input.parentNode.previousElementSibling.append(document.createElement('br'));
	input.parentNode.previousElementSibling.append(elem);
  }
  if ((div = div || document.getElementById('cover-size')) == null) return;
  if (urlParser.test(imgUrl)) {
	child.onload = function(evt) {
	  this.onload = null;
	  if (!this.naturalWidth || !this.naturalHeight) return; // invalid image
	  (size > 0 ? Promise.resolve(size) : getRemoteFileSize(imgUrl)).then(function(size) {
		var warn = prefs.huge_image_warning && size > prefs.huge_image_warning * 2**20;
		var html = warn ? '<strong style="color: #ff4c4c;">' + formattedSize(size) + '</strong>' : formattedSize(size);
		div.innerHTML = this.naturalWidth + '×' + this.naturalHeight + ' (' + html + ')';
		if (!warn) return;
		addMessage('high cover size (' + formattedSize(size) + ')', 'notice');
	  }.bind(this)).catch(reason => { div.textContent = this.naturalWidth + '×' + this.naturalHeight });
	};
	child.onerror = function(evt) {
	  this.onerror = null;
	  div.textContent = this.src = '';
	  console.warn('Image source cannot be updated:', evt, imgUrl);
	};
	child.src = imgUrl;
  } else div.textContent = child.src = '';
}

function getRemoteFileSize(url) {
  return new Promise(function(resolve, reject) {
	var imageSize, abort = GM_xmlhttpRequest({
	  method: 'GET', url: url, responseType: 'arraybuffer',
	  onreadystatechange: function(response) {
		if (imageSize || response.readyState < XMLHttpRequest.HEADERS_RECEIVED
			|| !/^Content-Length:\s*(\d+)\b/im.test(response.responseHeaders)) return;
		if (!(imageSize = parseInt(RegExp.$1))) return;
		resolve(imageSize);
		abort.abort();
	  },
	  onload: function(response) { // fail-safe
		if (imageSize) return;
		if (response.status >= 200 && response.status < 400) resolve(response.responseText.length /*response.response.byteLength*/);
			else reject(new Error('Image not accessible'));
	  },
	  onerror: response => reject('Image not accessible'),
	  ontimeout: response => reject('Image not accessible'),
	});
  });
}

function cleanupDescriptions(evt) {
  descriptionFields.forEach(function(ID) {
	if ((ref = evt.target.querySelector('textarea#' + ID)) == null || ref.value.length <= 0) return;
	var clean = ref.value
		.replace(/[ \t]*Vinyl rip by \[color=\S+\]\[\/color\]\s*/im, '')
		.replace(/\[u\]Lineage:\[\/u\]\n\n/i, '')
	for (var i = 0; i < 3; ++i) clean = clean.replace(/\s*\[(\w+)(?:=([^\[\]]*))?\]\[\/\1\]/gm, '');
	const drMatch = [
	  /(^| \| )DR(\d+)$\s+/m,
	  /(?:^| \| )DR(\d+)(?=$| \| )/gm,
	];
	var m = /\[hide=DR(\d+)?\]\[pre\]/i.exec(clean);
	//if (m != null && drMatch[0].test(clean) && RegExp.$2 == m[1]) clean = clean.replace(drMatch[0], '$1');
	if (m != null && drMatch[1].test(clean) && RegExp.$1 == m[1]) clean = clean.replace(drMatch[1], '');
	ref.value = clean.replace(/(?:[ \t\xA0]*\r?\n){3,}/g, '\n\n').replace(/[ \t\xA0]+$/gm, '').trim();
  });
  return true;
}

function reInParenthesis(expr) { return new RegExp('\\s+\\([^\\(\\)]*'.concat(expr, '[^\\(\\)]*\\)$'), 'i') }
function reInBrackets(expr) { return new RegExp('\\s+\\[[^\\[\\]]*'.concat(expr, '[^\\[\\]]*\\]$'), 'i') }

function notMonospaced(str) {
  return /[\u0080-\u009F]/.test(str)
// 	|| /[\u0000-\u001F]/.test(str) // Control character
// 	|| /[\u0020-\u007F]/.test(str) // Basic Latin
// 	|| /[\u0080-\u00FF]/.test(str) // Latin-1 Supplement
// 	|| /[\u0100-\u017F]/.test(str) // Latin Extended-A
// 	|| /[\u0180-\u024F]/.test(str) // Latin Extended-B
// 	|| /[\u0250-\u02AF]/.test(str) // IPA Extensions
	|| /[\u02B0-\u02FF]/.test(str) // Spacing Modifier Letters
	|| /[\u0300-\u036F]/.test(str) // Combining Diacritical Marks
	|| /[\u0370-\u03FF]/.test(str) // Greek and Coptic
	|| /[\u0400-\u04FF]/.test(str) // Cyrillic
	|| /[\u0500-\u052F]/.test(str) // Cyrillic Supplement
	|| /[\u0530-\u058F]/.test(str) // Armenian
	|| /[\u0590-\u05FF]/.test(str) // Hebrew
	|| /[\u0600-\u06FF]/.test(str) // Arabic
	|| /[\u0700-\u074F]/.test(str) // Syriac
	|| /[\u0750-\u077F]/.test(str) // Arabic Supplement
	|| /[\u0780-\u07BF]/.test(str) // Thaana
	|| /[\u07C0-\u07FF]/.test(str) // NKo
	|| /[\u0800-\u083F]/.test(str) // Samaritan
	|| /[\u0840-\u085F]/.test(str) // Mandaic
	|| /[\u0860-\u086F]/.test(str) // Syriac Supplement
	|| /[\u08A0-\u08FF]/.test(str) // Arabic Extended-A
	|| /[\u0900-\u097F]/.test(str) // Devanagari
	|| /[\u0980-\u09FF]/.test(str) // Bengali
	|| /[\u0A00-\u0A7F]/.test(str) // Gurmukhi
	|| /[\u0A80-\u0AFF]/.test(str) // Gujarati
	|| /[\u0B00-\u0B7F]/.test(str) // Oriya
	|| /[\u0B80-\u0BFF]/.test(str) // Tamil
	|| /[\u0C00-\u0C7F]/.test(str) // Telugu
	|| /[\u0C80-\u0CFF]/.test(str) // Kannada
	|| /[\u0D00-\u0D7F]/.test(str) // Malayalam
	|| /[\u0D80-\u0DFF]/.test(str) // Sinhala
	|| /[\u0E00-\u0E7F]/.test(str) // Thai
	|| /[\u0E80-\u0EFF]/.test(str) // Lao
	|| /[\u0F00-\u0FFF]/.test(str) // Tibetan
	|| /[\u1000-\u109F]/.test(str) // Myanmar
	|| /[\u10A0-\u10FF]/.test(str) // Georgian
	|| /[\u1100-\u11FF]/.test(str) // Hangul Jamo
	|| /[\u1200-\u137F]/.test(str) // Ethiopic
	|| /[\u1380-\u139F]/.test(str) // Ethiopic Supplement
	|| /[\u13A0-\u13FF]/.test(str) // Cherokee
	|| /[\u1400-\u167F]/.test(str) // Unified Canadian Aboriginal Syllabics
	|| /[\u1680-\u169F]/.test(str) // Ogham
	|| /[\u16A0-\u16FF]/.test(str) // Runic
	|| /[\u1700-\u171F]/.test(str) // Tagalog
	|| /[\u1720-\u173F]/.test(str) // Hanunoo
	|| /[\u1740-\u175F]/.test(str) // Buhid
	|| /[\u1760-\u177F]/.test(str) // Tagbanwa
	|| /[\u1780-\u17FF]/.test(str) // Khmer
	|| /[\u1800-\u18AF]/.test(str) // Mongolian
	|| /[\u18B0-\u18FF]/.test(str) // Unified Canadian Aboriginal Syllabics Extended
	|| /[\u1900-\u194F]/.test(str) // Limbu
	|| /[\u1950-\u197F]/.test(str) // Tai Le
	|| /[\u1980-\u19DF]/.test(str) // New Tai Lue
	|| /[\u19E0-\u19FF]/.test(str) // Khmer Symbols
	|| /[\u1A00-\u1A1F]/.test(str) // Buginese
	|| /[\u1A20-\u1AAF]/.test(str) // Tai Tham
	|| /[\u1AB0-\u1AFF]/.test(str) // Combining Diacritical Marks Extended
	|| /[\u1B00-\u1B7F]/.test(str) // Balinese
	|| /[\u1B80-\u1BBF]/.test(str) // Sundanese
	|| /[\u1BC0-\u1BFF]/.test(str) // Batak
	|| /[\u1C00-\u1C4F]/.test(str) // Lepcha
	|| /[\u1C50-\u1C7F]/.test(str) // Ol Chiki
	|| /[\u1C80-\u1C8F]/.test(str) // Cyrillic Extended C
	|| /[\u1CC0-\u1CCF]/.test(str) // Sundanese Supplement
	|| /[\u1CD0-\u1CFF]/.test(str) // Vedic Extensions
	|| /[\u1D00-\u1D7F]/.test(str) // Phonetic Extensions
	|| /[\u1D80-\u1DBF]/.test(str) // Phonetic Extensions Supplement
	|| /[\u1DC0-\u1DFF]/.test(str) // Combining Diacritical Marks Supplement
// 	|| /[\u1E00-\u1EFF]/.test(str) // Latin Extended Additional
	|| /[\u1F00-\u1FFF]/.test(str) // Greek Extended
	|| /[\u200B-\u200F\u2028\u2029\u203B\u202A-\u202E\u2060-\u206F]/.test(str) //|| /[\u2000-\u206F]/.test(str) // General Punctuation
	|| /[\u2070-\u209F]/.test(str) // Superscripts and Subscripts
// 	|| /[\u20A0-\u20CF]/.test(str) // Currency Symbols
	|| /[\u20D0-\u20FF]/.test(str) // Combining Diacritical Marks for Symbols
// 	|| /[\u2100-\u214F]/.test(str) // Letterlike Symbols
	|| /[\u2150-\u218F]/.test(str) // Number Forms
// 	|| /[\u2190-\u21FF]/.test(str) // Arrows
	|| /[\u2200-\u22FF]/.test(str) // Mathematical Operators
	|| /[\u2300-\u23FF]/.test(str) // Miscellaneous Technical
	|| /[\u2400-\u243F]/.test(str) // Control Pictures
// 	|| /[\u2440-\u245F]/.test(str) // Optical Character Recognition
	|| /[\u2460-\u24FF]/.test(str) // Enclosed Alphanumerics
	|| /[\u2500-\u257F]/.test(str) // Box Drawing
// 	|| /[\u2580-\u259F]/.test(str) // Block Elements
	|| /[\u25A0-\u25FF]/.test(str) // Geometric Shapes
	|| /[\u2600-\u26FF]/.test(str) // Miscellaneous Symbols
	|| /[\u2700-\u27BF]/.test(str) // Dingbats
	|| /[\u27C0-\u27EF]/.test(str) // Miscellaneous Mathematical Symbols-A
	|| /[\u27F0-\u27FF]/.test(str) // Supplemental Arrows-A
	|| /[\u2800-\u28FF]/.test(str) // Braille Patterns
	|| /[\u2900-\u297F]/.test(str) // Supplemental Arrows-B
// 	|| /[\u2980-\u29FF]/.test(str) // Miscellaneous Mathematical Symbols-B
// 	|| /[\u2A00-\u2AFF]/.test(str) // Supplemental Mathematical Operators
	|| /[\u2B00-\u2BFF]/.test(str) // Miscellaneous Symbols and Arrows
	|| /[\u2C00-\u2C5F]/.test(str) // Glagolitic
// 	|| /[\u2C60-\u2C7F]/.test(str) // Latin Extended-C
	|| /[\u2C80-\u2CFF]/.test(str) // Coptic
	|| /[\u2D00-\u2D2F]/.test(str) // Georgian Supplement
	|| /[\u2D30-\u2D7F]/.test(str) // Tifinagh
	|| /[\u2D80-\u2DDF]/.test(str) // Ethiopic Extended
	|| /[\u2DE0-\u2DFF]/.test(str) // Cyrillic Extended-A
	|| /[\u2E00-\u2E7F]/.test(str) // Supplemental Punctuation
	|| /[\u2E80-\u2EFF]/.test(str) // CJK Radicals Supplement
	|| /[\u2F00-\u2FDF]/.test(str) // Kangxi Radicals
	|| /[\u2FF0-\u2FFF]/.test(str) // Ideographic Description Characters
	|| /[\u3000-\u303F]/.test(str) // CJK Symbols and Punctuation
	|| /[\u3040-\u309F]/.test(str) // Hiragana
	|| /[\u30A0-\u30FF]/.test(str) // Katakana
	|| /[\u3100-\u312F]/.test(str) // Bopomofo
	|| /[\u3130-\u318F]/.test(str) // Hangul Compatibility Jamo
	|| /[\u3190-\u319F]/.test(str) // Kanbun
	|| /[\u31A0-\u31BF]/.test(str) // Bopomofo Extended
	|| /[\u31C0-\u31EF]/.test(str) // CJK Strokes
	|| /[\u31F0-\u31FF]/.test(str) // Katakana Phonetic Extensions
	|| /[\u3200-\u32FF]/.test(str) // Enclosed CJK Letters and Months
	|| /[\u3300-\u33FF]/.test(str) // CJK Compatibility
	|| /[\u3400-\u4DBF]/.test(str) // CJK Unified Ideographs Extension A
	|| /[\u4DC0-\u4DFF]/.test(str) // Yijing Hexagram Symbols
	|| /[\u4E00-\u9FFF]/.test(str) // CJK Unified Ideographs
// 	|| /[\uA000-\uA48F]/.test(str) // Yi Syllables
// 	|| /[\uA490-\uA4CF]/.test(str) // Yi Radicals
	|| /[\uA4D0-\uA4FF]/.test(str) // Lisu
	|| /[\uA500-\uA63F]/.test(str) // Vai
	|| /[\uA640-\uA69F]/.test(str) // Cyrillic Extended-B
	|| /[\uA6A0-\uA6FF]/.test(str) // Bamum
	|| /[\uA700-\uA71F]/.test(str) // Modifier Tone Letters
	|| /[\uA720-\uA7FF]/.test(str) // Latin Extended-D
	|| /[\uA800-\uA82F]/.test(str) // Syloti Nagri
	|| /[\uA830-\uA83F]/.test(str) // Common Indic Number Forms
	|| /[\uA840-\uA87F]/.test(str) // Phags-pa
	|| /[\uA880-\uA8DF]/.test(str) // Saurashtra
	|| /[\uA8E0-\uA8FF]/.test(str) // Devanagari Extended
	|| /[\uA900-\uA92F]/.test(str) // Kayah Li
	|| /[\uA930-\uA95F]/.test(str) // Rejang
	|| /[\uA960-\uA97F]/.test(str) // Hangul Jamo Extended-A
	|| /[\uA980-\uA9DF]/.test(str) // Javanese
	|| /[\uA9E0-\uA9FF]/.test(str) // Myanmar Extended-B
	|| /[\uAA00-\uAA5F]/.test(str) // Cham
	|| /[\uAA60-\uAA7F]/.test(str) // Myanmar Extended-A
	|| /[\uAA80-\uAADF]/.test(str) // Tai Viet
	|| /[\uAAE0-\uAAFF]/.test(str) // Meetei Mayek Extensions
	|| /[\uAB00-\uAB2F]/.test(str) // Ethiopic Extended-A
// 	|| /[\uAB30-\uAB6F]/.test(str) // Latin Extended-E
	|| /[\uAB70-\uABBF]/.test(str) // Cherokee Supplement
	|| /[\uABC0-\uABFF]/.test(str) // Meetei Mayek
	|| /[\uAC00-\uD7AF]/.test(str) // Hangul Syllables
	|| /[\uD7B0-\uD7FF]/.test(str) // Hangul Jamo Extended-B
	|| /[\uD800-\uDB7F]/.test(str) // High Surrogates
// 	|| /[\uDB80-\uDBFF]/.test(str) // High Private Use Surrogates
	|| /[\uDC00-\uDFFF]/.test(str) // Low Surrogates
	|| /[\uE000-\uF8FF]/.test(str) // Private Use Area
	|| /[\uF900-\uFAFF]/.test(str) // CJK Compatibility Ideographs
	|| /[\uFB00-\uFB4F]/.test(str) // Alphabetic Presentation Forms
	|| /[\uFB50-\uFDFF]/.test(str) // Arabic Presentation Forms-A
	|| /[\uFE00-\uFE0F]/.test(str) // Variation Selectors
	|| /[\uFE10-\uFE1F]/.test(str) // Vertical Forms
	|| /[\uFE20-\uFE2F]/.test(str) // Combining Half Marks
	|| /[\uFE30-\uFE4F]/.test(str) // CJK Compatibility Forms
	|| /[\uFE50-\uFE6F]/.test(str) // Small Form Variants
	|| /[\uFE70-\uFEFF]/.test(str) // Arabic Presentation Forms-B
	|| /[\uFF00-\uFFEF]/.test(str) // Halfwidth and Fullwidth Forms
	|| /[\uFFF0-\uFFFF]/.test(str) // Specials
// 	|| /[\u10000-\uFFFFF]/.test(str) // Others
}

function makeTimeString(duration) {
  let t = Math.abs(Math.round(duration));
  let H = Math.floor(t / 60 ** 2);
  let M = Math.floor(t / 60 % 60);
  let S = t % 60;
  return (duration < 0 ? '-' : '') + (H > 0 ? H + ':' + M.toString().padStart(2, '0') : M.toString()) +
	':' + S.toString().padStart(2, '0');
}

function timeStringToTime(str) {
  if (!/(-\s*)?\b(\d+(?::\d{2})*(?:\.\d+)?)\b/.test(str)) return null;
  var t = 0, a = RegExp.$2.split(':');
  while (a.length > 0) t = t * 60 + parseFloat(a.shift());
  return RegExp.$1 ? -t : t;
}

function normalizeDate(str) {
  if (typeof str != 'string') return null;
  if (/\b(\d{4}-\d+-\d+|\d{1,2}\/\d{1,2}\/\d{2})\b/.test(str)) return RegExp.$1; // US (clash with BE, IT)
  if (/\b(\d{1,2})\/(\d{1,2})\/(\d{4})\b/.test(str)) return RegExp.$2 + '/' + RegExp.$1 + '/' + RegExp.$3; // UK, IRL, FR
  if (/\b(\d{1,2})-(\d{1,2})-(\d{2})\b/.test(str)) return RegExp.$2 + '/' + RegExp.$1 + '/' + RegExp.$3; // NL
  if (/\b(\d{1,2})\.\s?(\d{1,2})\.\s?(\d{2}|\d{4})\b/.test(str)) return RegExp.$2 + '/' + RegExp.$1 + '/' + RegExp.$3; // AT, CH, DE, LU, CE
  if (/\b(\d{4})\.\s?(\d{1,2})\.\s?(\d{1,2})\b/.test(str)) return RegExp.$2 + '/' + RegExp.$3 + '/' + RegExp.$1; // JP
  return extractYear(str);
}

function extractYear(expr) {
  if (typeof expr == 'number') return Math.round(expr);
  if (typeof expr != 'string') return null;
  if (/\b(\d{4})\b/.test(expr)) return parseInt(RegExp.$1);
  var d = new Date(expr);
  return parseInt(isNaN(d) ? expr : d.getFullYear());
}

function formattedSize(size) {
  return size < 1024**1 ? Math.round(size) + ' B'
	: size < 1024**2 ? (Math.round(size * 10 / 2**10) / 10) + ' KiB'
	: size < 1024**3 ? (Math.round(size * 100 / 2**20) / 100) + ' MiB'
	: size < 1024**4 ? (Math.round(size * 100 / 2**30) / 100) + ' GiB'
	: size < 1024**5 ? (Math.round(size * 100 / 2**40) / 100) + ' TiB'
	: (Math.round(size * 100 / 2**50) / 100) + ' PiB';
}

function safeText(unsafeText) {
  let div = document.createElement('div');
  div.innerText = unsafeText || '';
  return div.innerHTML;
}

function imageClear(evt) {
  evt.target.value = '';
  coverPreview(evt.target, null);
}

function imageDropHandler(evt) { return imageDataHandler(evt, evt.dataTransfer) }
function imagePasteHandler(evt) { return imageDataHandler(evt, evt.clipboardData) }
function imageDataHandler(evt, data) {
  if (!data) return true;
  if (data.files.length > 0) {
	if (!data.files[0].type.toLowerCase().startsWith('image/')) return true;
	evt.target.disabled = true;
	if (evt.target.hTimer) {
	  clearTimeout(evt.target.hTimer);
	  delete evt.target.hTimer;
	}
	evt.target.style.backgroundColor = '#800000';
	let elem = evt.target, file = data.files[0], size = data.files[0].size;
	uploadImages([file]).then(function(urls) {
	  elem.value = urls[0];
	  elem.style.backgroundColor = '#008000';
	  elem.style.color = 'white';
	  elem.hTimer = setTimeout(function() {
		elem.style.backgroundColor = null;
		elem.style.color = null;
		delete elem.hTimer;
	  }, 10000);
	  coverPreview(elem, urls[0], size);
	}).catch(function(error) {
	  elem.style.backgroundColor = null;
	  imageClear(evt);
	  alert(error);
	}).then(function() { elem.disabled = false });
	return false;
  } else if (data.items.length > 0) {
	let links = data.getData('text/uri-list');
	if (links) links = links.split(/\r?\n/); else {
	  links = data.getData('text/x-moz-url');
	  if (links) links = links.split(/\r?\n/).filter((item, ndx) => ndx % 2 == 0);
	  	else if (links = data.getData('text/plain')) links = links.split(/\r?\n/);
	}
	if (Array.isArray(links) && links.length > 0) verifyImageUrl(links[0], true).then(function(imageUrl) {
	  evt.target.value = imageUrl;
	  coverPreview(evt.target, imageUrl);
	  if (!prefs.auto_rehost_cover || imageUrl.toLowerCase().startsWith(ptpimgOrigin)) return;
	  evt.target.disabled = true;
	  rehost2PTPIMG([imageUrl])
		.then(function(urls) { if (urls.length > 0) evt.target.value = urls[0] })
		.catch(e => { alert(e + ' (not rehosted)') })
		.then(function() { evt.target.disabled = false });
	}).catch(function(e) {
	  console.error(e);
	  alert(e);
	});
	return false;
  }
  return true;
}

function descDropHandler(evt) {
  if (evt.dataTransfer == null || evt.shiftKey) return true;
  if (evt.dataTransfer.files.length > 0) {
	let images = [];
	Array.from(evt.dataTransfer.files).forEach(function(file) {
	  switch (file.type) {
		case '':
		  if (!['log'/*, 'nfo'*/].some(ext => file.name.toLowerCase().endsWith('.' + ext))) break;
		case 'text/plain':
		//case 'text/nfo': // malformed encoding
		case 'text/log':
		  evt.target.disabled = true;
		  file.getText(file.name.toLowerCase().endsWith('.nfo') ? 'ibm850' : 'utf-8').then(function(text) {
			var isDR = file.name.toLowerCase().endsWith('foo_dr.txt') && /^Official DR value:\s*DR(\d+)\b/im.test(text);
			if (isDR) var DR = parseInt(RegExp.$1);
			var tag = isDR || file.name.toLowerCase().endsWith('.nfo') ? 'pre' : 'code';
			var php = isDR ? '[hide=DR' + RegExp.$1 + '][' + tag + ']' + text + '[/' + tag + '][/hide]'
				: '[hide=' + file.name + '][' + tag + ']' + text + '[/' + tag + '][/hide]';
			if (evt.target.value.length <= 0) evt.target.value = php; else if (evt.ctrlKey) {
			  evt.target.value = evt.target.value.slice(0, evt.rangeOffset) +
				php + evt.target.value.slice(evt.rangeOffset);
			} else if (isDR && /\[hide=DR\d*\]\[pre\]\[\/pre\]/i.test(evt.target.value)) {
			  evt.target.value = RegExp.leftContext + php.slice(0, -7) + RegExp.rightContext;
			} else if (isDR && /\[hide=DR(\d*)\]((?:\[pre\](foobar2000[\s\S]+?)^\[\/pre\]\s*)+)(?:\[pre\]\[\/pre\])?/im.test(evt.target.value)) {
			  php = '[hide=DR';
			  if (parseInt(RegExp.$1) == DR) php += RegExp.$1;
			  evt.target.value = RegExp.leftContext.concat(php, ']', RegExp.$2.trim(), '\n[pre]', text, '[/pre]', RegExp.rightContext);
			} else if (!isDR && /\[hide\](?:\[code\]\[\/code\])?\[\/hide\]/i.test(evt.target.value)) {
			  evt.target.value = RegExp.leftContext + php + RegExp.rightContext;
			} else if (!isDR && /(\[hide=[^\]]+\])(?:\[code\]\[\/code\])?(\[\/hide\])/i.test(evt.target.value)) {
			  evt.target.value = RegExp.leftContext.concat(RegExp.$1, '[code]', text, '[/code]', RegExp.$2, RegExp.rightContext);
			} else evt.target.value += '\n\n'.concat(php);
		  }).catch(function(e) { alert(e) }).then(function() {
			if (!evt.target.style.background) evt.target.disabled = false;
		  });
		  break;
		case 'image/png':
		case 'image/jpeg':
		case 'image/gif':
		case 'image/bmp':
		//case 'image/webp':
		//case 'image/svg+xml':
		  images.push(file);
		  break;
	  }
	});
	if (images.length > 0) {
	  evt.target.disabled = true;
	  evt.target.style.background = '#FF000040 no-repeat center center url(' + ulImgData + ')';
	  //evt.target.style.background = '#FF000040 no-repeat center center url(https://svgshare.com/i/H16.svg)';
	  let elem = evt.target;
	  uploadImages(images).then(urlHandler.bind({ tag: 'img' })).catch(error => { alert(error) }).then(function() {
		elem.style.background = null;
		elem.disabled = false;
	  });
	}
	return false;
  } else if (evt.dataTransfer.items.length > 0) {
	let content = evt.dataTransfer.getData('text/uri-list');
	if (content) content = content.split(/\r?\n/); else {
	  content = evt.dataTransfer.getData('text/x-moz-url');
	  if (content) content = content.split(/\r?\n/).filter((item, ndx) => ndx % 2 == 0);
	};
	if (Array.isArray(content) && content.length > 0) {
	  verifyImageUrls(content, true).then(function(imageUrls) {
		if (prefs.auto_rehost_cover) {
		  evt.target.disabled = true;
		  rehost2PTPIMG(imageUrls).catch(function(e) {
			addMessage('PTPimg ' + e + ' (not rehosted)', 'warning');
			return imageUrls;
		  }).then(urlHandler.bind({ tag: 'img' })).then(() => { evt.target.disabled = false });
		} else urlHandler.bind({ tag: 'img' })(content);
	  }).catch(function(e) {
		let as = domParser.parseFromString(evt.dataTransfer.getData('text/html'), 'text/html').body.querySelectorAll('a');
		Promise.all(content.map(urlResolver))
		  .then(resolved => urlHandler.bind({ tag: 'url', titles: Array.from(as).map(a => a.textContent.trim()) })(resolved));
	  });
	} else if (content = evt.dataTransfer.getData('text/html')) {
	  textHandler(html2php(domParser.parseFromString(content, 'text/html')).collapseGaps());
	} else if (content = evt.dataTransfer.getData('text/plain')) {
	  textHandler(content);
	}
	return false;
  }
  return true;

  function urlHandler(urls) {
	const rx = new RegExp('\\[' + this.tag + '\\]\\[\\/' + this.tag + '\\]', 'i');
	urls.forEach(function(url, ndx) {
	  if (url.length <= 0 || !urlParser.test(url)) return;
	  var php = '[' + this.tag;
	  php += Array.isArray(this.titles) && this.titles[ndx] ? '=' + url + ']' + this.titles[ndx] : ']' + url;
	  php += '[/' + this.tag + ']';
	  if (evt.target.value.trimRight().length <= 0) evt.target.value = php; else if (evt.ctrlKey) {
		evt.target.value = evt.target.value.slice(0, evt.rangeOffset) + php + evt.target.value.slice(evt.rangeOffset);
	  } else if (rx.test(evt.target.value)) {
		evt.target.value = RegExp.leftContext + php + RegExp.rightContext;
	  } else evt.target.value = evt.target.value.trimRight().concat('\n\n', php);
	}.bind(this));
  }
  function textHandler(php) {
	if (evt.target.value.length <= 0) evt.target.value = php; else if (evt.ctrlKey) {
	  evt.target.value = evt.target.value.slice(0, evt.rangeOffset) + php + evt.target.value.slice(evt.rangeOffset);
	} else evt.target.value += '\n\n'.concat(php);
  }
}

function descPasteHandler(evt) {
  if (evt.clipboardData == null) return true;
  if (evt.clipboardData.files.length > 0) {
	let images = Array.from(evt.clipboardData.files)
		.filter(file => ['image/png', 'image/jpeg', 'image/gif', 'image/bmp', 'image/webp', 'image/svg+xml']
			.some(mimeType => file.type == mimeType))
	if (images.length <= 0) return true;
	evt.target.disabled = true;
	evt.target.style.background = '#FF000040 no-repeat center center url(' + ulImgData + ')';
	uploadImages(images).then(function(urls) {
	  var phpBB = urls.map(url => '[img]'.concat(url, '[/img]')).join('\n');
	  evt.target.value = evt.target.value.slice(0, evt.target.selectionStart).concat(phpBB, evt.target.value.slice(evt.target.selectionEnd));
	}).catch(e => { alert(e) }).then(function() {
	  evt.target.style.background = null;
	  evt.target.disabled = false;
	});
	return false;
  } else if (evt.clipboardData.items.length > 0) {
	let content = evt.clipboardData.getData('text/html');
	if (!content) return true;
	content = html2php(domParser.parseFromString(content, 'text/html')).collapseGaps();
	var selStart = evt.target.selectionStart;
	evt.target.value = evt.target.value.slice(0, evt.target.selectionStart)
	  .concat(content, evt.target.value.slice(evt.target.selectionEnd));
	evt.target.setSelectionRange(selStart + content.length, selStart + content.length);
	return false;
  }
  return true;
}

function uaInsert(evt) {
  if (evt.clipboardData) evt.target.value = '';
  if (!(prefs.autfill_delay > 0)) return true;
  autofill = true;
  setTimeout(fillFromText, prefs.autfill_delay);
}

// Firefox accepts dropped playlist in malformed form, try to detect and correct it
function fixFirefoxDropBug(evt) {
  if (evt.target == null || evt.target.value.length <= 0) return true;
  var tl = (Math.sqrt(4 * evt.target.value.split('\n').length - 3) + 1) / 2;
  if (tl < 2 || tl != Math.floor(tl) || evt.target.value.length % tl != 0) return true;
  var l = evt.target.value.length / tl;
  var s = evt.target.value.slice(0, l);
  for (var i = 1; i < tl; ++i) if (evt.target.value.slice(i * l, (i + 1) * l) != s) return true;
  evt.target.value = s;
  return true;
}

function clear0(evt) { if (evt.target.value.length > 0) evt.target.value = '' }
function clear1(evt) { if (evt.buttons == 4) clear0(evt) }
function voidDragHandler0(evt) { return false }
function voidDragHandler1(evt) {
  return !evt.dataTransfer.types.includes('Files') || evt.target.nodeName == 'TEXTAREA'
	  || evt.target.nodeName == 'INPUT' && evt.target.type == 'file'
}

function removeRedirect(uri) {
  return typeof uri != 'string' ? null : [
	'www.anonymz.com/?', 'www.anonymz.com?',
	'anonymz.com/?', 'anonymz.com?',
	'anonym.to/?', 'anonym.to?',
	'dereferer.me/?',
	'reho.st/',
  ].reduce(function(acc, it) {
	if (acc.toLowerCase().startsWith('https://' + it)) return acc.slice(it.length + 8);
	if (acc.toLowerCase().startsWith('http://' + it)) return acc.slice(it.length + 7);
	return acc;
  }, uri);
}

function urlResolver(url) {
  if (!urlParser.test(url)) return Promise.reject('Invalid URL:\n\n'.concat(url));
  try { if (!(url instanceof URL)) url = new URL(url) } catch(e) { return Promise.reject(e) }
  switch (url.hostname) {
	case 'rutracker.org':
	  if (url.pathname != '/forum/out.php') break;
	  return globalFetch(url, { method: 'HEAD' }).then(response => urlResolver(response.finalUrl));
	case 'www.anonymz.com': case 'anonymz.com': case 'anonym.to': case 'dereferer.me':
	  var resolved = decodeURIComponent(url.search.slice(1));
	  return urlParser.test(resolved) ? urlResolver(resolved) : genericResolver();
	case 'reho.st':
	  resolved = url.pathname.concat(url.search, url.hash).slice(1);
	  if (/\b(?:https?):\/\/(?:\w+\.)*discogs\.com\//i.test(resolved)) break;
	  return urlParser.test(resolved) ? urlResolver(resolved) : genericResolver();
	// URL shorteners
	case 'tinyurl.com': case 'bit.ly': case 'j.mp': case 't.co': case 'goo.gl': case 'apple.co':
	case 'rebrand.ly': case 'b.link': case 't2m.io': case 'zpr.io': case 'yourls.org':
	  return genericResolver();
  }
  return Promise.resolve(url.href);

  function genericResolver() {
	return globalFetch(url).then(function(response) {
	  var redirect = response.document.querySelector('meta[http-equiv="refresh"]');
	  if (redirect != null && (redirect = redirect.content.replace(/^.*?\b(?:URL)\s*=\s*/i, '')) != url.href)
		return urlResolver(redirect);
	  if (/^ *(?:Location) *: *(\S+) *$/im.test(response.responseHeaders) && (redirect = RegExp.$1) != url.href)
		return urlResolver(redirect);
	  return response.finalUrl != url.href ? urlResolver(response.finalUrl) : Promise.resolve(url.href);
	});
  }
}

function tidalRlsParser(url) {
  return /^https?:\/\/(?:\w+\.)*tidal\.com\//.test(url)
  	&& (/\/album\/(\d+)(?:\/|$)/i.test(url) || /\/album(?:\/|\?).*\b(?:albumId)=(\d+)\b/i.test(url));
}

function verifyImageUrl(url, resolve = false) {
  return urlResolver(url).then(function(url) {
	//if (!strict && imageExtensions.some(ext => url.toLowerCase().endsWith('.'.concat(ext)))) return Promise.resolve(url); // weak
	return new Promise(function(resolve, reject) {
	  var img = new Image();
	  img.onload = load => { resolve(url) };
	  img.onerror = function(error) {
		if (img.src.includes('?')) img.src = url.replace(/\?.*?(?=\#|$)/, '');
			else reject('Not valid image:\n\n'.concat(url));
	  };
	  img.ontimeout = timeout => { reject('Image load timed out:\n\n'.concat(url)) };
	  img.src = url;
	}).catch(reason => resolve ? imageUrlResolver(url) : Promise.reject(reason));
  });
}
function verifyImageUrls(urls, resolve = false) {
  return Array.isArray(urls) ? Promise.all(urls.map(url => verifyImageUrl(url, resolve))) : Promise.reject('URLs not an array');
}

function imageUrlResolver(url) {
  return urlResolver(url).then(url => verifyImageUrl(url).catch(function(reason) {
	try { url = new URL(url) } catch(e) { return Promise.reject(e) }
	const notFound = Promise.reject('No title image for this URL');
	if (url.hostname.endsWith('bandcamp.com')) return globalFetch(url)
	  .then(response => verifyImageUrl(response.document.querySelector('div#tralbumArt > a.popupImage').href));
	else if (url.hostname.endsWith('.7digital.com')) return globalFetch(url)
	  .then(response => verifyImageUrl(response.document.querySelector('span.release-packshot-image > img[itemprop="image"]').src));
	else if (url.hostname.startsWith('hdtracks.') || url.hostname.startsWith('www.hdtracks.')) return globalFetch(url)
	  .then(response => verifyImageUrl(response.document.querySelector('p.product-image > img').src));
	else switch (url.hostname) {
	  // general image hostings
	  case 'imgur.com':
		return globalFetch(url).then(response => verifyImageUrl(response.document.querySelector('link[rel="image_src"]').href));
	  case 'pixhost.to':
		if (!url.pathname.startsWith('/show/')) break;
		return globalFetch(url).then(response => verifyImageUrl(response.document.querySelector('img#image').src));
	  case 'imgbb.com': case 'ibb.co':
		return globalFetch(url)
		  .then(response => verifyImageUrl(response.document.querySelector('div#image-viewer-container img').src));
	  case 'www.imagevenue.com':
		return globalFetch(url, { headers: { Referer: 'http://www.imagevenue.com/' } })
		  .then(response => verifyImageUrl((response.document.querySelector('div.card img')
			|| response.document.querySelector('img#thepic')).src));
	  case 'www.imageshack.us': case 'imageshack.us':
		return globalFetch(url).then(response => verifyImageUrl(response.document.querySelector('a#share-dl').href));
	  case 'redacted.ch':
		if (url.pathname != '/image.php') break;
		return globalFetch(url, { method: 'HEAD' }).then(response => verifyImageUrl(response.finalUrl));
	  case 'demo.cloudimg.io':
		if (!/\b(https?:\/\/\S+)$/.test(url.pathname.concat(url.search, url.hash))) break;
		var resolved = RegExp.$1;
		if (/\b(?:https?):\/\/(?:\w+\.)*discogs\.com\//i.test(resolved)) break;
		return imageResolver(resolved);
	  case 'fastpic.ru':
		if (url.pathname.startsWith('/view/'))
		  return globalFetch(url).then(response => imageUrlResolver(response.document.querySelector('a.img-a').href));
		if (url.pathname.startsWith('/fullview/'))
		  return globalFetch(url).then(response => verifyImageUrl(response.document.getElementById('image').src));
		break;
	  case 'radikal.ru': case 'a.radikal.ru':
		return globalFetch(url).then(response => verifyImageUrl(response.document.querySelector('div.mainBlock img').src));
	  case 'imageban.ru': case 'ibn.im':
		return globalFetch(url)
		  .then(response => verifyImageUrl(response.document.getElementById('img_main').src /* dataset.original */));
	  // music-related
	  case 'www.musicbrainz.org': case 'musicbrainz.org':
		if (!['release', 'release-group'].some(branch => url.pathname.includes('/'.concat(branch, '/')))) break;
		return globalFetch(url).then(function(response) {
		  var node = response.document.querySelector('a.artwork-image');
		  if (node != null) return verifyImageUrl(node.href);
		  return (node = response.document.querySelector('div.cover-art > img')) != null ?
			verifyImageUrl(node.src) : Promise.reject('Cover missing');
		});
	  case 'music.apple.com':
		if (!itunesRlsParser.test(url)) break;
		return globalFetch(url).then(function(response) {
		  var meta = response.document.querySelector('meta[property="og:image"][content]');
		  if (meta == null || !meta.content) return notFound;
		  return verifyImageUrl(meta.content.replace(/\/\d+x\d+\w*(?=\.\w+$)/, '/100000x100000-999'))
		  	.catch(reason => verifyImageUrl(meta.content));
		});
	  case 'www.deezer.com': case 'deezer.com':
		return globalFetch(url).then(function(response) {
		  var meta = response.document.querySelector('meta[property="og:image"][content]');
		  if (meta == null || !meta.content) return notFound;
		  return verifyImageUrl(meta.content.replace(/\/\d+x\d+\w*(?=\.\w+$)/, '/1400x1400-000000-100-0-0'))
			.catch(reason => verifyImageUrl(meta.content));
		});
	  case 'www.qobuz.com': case 'qobuz.com':
		if (!url.pathname.includes('/album/')) break;
		return globalFetch(url).then(function(response) {
		  var img = response.document.querySelector('div.album-cover > img');
		  if (img == null) return notFound;
		  return verifyImageUrl(img.src.replace(/_\d{3}(?=\.\w+$)/, '_max')).catch(reason => verifyImageUrl(img.src));
		});
	  case 'www.prestomusic.com': case 'prestomusic.com':
		if (!url.pathname.includes('/products/')) break;
		return globalFetch(url)
		  .then(response => verifyImageUrl(response.document.querySelector('div.c-product-block__aside > a').href.replace(/\?\d+$/)));
	  case 'www.bontonland.cz':case 'bontonland.cz':
		return globalFetch(url)
		  .then(response => verifyImageUrl(response.document.querySelector('a.detailzoom').href));
	  case 'www.nativedsd.com':case 'nativedsd.com':
		if (!url.pathname.includes('/albums/')) break;
		return globalFetch(url)
		  .then(response => verifyImageUrl(response.document.querySelector('a#album-cover').href));
	  case 'www.prostudiomasters.com': case 'prostudiomasters.com':
		if (!url.pathname.includes('/album/')) break;
		return globalFetch(url).then(function(response) {
		  var a = response.document.querySelector('img.album-art');
		  return verifyImageUrl(a.currentSrc || a.src);
		});
	  case 'www.e-onkyo.com': case 'e-onkyo.com':
		if (!url.pathname.includes('/album/')) break;
		return globalFetch(url).then(function(response) {
		  var a = response.document.querySelector('figure > a.colorbox');
		  return verifyImageUrl(new URL(response.finalUrl).origin.concat(a.pathname));
		})
	  case 'store.acousticsounds.com':
		return globalFetch(url).then(function(response) {
		  var link = response.document.querySelector('div#detail > link[rel="image_src"]');
		  return verifyImageUrl(link.href.replace(/\/medium\//i, '/large/'))
		  	.catch(reason => verifyImageUrl(link.href));
		});
	  case 'www.indies.eu': case 'indies.eu':
		if (!url.pathname.includes('/alba/')) break;
		return globalFetch(url)
		  .then(response => verifyImageUrl(response.document.querySelector('div.obrazekDetail > img').src));
	  case 'www.beatport.com': case 'beatport.com':
		if (!url.pathname.includes('/release/')) break;
		return globalFetch(url)
		  .then(response => verifyImageUrl(response.document.querySelector('div > img.interior-release-chart-artwork').src));
	  case 'www.supraphonline.cz': case 'supraphonline.cz':
		if (!url.pathname.includes('/album/')) break;
		return globalFetch(url)
		  .then(response => verifyImageUrl(response.document.querySelector('meta[itemprop="image"]').content.replace(/\?.*$/, '')));
	  case 'vgmdb.net':
		if (!url.pathname.includes('/album/')) break;
		return globalFetch(url).then(function(response) {
		  var div = response.document.querySelector('div#coverart');
		  return /\b(?:url)\s*\(\"(.*)"\)/i.test(div.style['background-image']) ? verifyImageUrl(RegExp.$1) : notFound;
		});
	  case 'www.ototoy.jp': case 'ototoy.jp':
		return globalFetch(url).then(function(response) {
		  var img = response.document.querySelector('div#tralbumArt > a.popupImage');
		  return verifyImageUrl(img.dataset.src || img.src);
		});
	  case 'music.yandex.ru':
		if (!url.pathname.includes('/album/')) break;
		return globalFetch(url).then(function(response) {
		  var script = response.document.querySelector('script.light-data');
		  return script != null ? verifyImageUrl(JSON.parse(script.text).image) : notFound;
		});
	  case 'mora.jp/':
		if (!url.pathname.includes('/package/')) break;
		return loadMoraMetadata(url).then(packageMeta => verifyImageUrl(packageMeta.packageUrl.concat(packageMeta.fullsizeimage)));
	}
	return globalFetch(url, { headers: { 'Referer': url.origin } }).then(function(response) {
	  var meta = [
		'head > meta[property="og:image"][content]', 'head > meta[itemprop="image"][content]', 'head > meta[name="og:image"][content]',
	  ].reduce((acc, selector) => acc || response.document.querySelector(selector), null);
	  return meta != null && meta.content ? verifyImageUrl(meta.content) : notFound;
	});
  }));
}

function uploadImages(files, elem) {
  if (typeof files != 'object') return Promise.reject('Invalid argument');
  if (!Array.isArray(files)) files = Array.from(files);
  //if (files.length > 1) files.push(files.shift()); // Windows bug
  var frs = files.filter(function(file) {
	return file instanceof File && imageExtensions.some(ext => file.type == 'image/' + ext);
  })/*.sort((file1, file2) => file1.name.localeCompare(file2.name))*/.map(file => new Promise(function(resolve, reject) {
	var reader = new FileReader();
	reader.onload = function() { resolve({ name: file.name, type: file.type, data: reader.result }) };
	reader.onerror = reader.ontimeout = function() { reject('FileReader error (' + file.name + ')') };
	reader.readAsBinaryString(file);
  }));
  return frs.length > 0 ? Promise.all(frs).then(images => upload2PTPIMG(images, elem).catch(function(reason) {
	addMessage('Upload to PTPIMG failed (' + reason + '), falling back to malzo.com', 'warning');
	return upload2ImageHost('malzo.com', images, elem);
  }).catch(function(reason) {
	addMessage('Upload to malzo.com failed (' + reason + '), falling back to imgbb.com', 'warning');
	return upload2ImageHost('imgbb.com', images, elem);
  }).catch(function(reason) {
	addMessage('Upload to imgbb.com failed (' + reason + '), falling back to pixhost.to', 'warning');
	return upload2PixHost(images, elem);
  }).catch(function(reason) {
	addMessage('Upload to pixhost.to failed (' + reason + '), falling back to catbox.moe', 'warning');
	return upload2Catbox(images, elem);
  }).catch(function(reason) {
	addMessage('Upload to catbox.moe failed (' + reason + '), falling back to imgbox.com', 'warning');
	return upload2ImgBox(images, elem);
  }).catch(function(reason) {
	addMessage('Upload to imgbox.com failed (' + reason + '), falling back to jerking.empornium.ph', 'warning');
	return upload2ImageHost('jerking.empornium.ph', images, elem);
  }).catch(reason => Promise.reject('Upload failed to all hosts'))) : Promise.reject('Nothing to upload');
}

function upload2PTPIMG(images, elem) {
  if (!Array.isArray(images)) return Promise.reject('invalid argument');
  if (images.length <= 0) return Promise.reject('nothing to upload');
  return getPTPIMGapiKey().then(apiKey => new Promise(function(resolve, reject) {
	var now = Date.now();
	const boundary = '----WebKitFormBoundary'.concat(now.toString(16).toUpperCase());
	var formData = '--' + boundary + '\r\n';
	images.filter(function(image) {
	  return image.data && image.name && ['png', 'jpg', 'jpeg', 'gif', 'bmp'].some(ext => image.type == 'image/'.concat(ext));
	}).forEach(function(image, ndx) {
	  formData += 'Content-Disposition: form-data; name="file-upload[' + ndx + ']"; filename="' + image.name.toASCII() + '"\r\n';
	  formData += 'Content-Type: ' + image.type + '\r\n\r\n';
	  formData += image.data + '\r\n';
	  formData += '--' + boundary + '\r\n';
	});
	formData += 'Content-Disposition: form-data; name="api_key"\r\n\r\n';
	formData += apiKey + '\r\n';
	formData += '--' + boundary + '--\r\n';
	GM_xmlhttpRequest({
	  method: 'POST',
	  url: ptpimgOrigin + '/upload.php',
	  responseType: 'json',
	  headers: {
		'Accept': 'application/json',
		'Content-Type': 'multipart/form-data; boundary=' + boundary,
		'Content-Length': formData.length,
	  },
	  data: formData,
	  binary: true,
	  timeout: Math.max(Math.ceil(formData.length / ulTimeFactor), 10000),
	  onload: function(response) {
		if (response.status >= 200 && response.status < 400) {
		  if (response.response) resolve(response.response.map(item => ptpimgOrigin + '/' + item.code + '.' + item.ext));
			else reject('void response');
		} else reject(defaultErrorHandler(response));
	  },
	  onprogress: elem instanceof HTMLElement && 'value' in elem ? function(progress) {
		var pct = progress.position * 100 / progress.total;
		//elem.value = 'Uploading... (' + Math.round(pct) + '%)';
	  } : undefined,
	  onerror: error => reject(defaultErrorHandler(error)),
	  ontimeout: timeout => reject(defaultTimeoutHandler(timeout)),
	});
  }));
}

function rehost2PTPIMG(urls) {
  if (!Array.isArray(urls)) return Promise.reject('Invalid parameter');
  if (urls.length <= 0) return Promise.resolve([]); //Promise.reject('Nothing to rehost');
  return Promise.all(urls.map(function(url) {
	if (!urlParser.test(url)) return Promise.reject('URL not valid ('.concat(url, ')'));
	var hostname = new URL(url).hostname;
	if (hostname.endsWith('discogs.com')) {
	  return verifyImageUrl('https://reho.st/'.concat(url))
		.catch(reason => rehost2CatBox(url))
		.catch(reason => rehost2PixHost(url))
		.catch(reason => reupload2PTPIMG(url));
	} else if (!imageExtensions.slice(0, 5).some(ext => url.toLowerCase().endsWith('.'.concat(ext)))) {
	  return verifyImageUrl(url.concat('#.jpg'))
		.catch(reason => rehost2ImageHost('malzo.com', url))
		.catch(reason => rehost2PixHost(url))
		.catch(reason => rehost2ImageHost('imgbb.com', url))
		.catch(reason => rehost2Imgur(url));
	}
	return verifyImageUrl(url);
  })).then(imageUrls => getPTPIMGapiKey().then(function(apiKey) {
	var formData = new URLSearchParams({
	  'link-upload': imageUrls.join('\r\n'),
	  'api_key': apiKey,
	});
	return globalFetch(ptpimgOrigin + '/upload.php', {
	  responseType: 'json',
	  timeout: imageUrls * rehostTimeout,
	}, formData).then(function(response) {
	  if (!response.response) return Promise.reject('PTPIMG void response');
	  if (!response.response.length < imageUrls.length)
		return Promise.reject(`not all images rehosted (${response.response.length}/${imageUrls.length})`);
	  return response.response.map(item => ptpimgOrigin.concat('/', item.code, '.', item.ext));
	});
  }));
}

function reupload2PTPIMG(imgUrl) {
  console.warn('PTPIMG rehoster fallback to local reupload');
  return globalFetch(imgUrl, { responseType: 'blob' }).then(function(response) {
	var image = {
	  name: imgUrl.replace(/^.*\//, ''),
	  data: response.responseText,
	};
	switch (imgUrl.replace(/^.*\./, '').toLowerCase()) {
	  case 'jpg': case 'jpeg': case 'jfif': image.type = 'image/jpeg'; break;
	  case 'png': image.type = 'image/png'; break;
	  case 'gif': image.type = 'image/gif'; break;
	  case 'bmp': image.type = 'image/bmp'; break;
	  default: return Promise.reject('Unsupported extension');
	}
	return upload2PTPIMG([image]).then(imgUrls => imgUrls[0]);
  });
}

function getPTPIMGapiKey() {
  if (prefs.ptpimg_api_key) return Promise.resolve(prefs.ptpimg_api_key);
  try {
	var apiKey = JSON.parse(window.localStorage.ptpimg_it).api_key;
	if (apiKey) {
	  GM_setValue('ptpimg_api_key', prefs.ptpimg_api_key = apiKey);
	  return Promise.resolve(apiKey);
	}
  } catch(e) { if (prefs.diag_mode) console.debug('getPTPIMGapiKey():', e) }
  return globalFetch(ptpimgOrigin).then(function(response) {
	if ((apiKey = response.document.getElementById('api_key')) == null) {
	  let counter = GM_getValue('ptpimg_reminder_read', 0);
	  if (counter < 5) {
		alert(`
PTPIMG API key could not be captured. Please login to ${ptpimgOrigin}/ and redo the action.

If you don\'t have PTPIMG account, consider to set auto_rehost_cover to false in preferences
(Tampermonkey extension menu -> right click to Upload Assistant -> Storage tab)

Direct images uploading is still available to fallback image hosts (proxied).
`);
		GM_setValue('ptpimg_reminder_read', ++counter);
	  }
	  return Promise.reject('PTPIMG API key not configured');
	}
	if (!apiKey.value) return Promise.reject('Assertion failed: missing PTPIMG API key');
	GM_setValue('ptpimg_api_key', prefs.ptpimg_api_key = apiKey.value);
	Promise.resolve(prefs.ptpimg_api_key).then(apiKey => { alert(`Your PTPIMG API key [${apiKey}] was successfully configured`) });
	return prefs.ptpimg_api_key;
  });
}

function upload2ImageHost(hostname, images, elem) {
  if (!Array.isArray(images)) return Promise.reject('invalid argument');
  if (images.length <= 0) return Promise.reject('nothing to upload');
  const anonSessionLimits = {
	'malzo.com': 2,
	'imgbb.com': 2,
	'jerking.empornium.ph': 5,
	'free-picload.com': 50,
  };
  return setImageHostSession(hostname).then(session => Promise.all(images.map(image => new Promise(function(resolve, reject) {
	switch (hostname) {
	  case 'malzo.com':
	  case 'jerking.empornium.ph':
	  case 'free-picload.com':
		if (!['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp'].some(ext => image.type == 'image/'.concat(ext)))
		  throw 'MIME type not supported: '.concat(image.type);
		break;
	  case 'imgbb.com':
		if (!['png', 'jpg', 'jpeg', 'gif', 'bmp', 'webp', 'tif', 'tiff', 'heic'].some(ext => image.type == 'image/'.concat(ext)))
		  throw 'MIME type not supported: '.concat(image.type);
		break;
	}
	var anonSessionLimit = anonSessionLimits[hostname.toLowerCase()];
	if (!session.username && anonSessionLimit > 0 && image.size > anonSessionLimit * 2**20)
	  throw 'image size exceeds anonymous upload limit';
	const boundary = '----WebKitFormBoundary'.concat(Date.now().toString(16).toUpperCase());
	var formData = '--' + boundary + '\r\n', params = Object.assign({
	  action: 'upload',
	  type: 'file',
	  nsfw: 0,
	}, session);
	Object.keys(params).forEach(function(field, index, arr) {
	  formData += 'Content-Disposition: form-data; name="' + field + '"\r\n\r\n';
	  formData += params[field] + '\r\n';
	  formData += '--' + boundary + '\r\n';
	});
	formData += 'Content-Disposition: form-data; name="source"; filename="' + image.name.toASCII() + '"\r\n';
	formData += 'Content-Type: ' + image.type + '\r\n\r\n' + image.data + '\r\n';
	formData += '--' + boundary + '--\r\n';
	GM_xmlhttpRequest({
	  method: 'POST',
	  url: 'https://'.concat(hostname, '/json'),
	  responseType: 'json',
	  headers: {
		'Accept': 'application/json',
		'Content-Type': 'multipart/form-data; boundary=' + boundary,
		'Content-Length': formData.length,
		'Referer': 'https://'.concat(hostname, '/'),
	  },
	  data: formData,
	  binary: true,
	  timeout: Math.max(Math.ceil(formData.length / ulTimeFactor), 10000),
	  onload: function(response) {
		if (response.status >= 200 && response.status < 400) {
		  if (response.response.success) resolve(response.response.image.url);
		  	else reject(response.response.error.message.concat(' (', response.response.status_code, ')'));
		} else reject(defaultErrorHandler(response));
	  },
	  onerror: error => reject(defaultErrorHandler(error)),
	  ontimeout: timeout => reject(defaultTimeoutHandler(timeout)),
	});

	function formField(key, value) {
	  return 'Content-Disposition: form-data; name="' + key + '"\r\n\r\n' + value + '\r\n--' + boundary;
	}
  }))));
}

function rehost2ImageHost(hostname, url) {
  return verifyImageUrl(url).then(imageUrl => setImageHostSession(hostname).then(function(session) {
	var formData = new URLSearchParams(Object.assign({
	  action: 'upload',
	  type: 'url',
	  nsfw: 0,
	  source: imageUrl,
	}, session));
	return globalFetch('https://'.concat(hostname, '/json'), {
	  responseType: 'json',
	  headers: { 'Referer': 'https://'.concat(hostname, '/') },
	  timeout: rehostTimeout,
	}, formData).then(function(response) {
	  return response.response.success ? response.response.image.url
		: Promise.reject(hostname.concat(': ', response.response.error.message,' (', response.response.status_code, ')'));
	});
  }));
}

function setImageHostSession(hostname) {
  return globalFetch('https://'.concat(hostname, '/')).then(function(response) {
	if (!/\b(?:auth_token)\s*=\s*"(\w+)"/.test(response.responseText)) return Promise.reject('Auth token detection failure');
	var session = {
	  auth_token: RegExp.$1,
	  timestamp: Date.now(),
	};
	if (getUser(response)) return session;
	if (/^([\w\-]+)(?:\.[\w\-]+)+$/.test(hostname)) var hostPrefix = RegExp.$1.toLowerCase().concat('_');
	if (hostname.toLowerCase() == 'free-picload.com') hostPrefix = 'picload_';
	if (!hostPrefix || !prefs[hostPrefix.concat('uid')] || !prefs[hostPrefix.concat('password')]) return session;
	var formData = new URLSearchParams({
	  'login-subject': prefs[hostPrefix.concat('uid')],
	  'password': prefs[hostPrefix.concat('password')],
	  'auth_token': session.auth_token,
	});
	return new Promise(function(resolve, reject) {
	  GM_xmlhttpRequest({ method: 'POST', url: 'https://'.concat(hostname, '/login'),
		headers: {
		  'Accept': '*/*',
		  'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
		  'Content-Length': formData.toString().length,
		  'Referer': 'https://'.concat(hostname, '/'),
		}, data: formData.toString(), onload: function(response) {
		  // Server response HTTP/400 - "Bad Request" even if the login succeeds
		  if (response.status < 200 || response.status > 400) reject(defaultErrorHandler(response)); else {
			if (getUser(response) && prefs.diag_mode) console.debug(hostname, 'authorized token:', session.auth_token);
			resolve(session);
		  }
		},
		onerror: error => reject(defaultErrorHandler(error)),
		ontimeout: timeout => reject(defaultTimeoutHandler(timeout)),
	  });
	});

	function getUser(response) {
	  if (!/\b(?:logged_user)\s*=\s*(\{.*?\});/.test(response.responseText)) return false;
	  try {
		let logged_user = JSON.parse(RegExp.$1);
		session.username = logged_user.username;
		session.userid = logged_user.id;
		return true;
	  } catch(e) {
		console.warn(e);
		return false;
	  }
	}
  });
}

function upload2PixHost(images, elem) {
  if (!Array.isArray(images)) return Promise.reject('invalid argument');
  if (images.length <= 0) return Promise.reject('nothing to upload');
  return Promise.all(images.map(image => new Promise(function(resolve, reject) {
	if (!['png', 'jpg', 'jpeg', 'gif'].some(ext => image.type == 'image/'.concat(ext)))
	  throw 'MIME type not supported: '.concat(image.type);
	var now = Date.now();
	const boundary = '----WebKitFormBoundary'.concat(now.toString(16).toUpperCase());
	var formData = '--' + boundary + '\r\n';
	formData += 'Content-Disposition: form-data; name="img"; filename="' + image.name.toASCII() + '"\r\n';
	formData += 'Content-Type: ' + image.type + '\r\n\r\n';
	formData += image.data + '\r\n';
	formData += '--' + boundary + '\r\n';
	formData += 'Content-Disposition: form-data; name="content_type"\r\n\r\n';
	formData += '0\r\n';
	formData += '--' + boundary + '--\r\n';
	GM_xmlhttpRequest({
	  method: 'POST',
	  url: 'https://api.pixhost.to/images',
	  responseType: 'json',
	  headers: {
		'Accept': 'application/json',
		'Content-Type': 'multipart/form-data; boundary=' + boundary,
		'Content-Length': formData.length,
	  },
	  data: formData,
	  binary: true,
	  timeout: Math.max(Math.ceil(formData.length / ulTimeFactor), 10000),
	  onload: function(response) {
		if (response.status >= 200 && response.status < 400) resolve(response.response.show_url);
			else reject(defaultErrorHandler(response));
	  },
	  onerror: error => reject(defaultErrorHandler(error)),
	  ontimeout: timeout => reject(defaultTimeoutHandler(timeout)),
	});
  }).then(imageUrlResolver)));
}

function rehost2PixHost(url) {
  return verifyImageUrl(url).then(function(imageUrl) {
	var form = new URLSearchParams({
	  imgs: imageUrl,
	  content_type: 0,
	  tos: 'on',
	});
	return globalFetch('https://pixhost.to/remote/', {
	  responseType: 'text',
	  timeout: rehostTimeout,
	}, form).then(function(response) {
	  if (!/\b(?:upload_results)\s*=\s*(\{.*\});$/m.test(response.responseText)) return Promise.reject('Pixhost.to: page parsing error');
	  return imageUrlResolver(JSON.parse(RegExp.$1).images[0].show_url);
	});
  });
}

function upload2Catbox(images, elem) {
  if (!Array.isArray(images)) return Promise.reject('Invalid argument');
  if (images.length <= 0) return Promise.reject('Nothing to upload or format not supported');
  return getCatBoxUserHash().catch(reason => undefined).then(userHash => Promise.all(images.map(image => new Promise(function(resolve, reject) {
	var now = Date.now();
	const boundary = '----WebKitFormBoundary'.concat(now.toString(16).toUpperCase());
	var formData = '--' + boundary + '\r\n';
	formData += 'Content-Disposition: form-data; name="reqtype"\r\n\r\n';
	formData += 'fileupload\r\n';
	formData += '--' + boundary + '\r\n';
	if (userHash) {
	  formData += 'Content-Disposition: form-data; name="userhash"\r\n\r\n';
	  formData += userHash + '\r\n';
	  formData += '--' + boundary + '\r\n';
	}
	formData += 'Content-Disposition: form-data; name="fileToUpload"; filename="' + image.name.toASCII() + '"\r\n';
	formData += 'Content-Type: ' + image.type + '\r\n\r\n';
	formData += image.data + '\r\n';
	formData += '--' + boundary + '--\r\n';
	GM_xmlhttpRequest({
	  method: 'POST',
	  url: 'https://catbox.moe/user/api.php',
	  responseType: 'text',
	  headers: {
		'Content-Type': 'multipart/form-data; boundary=' + boundary,
		'Content-Length': formData.length,
	  },
	  data: formData,
	  binary: true,
	  timeout: Math.max(Math.ceil(formData.length / ulTimeFactor), 10000),
	  onload: function(response) {
		if (response.status >= 200 && response.status < 400) resolve(response.responseText);
			else reject(defaultErrorHandler(response));
	  },
	  onerror: error => reject(defaultErrorHandler(error)),
	  ontimeout: timeout => reject(defaultTimeoutHandler(timeout)),
	});
  }))));
}

function rehost2CatBox(url) {
  return verifyImageUrl(url).then(imageUrl => getCatBoxUserHash().catch(reason => undefined).then(function(userHash) {
	var formData = new URLSearchParams({
	  reqtype: 'urlupload',
	  url: imageUrl,
	});
	if (userHash) formData.set('userhash', userHash);
	return globalFetch('https://catbox.moe/user/api.php', {
	  responseType: 'text',
	  timeout: rehostTimeout,
	}, formData).then(response => response.responseText);
  }));
}

function getCatBoxUserHash() {
  return prefs.catbox_userhash ? Promise.resolve(prefs.catbox_userhash) : globalFetch('https://catbox.moe/').then(function(response) {
	var userHash = response.document.querySelector('input[name="userhash"][value]');
	return userHash != null && userHash.value || Promise.reject('CatBox.moe: not logged in or userhash not found');
  });
}

function upload2ImgBox(images, elem) {
  if (!Array.isArray(images)) return Promise.reject('invalid argument');
  if (images.length <= 0) return Promise.reject('nothing to upload');
  return setImgBoxSession().then(session => Promise.all(images.map(image => new Promise(function(resolve, reject) {
	if (!['png', 'jpg', 'jpeg', 'gif'].some(ext => image.type == 'image/'.concat(ext)))
	  throw 'MIME type not supported: '.concat(image.type);
	var now = Date.now();
	const boundary = '----WebKitFormBoundary'.concat(now.toString(16).toUpperCase());
	var formData = '--' + boundary + '\r\n';
	Object.keys(session.params).forEach(function(field, index, arr) {
	  formData += 'Content-Disposition: form-data; name="' + field + '"\r\n\r\n';
	  formData += session.params[field] + '\r\n';
	  formData += '--' + boundary + '\r\n';
	});
	formData += 'Content-Disposition: form-data; name="files[]"; filename="' + image.name.toASCII() + '"\r\n';
	formData += 'Content-Type: ' + image.type + '\r\n\r\n';
	formData += image.data + '\r\n';
	formData += '--' + boundary + '--\r\n';
	GM_xmlhttpRequest({
	  method: 'POST',
	  url: 'https://imgbox.com/upload/process',
	  headers: {
		'Accept': 'application/json',
		'Content-Type': 'multipart/form-data; boundary=' + boundary,
		'Content-Length': formData.length,
		'X-CSRF-Token': session.csrf_token,
	  },
	  data: formData,
	  responseType: 'json',
	  binary: true,
	  timeout: Math.max(Math.ceil(formData.length / ulTimeFactor), 10000),
	  onload: function(response) {
		if (response.status < 200 || response.status >= 400) return reject(defaultErrorHandler(response));
		resolve(response.response.files[0].original_url);
	  },
	  onerror: error => reject(defaultErrorHandler(error)),
	  ontimeout: timeout => reject(defaultTimeoutHandler(timeout)),
	});
  }))));
}

function setImgBoxSession() {
  return globalFetch('https://imgbox.com/').then(function(response) {
	var csrfToken = response.document.querySelector('meta[name="csrf-token"]');
	if (csrfToken == null) return Promise.reject('ImgBox.com session token not found');
	if (prefs.diag_mode) console.debug('ImgBox.com session token:', csrfToken.content);
	if (response.document.querySelector('div.btn-group > ul.dropdown-menu') != null) return csrfToken.content;
	if (!prefs.imgbox_uid || !prefs.imgbox_password) return csrfToken.content;
	var formData = new URLSearchParams({
	  "utf8": "✓",
	  "authenticity_token": csrfToken.content,
	  "user[login]": prefs.imgbox_uid,
	  "user[password]": prefs.imgbox_password,
	});
	GM_xmlhttpRequest({ method: 'POST', url: 'https://imgbox.com/login', headers: {
	  'Referer': 'https://imgbox.com/login',
	  'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
	  'Content-Length': formData.toString().length,
	}, data: formData.toString() });
	return new Promise(function(resolve, reject) {
	  setTimeout(() => { globalFetch('http://imgbox.com/').then(function(response) {
		if (response.document.querySelector('div.btn-group > ul.dropdown-menu') == null)
		  console.warn('ImgBox.com login failed, continuing as anonymous', response);
		if ((csrfToken = response.document.querySelector('meta[name="csrf-token"]')) != null) {
		  if (prefs.diag_mode) console.debug('ImgBox.com session token after login:', csrfToken.content);
		  resolve(csrfToken.content);
		} else reject('ImgBox.com session token not found');
	  }) }, 1000);
	});
  }).then(csrfToken => globalFetch('https://imgbox.com/ajax/token/generate', {
	method: 'POST',
	responseType: 'json',
	headers: { 'X-CSRF-Token': csrfToken },
  }).then(response => ({
	csrf_token: csrfToken,
	params: {
	  token_id: response.response.token_id,
	  token_secret: response.response.token_secret,
	  content_type: 1,
	  thumbnail_size: '100c',
	  gallery_id: null,
	  gallery_secret: null,
	  comments_enabled: 0,
	},
  })));
}

function rehost2Imgur(url) {
  return verifyImageUrl(url).then(function(imageUrl) {
	const requestUrl = 'https://imgur.com/upload';
	var formData = new URLSearchParams({ url: imageUrl });
	return globalFetch(requestUrl, {
	  responseType: 'json',
	  headers: { Referer: requestUrl },
	  timeout: rehostTimeout,
	}, formData).then(function(result) {
	  if (!result.response.success) return Promise.reject(result.response.status);
	  return 'https://i.imgur.com/'.concat(result.response.data.hash, result.response.data.ext);
	});
  });
}

function dcFmtToGazelle(format) {
  if (/^(?:CD|CDi|CDr|HDCD)\b/.test(format)) return 'CD';
  if (/\b(?:File|AAC|AIFC|AIFF|ALAC|AMR|APE|DFF|DSD|FLAC|MP2|MP3|ogg-vorbis|Opus|SHN|WAV|WavPack|WMA|WMV)\b/.test(format)) return 'WEB';
  if (/^(?:Vinyl|LP|\d+(?:\.\d+)?\s*")$/.test(format)) return 'Vinyl';
  if (/\b(?:SACD|Hybrid)\b/.test(format)) return 'SACD';
  if (/^(?:Blu[ \-]?ray)\b/i.test(format)) return isOPS ? 'BD' : 'Blu-Ray';
  if (/^(?:DVD|HD\s+DVD)/.test(format)) return 'DVD';
  if (/^(?:Cassette|Microcassette)$/i.test(format)) return 'Cassette';
  if (/^(?:DAT)$/.test(format)) return 'DAT';
  if (/^(?:Soundboard)$/i.test(format)) return 'Soundboard';
  //if (/^(?:Memory\s+Stick)$/i.test(format)) return ??
  return null;
}

function queryAjaxAPI(action, params) {
  if (!action) return Promise.reject('Action missing');
  var retryCount = 0;
  return new Promise(function(resolve, reject) {
	params = new URLSearchParams(params || undefined);
	params.set('action', action);
	var url = '/ajax.php?'.concat(params);
	var xhr = new XMLHttpRequest();
	queryInternal();

	function queryInternal() {
	  var now = Date.now();
	  try { var apiTimeFrame = JSON.parse(window.localStorage[siteApiTimeframeStorageKey]) } catch(e) { apiTimeFrame = {} }
	  if (!apiTimeFrame.timeStamp || now > apiTimeFrame.timeStamp + gazelleApiFrame) {
		apiTimeFrame.timeStamp = now;
		apiTimeFrame.requestCounter = 1;
	  } else ++apiTimeFrame.requestCounter;
	  window.localStorage[siteApiTimeframeStorageKey] = JSON.stringify(apiTimeFrame);
	  if (apiTimeFrame.requestCounter <= 5) {
		xhr.open('GET', url, true);
		xhr.setRequestHeader('Accept', 'application/json');
		if (isRED && prefs.redacted_api_key) xhr.setRequestHeader('Authorization', prefs.redacted_api_key);
		xhr.responseType = 'json';
		xhr.onload = function() {
		  if (xhr.status == 404) return reject('not found');
		  if (xhr.status < 200 || xhr.status >= 400) return reject(defaultErrorHandler(xhr));
		  if (xhr.response.status == 'success') return resolve(xhr.response.response);
		  if (xhr.response.error == 'not found') return reject(xhr.response.error);
		  console.warn('queryAjaxAPI.queryInternal(...) response:', xhr, xhr.response);
		  if (xhr.response.error == 'rate limit exceeded') {
			console.warn('queryAjaxAPI.queryInternal(...) ' + xhr.response.error + ':', apiTimeFrame, now, retryCount);
			if (retryCount++ <= 10) return setTimeout(queryInternal, apiTimeFrame.timeStamp + gazelleApiFrame - now);
		  }
		  reject('API '.concat(xhr.response.status, ': ', xhr.response.error));
		};
		xhr.onerror = function() { reject(defaultErrorHandler(xhr)) };
		xhr.ontimeout = function() { reject(defaultTimeoutHandler(xhr)) };
		xhr.timeout = 10000;
		xhr.send();
	  } else {
		setTimeout(queryInternal, apiTimeFrame.timeStamp + gazelleApiFrame - now);
		if (prefs.diag_mode) console.debug('AJAX API request quota exceeded: /ajax.php?action=' +
			action + ' (' + apiTimeFrame.requestCounter + ')');
		if (prefs.messages_verbosity >= 1) {
		  addMessage('AJAX API request exceeding time frame: action=' + action + ' (' + apiTimeFrame.requestCounter + ')', 'notice');
		} else addMessage('please wait for next AJAX timeframe', 'notice');
	  }
	}
  });
}

function validataTorrentFile(torrent) {
  tfMessages.forEach(node => { node.remove() });
  tfMessages = [];
  var fr = new FileReader();
  fr.onload = function(evt) {
	torrent = bdecode(new Uint8Array(fr.result));
	torrent.info.files.forEach(function(file) {
	  var folderName = decodeURIComponent(escape(torrent.info.name));
	  var fileName = decodeURIComponent(escape(file.path[0]));
	  var totalLen = folderName.trueLength() + 1 + fileName.trueLength();
	  if (totalLen > 180) tfMessages.push(addMessage(new HTML('file "' + safeText(fileName).bold() +
			'" exceeding allowed length (' + totalLen + ' > 180)'), 'warning'));
	  if (/\.(?:torrent|\!ut|\!qb|url|lnk)$/i.test(fileName)) {
		tfMessages.push(addMessage(new HTML('forbidden file "' + safeText(fileName).bold() + '"'), 'warning'));
	  }
	});
	ref = document.querySelector('td.ua-messages-bg');
	if (ref != null && ref.childElementCount <= 0) ref.parentNode.remove();
  };
  fr.onerror = fr.ontimeout = error => { console.error('FileReader error (' + torrent.name + ')') };
  fr.readAsArrayBuffer(torrent);

  function bdecode(str) {
	var pos = 0, infoBegin = 0, infoEnd = 0;
	return bdecodeInternal(str);

	function bdecodeInternal(str) {
	  if (pos > str.length) return null;
	  switch (str[pos]) {
		case 100: // char code for 'd'
		  ++pos;
		  var retval = [];
		  while (str[pos] != 101){ // char code for 'e'
			var key = bdecodeInternal(str);
			var val = bdecodeInternal(str);
			if (key === null || val === null) break;
			retval[key] = val;
		  }
		  if(infoEnd == -1) infoEnd = pos + 1;
		  retval.isDct = true;
		  ++pos;
		  return retval;
		case 108: // char code for 'l'
		  ++pos;
		  retval = [];
		  while (str[pos] != 101){ // char code for 'e'
			let val = bdecodeInternal(str);
			if (val === null) break;
			retval.push(val);
		  }
		  ++pos;
		  return retval;
		case 105: // char code for 'i'
		  ++pos;
		  var digits = Array.prototype.indexOf.call(str, 101, pos) - pos; // 101 = char code for 'e'
		  val = '';
		  for (var i = pos; i < digits + pos; ++i) val += String.fromCharCode(str[i]);
		  val = Math.round(parseFloat(val));
		  pos += digits + 1;
		  return val;
		default:
		  digits = Array.prototype.indexOf.call(str, 58, pos) - pos; // 58 = char code for ':'
		  if (digits < 0 || digits > 20) return null;
		  var len = '';
		  for (i = pos; i < digits + pos; ++i) len += String.fromCharCode(str[i]);
		  len = parseInt(len);
		  pos += digits + 1;
		  var fstring = '';
		  for (i = pos; i < len + pos; ++i) fstring += String.fromCharCode(str[i]);
		  pos += len;
		  if(fstring == 'info') {
			infoBegin = pos;
			infoEnd = -1;
		  }
		  return fstring;
	  }
	}
  }
}

function defaultErrorHandler(response) {
  var e = 'XHR: error status=' + response.status;
  if (response.statusText) e += ' (' + response.statusText + ')';
  if (response.error) e += ' (' + response.error + ')';
  console.error('XHR error:', response);
  if (prefs.messages_verbosity >= 2) addMessage(e, 'notice');
  return e;
}
function defaultTimeoutHandler(response) {
  const e = 'XHR: timeout';
  console.error('XHR timeout:', response);
  if (prefs.messages_verbosity >= 2) addMessage(e, 'notice');
  return e;
}