-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshow-alt-on-hover.js
77 lines (68 loc) · 3.26 KB
/
show-alt-on-hover.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// ==UserScript==
// @name Pixiv Show Alt on Hover (WIP)
// @namespace https://github.com/MonoScyron/pixiv-scripts
// @version 0.1.4
// @description Adds the alt of works as a tooltip to their previews.
// @author MonoScyron
// @updateURL https://raw.githubusercontent.com/MonoScyron/pixiv-scripts/main/show-alt-on-hover.js
// @downloadURL https://raw.githubusercontent.com/MonoScyron/pixiv-scripts/main/show-alt-on-hover.js
// @match https://www.pixiv.net/*/artworks/*
// @match https://www.pixiv.net/*/tags/*
// @match https://www.pixiv.net/*/users/*
// @match https://www.pixiv.net/*/
// @icon https://www.google.com/s2/favicons?sz=64&domain=pixiv.net
// @grant none
// @noframes
// ==/UserScript==
(function() {
'use strict';
// ? Consider injecting alt directly to preview (no need to hover to show)
// ? Consider splitting this script into multiple, one for each matched page (if too difficult to scrape w/ 1 algo)
console.log("Script: Loaded")
let root = document.querySelector("div#root");
if(root != null) {
console.log("Script: Begin observing mutations")
const config = {attributes: false, childList: true, subtree: true};
const observer = new MutationObserver(callback);
observer.observe(root, config);
}
/**
* Callback function to execute when mutations are observerd.
* @param {Array<MutationRecord>} mList
* @param {MutationObserver} observer
*/
function callback(mList, observer) {
for(const m of mList) {
if(m.type === "childList") {
// ! No need to add alt of novels (Thankfully pixiv doesn't mix the two)
let target = m.target;
// On header load, scrape alt of header works and add them as tooltips to the work previews
// TODO: Script tries to grab alts too early and gets null
if(target.nodeName === "ASIDE" && target.classList.contains("sc-1nr368f-7") && target.classList.contains("hdvpLU")) {
console.log(`Script: Adding alts to header...`);
target.querySelector("nav.sc-x0j4pn-0.gbvtwZ").childNodes.forEach((n) => {
addTitle(n);
});
}
// On footer load, scrape alt of footer works and add them as tooltips to the work previews
if(target.nodeName === "DIV" && target.classList.contains("sc-rp5asc-9") && target.classList.contains("cYUezH")) {
console.log("Script: Adding alts to a footer work...");
}
// TODO: Scrape alt of /tags/* (works search) page and add them as tooltips to the work previews
// TODO: Scrape alt of /users/* (users) page and add them as tooltips to the work previews
// TODO: Scrape alt of /*/ (home) page and add them as tooltips to the work previews
}
}
/**
* @param {HTMLElement} n
*/
function addTitle(n) {
n = n.querySelector('img');
if(n != null) {
let alt = n.getAttribute("alt");
console.log(`Script: ${alt}`)
n.setAttribute("title", alt);
}
}
}
})();