Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(split links): add bulk URL copy buttons to a-tisket #686

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Display all tracks and releases on which a recording appears from the release pa

## MB: QoL: Paste multiple external links at once

Paste multiple external links at once into the external link editor. Input is split on whitespace (newlines, tabs, spaces, etc.) and fed into the link editor separately.
Paste multiple external links at once into the external link editor. Input is split on whitespace (newlines, tabs, spaces, etc.) and fed into the link editor separately. Additionally adds handy bulk Copy buttons to a-tisket release and artist URLs.

[![Install](https://img.shields.io/badge/dynamic/json?label=install&query=%24.version&url=https%3A%2F%2Fraw.github.com%2FROpdebee%2Fmb-userscripts%2Fdist%2Fmb_multi_external_links.metadata.json&logo=tampermonkey&style=for-the-badge&color=informational)](https://raw.github.com/ROpdebee/mb-userscripts/dist/mb_multi_external_links.user.js)
[![Source](https://img.shields.io/badge/source-grey?style=for-the-badge&logo=github)](src/mb_multi_external_links)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function insertCheckboxElements(editor: ExternalLinks, checkboxElmt: HTMLInputEl
checkboxElmt.style.marginLeft = `${marginLeft}px`;
}

async function run(windowInstance: Window): Promise<void> {
async function initSplitter(windowInstance: Window): Promise<void> {
// This element should be present even before React is initialized. Checking
// for its existence enables us to skip attempting to find the link input on
// edit pages that don't have external links, without having to exclude
Expand All @@ -136,7 +136,7 @@ function onIframeAdded(iframe: HTMLIFrameElement): void {
LOGGER.debug(`Initialising on iframe ${iframe.src}`);

onAddEntityDialogLoaded(iframe, () => {
logFailure(run(iframe.contentWindow!));
logFailure(initSplitter(iframe.contentWindow!));
});
}

Expand All @@ -162,6 +162,46 @@ function listenForIframes(): void {
});
}

logFailure(run(window));
function attachLinkSplitter(): void {
logFailure(initSplitter(window));
listenForIframes();
}

const insertAtisketCopyButtons = ((): (() => void) => {
function createButton(textGetter: () => string): HTMLButtonElement {
const btn = <button
type="button"
onClick={(): void => {
logFailure(navigator.clipboard.writeText(textGetter()));
btn.textContent = '(copied)';
setTimeout(() => btn.textContent = 'Copy', 1500);
}}
style={{
marginLeft: '1ex',
}}
>Copy</button> as HTMLButtonElement;
return btn;
}

function getArtistLinks(): string {
const urls = qsa<HTMLAnchorElement>('.prop.artists a:not(.mb)').map((a) => a.href);
return urls.join('\n');
}

listenForIframes();
function getReleaseLinks(): string {
const urls = qsa<HTMLAnchorElement>('section#mba-links a').map((a) => a.href);
return urls.join('\n');
}

return (): void => {
qsMaybe('.prop.artists div')?.prepend(createButton(getArtistLinks));
qsMaybe('section#mba-links > h2')?.append(createButton(getReleaseLinks));
};
})();


if (document.location.hostname === 'musicbrainz.org' || document.location.hostname.endsWith('.musicbrainz.org')) {
attachLinkSplitter();
} else if (document.location.hostname === 'atisket.pulsewidth.org.uk' || document.location.hostname === 'etc.marlonob.info') {
insertAtisketCopyButtons();
}
24 changes: 15 additions & 9 deletions src/mb_multi_external_links/meta.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import type { UserscriptMetadata } from '@lib/util/metadata';
import { transformMBMatchURL } from '@lib/util/metadata';

const mbMatchedUrls = [
'*/edit',
'*/edit?*',
'release/*/edit-relationships*',
'*/add',
'*/add?*',
'*/create',
'*/create?*',
].map((path) => transformMBMatchURL(path));

const metadata: UserscriptMetadata = {
name: 'MB: QoL: Paste multiple external links at once',
description: 'Enables pasting multiple links, separated by whitespace, into the external link editor.',
'run-at': 'document-end',
match: [
'*/edit',
'*/edit?*',
'release/*/edit-relationships*',
'*/add',
'*/add?*',
'*/create',
'*/create?*',
].map((path) => transformMBMatchURL(path)),
blurb: 'Paste multiple external links at once into the external link editor. Input is split on whitespace (newlines, tabs, spaces, etc.) and fed into the link editor separately.',
...mbMatchedUrls,
'*://atisket.pulsewidth.org.uk/*',
'*://etc.marlonob.info/atisket/*',
],
blurb: 'Paste multiple external links at once into the external link editor. Input is split on whitespace (newlines, tabs, spaces, etc.) and fed into the link editor separately. Additionally adds handy bulk Copy buttons to a-tisket release and artist URLs.',
};

export default metadata;