Repository navigation
Expand file tree
/
Copy pathmulti-line-search.user.js
More file actions
127 lines (107 loc) · 3.89 KB
/
Copy pathmulti-line-search.user.js
File metadata and controls
127 lines (107 loc) · 3.89 KB
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
// ==UserScript==
// @name Search Each Line in New Tab
// @namespace http://tampermonkey.net/
// @version 1.5
// @description Select multiple lines of text, then click the button to open each line in a new DuckDuckGo search tab. Works only on youtube.com (not embedded players). Button adapts to light/dark system theme, cleans timestamps/list numbering, and disables during fullscreen.
// @author Leonidas
// @match *://www.youtube.com/*
// @grant GM_openInTab
// @grant GM_registerMenuCommand
// @run-at document-end
// ==/UserScript==
(function () {
'use strict';
// Stop if we're inside an embedded player (iframe)
if (window.top !== window.self) return;
// --- Theme‑adaptive styles ---
const style = document.createElement('style');
style.textContent = `
#search-lines-btn {
position: fixed;
bottom: 20px;
right: 20px;
z-index: 999999;
padding: 8px 12px;
border-radius: 6px;
cursor: pointer;
font: 14px sans-serif;
border: 1px solid;
box-shadow: 0 2px 6px rgba(0,0,0,0.2);
/* Light theme (default) */
background: rgba(255, 255, 255, 0.85);
color: #000;
border-color: #ccc;
}
@media (prefers-color-scheme: dark) {
#search-lines-btn {
background: rgba(40, 40, 40, 0.9);
color: #eee;
border-color: #555;
box-shadow: 0 2px 6px rgba(0,0,0,0.5);
}
}
`;
document.head.appendChild(style);
// --- Create the button ---
const btn = document.createElement('button');
btn.id = 'search-lines-btn';
btn.textContent = '🔍';
document.documentElement.appendChild(btn);
// --- Fullscreen handling ---
function isFullscreen() {
return !!(document.fullscreenElement || document.webkitFullscreenElement);
}
function updateButtonVisibility() {
btn.style.display = isFullscreen() ? 'none' : '';
}
// Listen for fullscreen changes (standard and Safari/older WebKit)
document.addEventListener('fullscreenchange', updateButtonVisibility);
document.addEventListener('webkitfullscreenchange', updateButtonVisibility);
// Set initial visibility
updateButtonVisibility();
// --- Text cleaning function (robust) ---
function cleanLine(line) {
let cleaned = line;
// 1. Remove timestamps like 01:12, 1:23:45, etc.
cleaned = cleaned.replace(/\b\d{1,2}:\d{2}(?::\d{2})?\b/g, '');
// 2. Repeatedly strip leading enumeration patterns like "12 - ", "12.", "12)", "12: "
let previous;
do {
previous = cleaned;
cleaned = cleaned.replace(/^[\s-–—.]*\d+\s*[-–—.:)]\s*/, '');
} while (cleaned !== previous);
// 3. Trim any remaining leading/trailing separators and whitespace
cleaned = cleaned.replace(/^[-–—.\s]+/, '');
cleaned = cleaned.replace(/[-–—.\s]+$/, '');
return cleaned.trim();
}
// --- Click logic ---
btn.addEventListener('click', () => {
// Disable entirely when in fullscreen
if (isFullscreen()) return;
const selection = window.getSelection();
const text = selection ? selection.toString().trim() : '';
if (!text) {
alert('Select some text with multiple lines first.');
return;
}
let lines = text.split(/\r?\n/)
.map(l => cleanLine(l))
.filter(l => l.length > 0);
if (lines.length === 0) {
alert('No text remaining after cleaning.');
return;
}
// Open each line as a DuckDuckGo search (adding " steam") in a new tab
lines.forEach((line, i) => {
const url = 'https://www.duckduckgo.com/search?q=' + encodeURIComponent(line + ' steam');
setTimeout(() => {
GM_openInTab(url, { active: i === 0, setParent: true });
}, i * 150);
});
});
// Optional Tampermonkey menu command (also blocked during fullscreen)
GM_registerMenuCommand('Search selected lines in new tabs', () => {
btn.click(); // will be no‑op if in fullscreen
});
})();