// ==UserScript==
// @name [GMT] Tags Helper
// @version 1.01.3
// @author Anakunda
// @copyright 2021, Anakunda (https://greasyfork.org/users/321857-anakunda)
// @license GPL-3.0-or-later
// @namespace https://greasyfork.org/users/321857-anakunda
// @run-at document-end
// @match https://*/artist.php?id=*
// @match https://*/artist.php?*&id=*
// @match https://*/requests.php
// @match https://*/requests.php?submit=true&*
// @match https://*/requests.php?type=*
// @match https://*/requests.php?page=*
// @match https://*/requests.php?action=new*
// @match https://*/requests.php?action=view&id=*
// @match https://*/requests.php?action=view&*&id=*
// @match https://*/requests.php?action=edit&id=*
// @match https://*/torrents.php?id=*
// @match https://*/torrents.php
// @match https://*/torrents.php?action=advanced
// @match https://*/torrents.php?action=advanced&*
// @match https://*/torrents.php?*&action=advanced
// @match https://*/torrents.php?*&action=advanced&*
// @match https://*/torrents.php?action=basic
// @match https://*/torrents.php?action=basic&*
// @match https://*/torrents.php?*&action=basic
// @match https://*/torrents.php?*&action=basic&*
// @match https://*/torrents.php?page=*
// @match https://*/torrents.php?action=notify
// @match https://*/torrents.php?action=notify&*
// @match https://*/torrents.php?type=*
// @match https://*/collages.php?id=*
// @match https://*/collages.php?page=*&id=*
// @match https://*/collages.php?action=new
// @match https://*/collages.php?action=edit&collageid=*
// @match https://*/bookmarks.php?type=*
// @match https://*/bookmarks.php?page=*
// @match https://*/upload.php
// @match https://*/upload.php&url=*
// @match https://*/upload.php&tags=*
// @match https://*/bookmarks.php?type=torrents
// @match https://*/bookmarks.php?page=*&type=torrents
// @match https://*/top10.php
// @match https://*/top10.php?*
// @grant GM_getValue
// @grant GM_setClipboard
// @grant GM_registerMenuCommand
// @require https://openuserjs.org/src/libs/Anakunda/QobuzLib.min.js
// @require https://openuserjs.org/src/libs/Anakunda/GazelleTagManager.min.js
// @description Improvements for working with groups of tags + increased efficiency of new requests creation
// ==/UserScript==
(function() {
'use strict';
const hasStyleSheet = styleSheet => document.styleSheetSets && document.styleSheetSets.contains(styleSheet);
const isLightTheme = ['postmod', 'shiro', 'layer_cake', 'proton', 'red_light'].some(hasStyleSheet);
const isDarkTheme = ['kuro', 'minimal', 'red_dark'].some(hasStyleSheet);
const uriTest = /^(https?:\/\/.+)$/i;
const isFirefox = /\b(?:Firefox)\b/.test(navigator.userAgent) || Boolean(window.InstallTrigger);
const fieldNames = ['tags', 'tagname', 'taglist'];
const exclusions = GM_getValue('exclusions', [
'/^(?:\\d{4}s)$/i',
'/^(?:delete\.this\.tag)$/i',
]).map(function(expr) {
const m = /^\/(.+)\/([dgimsuy]*)$/.exec(expr);
if (m != null) return new RegExp(m[1], m[2]);
}).filter(it => it instanceof RegExp);
const getTagsFromIterable = iterable => Array.from(iterable)
.filter(elem => elem.offsetWidth > 0 && elem.offsetHeight > 0 && elem.pathname && elem.search
&& fieldNames.some(URLSearchParams.prototype.has.bind(new URLSearchParams(elem.search))))
.map(elem => elem.textContent.trim())
.filter(tag => /^([a-z\d\.]+)$/.test(tag) && !exclusions.some(rx => rx.test(tag)));
const contextId = 'cae67c72-9aa7-4b96-855e-73cb23f5c7f8';
let menuHooks = 0, menuInvoker;
function createMenu() {
const menu = document.createElement('menu');
menu.type = 'context';
menu.id = contextId;
menu.className = 'tags-helper';
function addMenuItem(label, callback) {
if (label) {
let menuItem = document.createElement('MENUITEM');
menuItem.label = label;
if (typeof callback == 'function') menuItem.onclick = callback;
menu.append(menuItem);
}
return menu.children.length;
}
addMenuItem('Copy tags to clipboard', function(evt) {
console.assert(menuInvoker instanceof HTMLElement, 'menuInvoker instanceof HTMLElement')
if (!(menuInvoker instanceof HTMLElement)) throw 'Invalid invoker';
const tags = getTagsFromIterable(menuInvoker.getElementsByTagName('A'));
if (tags.length > 0) GM_setClipboard(tags.join(', '), 'text');
});
addMenuItem('Make new request using these tags', function(evt) {
console.assert(menuInvoker instanceof HTMLElement, 'menuInvoker instanceof HTMLElement')
if (!(menuInvoker instanceof HTMLElement)) throw 'Invalid invoker';
const tags = getTagsFromIterable(menuInvoker.getElementsByTagName('A'));
if (tags.length > 0) document.location.assign('/requests.php?' + new URLSearchParams({
action: 'new',
tags: JSON.stringify(tags),
}).toString());
});
addMenuItem('Make new upload using these tags', function(evt) {
console.assert(menuInvoker instanceof HTMLElement, 'menuInvoker instanceof HTMLElement')
if (!(menuInvoker instanceof HTMLElement)) throw 'Invalid invoker';
const tags = getTagsFromIterable(menu.getElementsByTagName('A'));
if (tags.length > 0) document.location.assign('/upload.php?' + new URLSearchParams({
tags: JSON.stringify(tags),
}).toString());
});
document.body.append(menu);
}
function setElemHandlers(elem) {
console.assert(elem instanceof HTMLElement);
elem.addEventListener('click', function(evt) {
if (evt.altKey) evt.preventDefault(); else return;
const tags = getTagsFromIterable(evt.currentTarget.getElementsByTagName('A'));
if (tags.length > 0) if (evt.ctrlKey) document.location.assign('/requests.php?' + new URLSearchParams({
action: 'new',
tags: JSON.stringify(tags)
}).toString()); else if (evt.shiftKey) document.location.assign('/upload.php?' + new URLSearchParams({
tags: JSON.stringify(tags)
}).toString()); else {
GM_setClipboard(tags.join(', '), 'text');
evt.currentTarget.style.backgroundColor = isDarkTheme ? 'darkgreen' : 'lightgreen';
setTimeout(elem => { elem.style.backgroundColor = null }, 1000, evt.currentTarget);
}
return false;
});
elem.ondragover = evt => false;
elem.ondragenter = evt => { evt.currentTarget.style.backgroundColor = 'lawngreen' };
elem[isFirefox ? 'ondragexit' : 'ondragleave'] = evt => { evt.currentTarget.style.backgroundColor = null };
elem.draggable = true;
elem.ondragstart = function(evt) {
//evt.dataTransfer.clearData('text/uri-list');
//evt.dataTransfer.clearData('text/x-moz-url');
evt.dataTransfer.setData('text/plain',
getTagsFromIterable(evt.currentTarget.getElementsByTagName('A')).join(', '));
console.debug(evt.currentTarget, evt.currentTarget.getElementsByTagName('A'),
getTagsFromIterable(evt.currentTarget.getElementsByTagName('A')));
};
elem.ondrop = function(evt) {
evt.preventDefault();
evt.stopPropagation();
let links = evt.dataTransfer.getData('text/uri-list');
if (links) links = links.split(/\r?\n/); else {
links = evt.dataTransfer.getData('text/x-moz-url');
if (links) links = links.split(/\r?\n/).filter((item, ndx) => ndx % 2 == 0);
else if (links = evt.dataTransfer.getData('text/plain'))
links = links.split(/\r?\n/).filter(RegExp.prototype.test.bind(uriTest));
}
if (Array.isArray(links) && links.length > 0) {
const tags = getTagsFromIterable(evt.currentTarget.getElementsByTagName('A'));
if (tags.length > 0) if (evt.shiftKey) document.location.assign('/upload.php?' + new URLSearchParams({
url: links[0],
tags: JSON.stringify(tags),
}).toString()); else document.location.assign('/requests.php?' + new URLSearchParams({
action: 'new',
url: links[0],
tags: JSON.stringify(tags),
}).toString());
}
evt.currentTarget.style.backgroundColor = null;
return false;
};
elem.setAttribute('contextmenu', contextId);
elem.oncontextmenu = evt => { menuInvoker = evt.currentTarget };
elem.style.cursor = 'context-menu';
++menuHooks;
elem.title = `Alt + click => copy tags to clipboard
Ctrl + Alt + click => make new request using these tags
Shift + Alt + click => make new upload using these tags
---
Drag & drop active link here => make new request using these tags
Shift + Drag & drop active link here => make new upload using these tags
Drag this tags area and drop to any text input to get inserted all tags as comma-separated list
--or-- use context menu (older browsers only)`;
}
switch (document.location.pathname) {
case '/torrents.php': {
if (!new URLSearchParams(document.location.search).has('id')) break;
const urlParams = new URLSearchParams(document.location.search);
try {
let tags = urlParams.get('tags');
if (tags && (tags = JSON.parse(tags)).length > 0) {
const input = document.getElementById('tagname');
if (input == null) throw 'Tags input not found';
tags = new TagManager(...tags);
input.value = tags.toString();
input.scrollIntoView({ behavior: 'smooth', block: 'start' });
//if (input.nextElementSibling != null) input.nextElementSibling.click();
}
} catch(e) { }
break;
}
case '/requests.php':
case '/upload.php': {
const urlParams = new URLSearchParams(document.location.search);
try {
let tags = urlParams.get('tags');
if (tags && (tags = JSON.parse(tags)).length > 0) {
const input = document.getElementById('tags');
if (input == null) throw 'Tags input not found';
tags = new TagManager(...tags);
input.value = tags.toString();
}
} catch(e) { }
const url = urlParams.get('url');
if (uriTest.test(url)) {
let ua = document.getElementById('ua-data');
function feedData() {
ua.value = url;
if ((ua = document.getElementById('autofill-form-2')) == null) return; // assertion failed
if (typeof ua.onclick == 'function') ua.onclick(); else ua.click();
}
if (ua != null) feedData(); else {
const container = document.querySelector('form#request_form > table > tbody');
if (container != null) {
let counters = [0, 0], timeStamp = Date.now();
const mo = new MutationObserver(function(mutationsList) {
++counters[0];
for (let mutation of mutationsList) for (let node of mutation.addedNodes) {
++counters[1];
if (node.nodeName != 'TR' || (ua = node.querySelector('textarea#ua-data')) == null) continue;
console.log('Found UA data by trigger:', counters, (Date.now() - timeStamp) / 1000);
clearTimeout(timer);
return feedData();
}
}), timer = setTimeout(mo => { mo.disconnect() }, 10000, mo);
mo.observe(container, { childList: true });
}
}
}
break;
}
}
document.body.querySelectorAll('div.tags').forEach(setElemHandlers);
(function() {
const tagsBox = document.body.querySelector('div.box_tags');
if (tagsBox != null) setElemHandlers(tagsBox); else return;
const head = tagsBox.querySelector('div.head');
if (head == null) return;
const span = document.createElement('SPAN'), a = document.createElement('A');
span.style.float = 'right';
a.className = 'brackets';
a.textContent = 'Copy';
a.href = '#';
a.onclick = function(evt) {
let tags = getTagsFromIterable(tagsBox.querySelectorAll('* > li > a'));
if (tags.length <= 0) return false;
GM_setClipboard(tags.join(', '), 'text');
evt.currentTarget.style.color = isDarkTheme ? 'darkgreen' : 'lightgreen';
setTimeout(elem => {elem.style.color = null }, 1000, evt.currentTarget);
return false;
};
span.append(a);
head.append(span);
})();
if (menuHooks > 0) createMenu();
function groupDropHandler(evt) {
if (evt.dataTransfer.items.length <= 0) return false;
let tags = evt.dataTransfer.getData('text/plain');
if (tags) tags = tags.split(/[\r\n\,\;\|\>]+/).map(expr => expr.trim()).filter(Boolean); else return false;
if (tags.length > 0) tags = new TagManager(...tags); else return false;
if (tags.length <= 0) return false;
const a = evt.currentTarget.querySelector('a[href^="torrents.php?id="]');
if (a != null) document.location.assign(a.href + '&tags=' + encodeURIComponent(JSON.stringify(tags)));
return false;
}
for (let tr of document.body.querySelectorAll([
'div#discog_table > table > tbody > tr.group',
'table.torrent_table > tbody > tr.group',
'table.torrent_table > tbody > tr.torrent',
].join(', '))) {
tr.ondragover = evt => false;
tr.ondrop = groupDropHandler;
}
function inputDataHandler(evt) {
switch (evt.type) {
case 'paste': var tags = evt.clipboardData; break;
case 'drop': tags = evt.dataTransfer; break;
}
if (tags) tags = tags.getData('text/plain'); else return;
if (tags) tags = tags.split(/[\r\n\,\;\|\>]+/).map(expr => expr.trim()).filter(Boolean); else return;
if (tags.length > 0) switch (evt.type) {
case 'paste': tags = new TagManager(...tags); break;
case 'drop': tags = new TagManager(evt.currentTarget.value, ...tags); break;
}
if (tags.length > 0) tags = tags.toString(); else return;
switch (evt.type) {
case 'paste': {
const cursor = evt.currentTarget.selectionStart + tags.length;
evt.currentTarget.value = evt.currentTarget.value.slice(0, evt.currentTarget.selectionStart) +
tags + evt.currentTarget.value.slice(evt.currentTarget.selectionEnd);
evt.currentTarget.selectionEnd = evt.currentTarget.selectionStart = cursor;
break;
}
case 'drop': evt.currentTarget.value = tags; break;
}
return false;
}
for (let input of document.body.querySelectorAll(fieldNames.map(name => `input[name="${name}"]`).join(', ')))
input.onpaste = input.ondrop = inputDataHandler;
})();