From ea2f8ec268603a0c97a7a17afb9e2844c91bc142 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Hugo=20H=C3=B6rnquist?= <hugo@lysator.liu.se>
Date: Mon, 27 Feb 2023 10:33:56 +0100
Subject: [PATCH] Got show-alternate working.

---
 show-alternate/index.js | 54 ++++++++++++++++++++++++++++-------------
 1 file changed, 37 insertions(+), 17 deletions(-)

diff --git a/show-alternate/index.js b/show-alternate/index.js
index 1b1d35a..722d7cc 100644
--- a/show-alternate/index.js
+++ b/show-alternate/index.js
@@ -1,10 +1,10 @@
 // ==UserScript==
 // @name        Show alternate links
 // @namespace   http://hugo.hornquist.se
-// @version     0.1.1
+// @version     0.2.0
 // @description Show alternate links
 // @author      hugo@lysator.liu.se
-// @match       *://*
+// @match       *://*/*
 // @updateURL   https://git.lysator.liu.se/hugo/web-monkey-scripts/raw/master/show-alternate/index.js
 // @downloadURL https://git.lysator.liu.se/hugo/web-monkey-scripts/raw/master/show-alternate/index.js
 // @source      https://git.lysator.liu.se/hugo/web-monkey-scripts/-/tree/master/show-alternate
@@ -14,7 +14,7 @@
 (function () {
 	'use strict';
 
-	let id = `id-${Math.random()}`
+	let id = `id-${Math.floor(Math.random() * 1e6)}`
 
 	let outer = document.createElement('div');
 	let inner = document.createElement('div');
@@ -23,7 +23,9 @@
 	outer.appendChild(inner);
 	inner.appendChild(ul);
 
+	let link_count = 0
 	for (let link of document.querySelectorAll('link[rel=alternate]')) {
+		link_count++
 		let li = document.createElement('li');
 		let a = document.createElement('a')
 		a.href = link.href
@@ -32,21 +34,39 @@
 		ul.appendChild(li)
 	}
 
-	GM_addStyle(`
-	#${id} {
-		margin: 0 !important;
-		position: absolute;
-		top: 0;
-		right: 0;
-		background: red;
+	if (link_count > 0) {
+		document.body.appendChild(outer)
 	}
 
-	#${id} > div {
-		display: none;
-	}
+	GM_addStyle(`
+#${id} {
+	margin: 0 !important;
+	position: absolute;
+	top: 0;
+	right: 0;
+	display: block;
+	width: 2em;
+	min-height: 2em;
+	background: red;
+	color: white;
+	font-weight: bold;
+	padding: 1ex;
+}
 
-	#${id}:hover > div {
-		display: block;
-	}
-	`)
+#${id}:hover {
+	width: unset;
+}
+
+#${id} ul {
+	padding: revert;
+}
+
+#${id} > div {
+	display: none;
+}
+
+#${id}:hover > div {
+	display: block;
+}
+`)
 })();
-- 
GitLab