« get me outta code hell

Search their favorites!.user.js « e621 « userstuff - dotfiles - Miscellaneous configuration files of my personal use
about summary refs log tree commit diff
path: root/userstuff/e621/Search their favorites!.user.js
blob: e34f4ee99628716f0aa29e67781d37a675f53437 (plain)
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// ==UserScript==
// @name        Search their favorites!
// @description This is your new file, start writing code
// @match       https://e621.net/posts/*/favorites
// ==/UserScript==

const settings = {
  normalHoverInfoDelay: 400,
  fastHoveringInfoDelay: 150,
  endFastHoveringDelay: 500,

  focusInfoDelay: 750,

  hideTooltipDelay: 500,
};

const state = {
  hoverTimeout: null,
  focusTimeout: null,

  fastHovering: false,
  endFastHoveringTimeout: false,
  
  showing: false,
  justHidden: false,
  
  row: null,
};

const thumb = document.querySelector('#a-index .thumbnail');

const container = document.createElement('div');

const tagLabel = document.createElement('label');
const tagInput = document.createElement('input');
tagLabel.appendChild(document.createTextNode('Search these tags: '));
tagLabel.appendChild(tagInput);
container.appendChild(tagLabel);

let hoverTimeout = null;
for (const a of document.querySelectorAll('#a-index td a[href^="/favorites"]')) {
  a.addEventListener('mouseover', event => {
    const hoverTimeoutDelay =
          (state.fastHovering
           ? settings.fastHoveringInfoDelay
           : settings.normalHoverInfoDelay);

    state.hoverTimeout = setTimeout(() => {
      state.hoverTimeout = null;
      state.fastHovering = true;
      show(a);
    }, hoverTimeoutDelay);

    if (state.endFastHoveringTimeout) {
      clearTimeout(state.endFastHoveringTimeout);
      state.endFastHoveringTimeout = null;
    }

    if (state.hideTimeout) {
      clearTimeout(state.hideTimeout);
      state.hideTimeout = null;
    }
  });
  
  a.addEventListener('mouseleave', event => {
    if (state.hoverTimeout) {
      clearTimeout(state.hoverTimeout);
      state.hoverTimeout = null;
    }

    if (state.fastHovering && !state.endFastHoveringTimeout) {
      state.endFastHoveringTimeout = setTimeout(() => {
        state.endFastHoveringTimeout = null;
        state.fastHovering = false;
      }, settings.endFastHoveringDelay);
    }
  });
  
  a.closest('tr').addEventListener('mouseleave', event => {
    if (state.showing && !state.hideTimeout) {
      state.hideTimeout = setTimeout(() => {
        state.hideTimeout = null;
        hide();
      }, settings.hideTooltipDelay);
    }
  });
  
  a.addEventListener('focus', event => {
    state.focusTimeout = setTimeout(() => {
      state.focusTimeout = null;
      show(a);
    }, settings.focusInfoDelay);

    if (state.justHidden) {
      clearTimeout(state.focusTimeout);
      state.focusTimeout = null;
      show(a);
    }
  })
}

function show(a) {
  hide();
  
  const tr = document.createElement('tr');
  const td = document.createElement('td');
  const aa = document.createElement('a');
  aa.href = '#';
  aa.appendChild(document.createTextNode('Hiya'));
  td.appendChild(aa);
  tr.appendChild(td);

  state.showing = true;
  state.row = tr;
  
  tr.addEventListener('mouseenter', event => {
    if (state.hideTimeout) {
      clearTimeout(state.hideTimeout);
      state.hideTimeout = null;
    }
  });
  
  tr.addEventListener('focusin', event => {
    if (state.hideTimeout) {
      clearTimeout(state.hideTimeout);
      state.hideTimeout = null;
    }
  });
  
  tr.addEventListener('focusout', event => {
    hide();
  });
  
  a.closest('tr').after(tr);
}

function hide() {
  if (!state.showing) return;
  if (!state.row) return;

  state.row.remove();

  state.showing = false;
  state.row = null;

  state.justHidden = true;
  setTimeout(() => {
    state.justHiden = false;
  });
}

thumb.after(container);