-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGoogle_Search_Stremio_Links.user.js
More file actions
235 lines (214 loc) · 7.92 KB
/
Copy pathGoogle_Search_Stremio_Links.user.js
File metadata and controls
235 lines (214 loc) · 7.92 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// ==UserScript==
// @name Google Search: Stremio Links
// @namespace https://github.com/sinazadeh/userscripts
// @version 2.0.0
// @description Adds "Open in Stremio" buttons to Google search results and knowledge panels for IMDb titles.
// @author TheSina
// @match *://www.google.*/*
// @exclude *://*.google.*/recaptcha/*
// @grant GM_addStyle
// @run-at document-end
// @license MIT
// @downloadURL https://raw.githubusercontent.com/sinazadeh/userscripts/refs/heads/main/Google_Search_Stremio_Links.user.js
// @updateURL https://raw.githubusercontent.com/sinazadeh/userscripts/refs/heads/main/Google_Search_Stremio_Links.meta.js
// ==/UserScript==
(function () {
'use strict';
const STREMIO_ICON_URL =
'https://www.stremio.com/website/stremio-logo-small.png';
const addStremioButtonToGoogle = () => {
let seriesOptions = document.querySelector(
"div[data-attrid='kc:/tv/tv_program:media_actions_wholepage']",
);
let movieOptions = document.querySelector(
"div[data-attrid='kc:/film/film:media_actions_wholepage']",
);
let filmReviewContainer = document.querySelector(
"div[data-attrid='kc:/film/film:reviews']",
);
let seriesReviewContainer = document.querySelector(
"div[data-attrid='kc:/tv/tv_program:reviews']",
);
let watchOption = null;
let reviewContainer = null;
let contentType = 'movie';
let imdbCode = null;
if (seriesOptions) {
watchOption = seriesOptions;
reviewContainer = seriesReviewContainer;
contentType = 'series';
} else if (movieOptions) {
watchOption = movieOptions;
reviewContainer = filmReviewContainer;
}
if (watchOption === null) {
return;
}
if (reviewContainer != null) {
let imdbEle = reviewContainer.querySelector(
"a[href*='https://www.imdb.com/']",
);
if (imdbEle) {
let imdbParts = imdbEle.href.split('/');
imdbCode = imdbParts.pop() || imdbParts.pop();
}
}
if (imdbCode === null) {
let imdbLink = document.querySelector(
"a[href*='https://www.imdb.com/']",
)?.href;
imdbCode = imdbLink?.match(/title\/(tt\d+)/)?.[1];
}
if (imdbCode === null) {
return;
}
let childCount =
watchOption.firstElementChild.firstElementChild.childElementCount;
let watchNowEle =
watchOption.firstElementChild.firstElementChild.firstElementChild;
if (childCount === 2) {
let divEle = document.createElement('div');
watchNowEle =
watchOption.firstElementChild.firstElementChild.insertBefore(
divEle,
watchNowEle,
);
}
// Remove previous button if exists
let prev = watchNowEle.querySelector('.stremio-cta__href');
if (prev) prev.remove();
// Inject custom CSS for styling (no black background) and diamond icon
if (!document.getElementById('stremio-cta-style')) {
const style = document.createElement('style');
style.id = 'stremio-cta-style';
style.textContent = `
.stremio-cta__href {
display: flex;
align-items: center;
gap: 14px;
border-radius: 8px;
padding: 6px 0;
margin: 8px 0;
text-decoration: none !important;
}
.stremio-cta__icon-wrap {
width: 48px;
height: 48px;
display: flex;
align-items: center;
justify-content: center;
background: none;
border-radius: 6px;
box-shadow: none;
}
.stremio-cta__icon { /* keep inner content upright */
transform: none;
display:flex;
align-items:center;
justify-content:center;
}
.stremio-png-icon {
width: 36px;
height: 36px;
object-fit: contain;
transform: none;
display: block;
}
.stremio-play {
width: 22px;
height: 22px;
clip-path: polygon(10% 0%, 100% 50%, 10% 100%);
background: white;
opacity: 0.95;
}
.stremio-cta__texts {
display: flex;
flex-direction: column;
align-items: flex-start;
}
.stremio-cta__title {
font-family: 'Segoe UI', 'Arial', sans-serif;
font-size: 16px;
color: #ffffff;
font-weight: 600;
line-height: 1.1;
margin:0;
}
.stremio-cta__subtitle {
font-family: 'Segoe UI', 'Arial', sans-serif;
font-size: 12px;
color: rgba(255,255,255,0.75);
margin-top:4px;
}
/* When page uses light background, slightly adapt colors */
.stremio-cta__href.light .stremio-cta__icon-wrap {
background: linear-gradient(135deg,#6f4df0 0%,#3aa1ff 100%);
}
.stremio-cta__href.light .stremio-cta__title { color: #181818; }
.stremio-cta__href.light .stremio-cta__subtitle { color: #666; }
`;
document.head.appendChild(style);
}
// Determine if surrounding area is dark to flip text color
const isAreaDark = (() => {
try {
const bg =
window.getComputedStyle(watchOption).backgroundColor || '';
if (!bg) return true; // default to dark for Google knowledge panels
// crude check for rgb darkness
const m = bg.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)/);
if (!m) return true;
const r = Number(m[1]),
g = Number(m[2]),
b = Number(m[3]);
const lum = 0.2126 * r + 0.7152 * g + 0.0722 * b;
return lum < 128;
} catch (e) {
return true;
}
})();
const lightClass = isAreaDark ? '' : 'light';
// Try to use the repo PNG; fall back to CSS-drawn icon if it fails to load
watchNowEle.innerHTML = `
<a class="stremio-cta__href ${lightClass}" href='stremio:///detail/${contentType}/${imdbCode}'>
<div class="stremio-cta__icon-wrap">
<img class="stremio-png-icon" src="${STREMIO_ICON_URL}" alt="Stremio icon" />
<div class="stremio-cta__icon css-fallback"><div class="stremio-play"></div></div>
</div>
<div class="stremio-cta__texts">
<div class="stremio-cta__title">Stremio</div>
<div class="stremio-cta__subtitle">Freedom to stream</div>
</div>
</a>
`;
// If PNG loads, hide the CSS fallback. If it errors, keep fallback visible.
const img = watchNowEle.querySelector('.stremio-png-icon');
const fallback = watchNowEle.querySelector('.css-fallback');
if (img && fallback) {
img.addEventListener('load', () => {
img.style.display = 'block';
fallback.style.display = 'none';
});
img.addEventListener('error', () => {
img.style.display = 'none';
fallback.style.display = 'flex';
});
// initial style
img.style.display = 'none';
fallback.style.display = 'flex';
}
};
// Run on page load and after navigation (for Google SPA)
const runScript = () => {
addStremioButtonToGoogle();
};
window.addEventListener('load', runScript);
// For Google SPA navigation
let lastUrl = location.href;
setInterval(() => {
if (location.href !== lastUrl) {
lastUrl = location.href;
runScript();
}
}, 1000);
})();