mirror of
https://github.com/zedeus/nitter
synced 2026-09-05 22:59:31 +00:00
Add video-only embed support and clean up widgets.js
This commit is contained in:
parent
5733297f92
commit
fe8f482d5c
4 changed files with 112 additions and 109 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
function sendHeight() {
|
function sendHeight() {
|
||||||
var currentHeight = embedElement.offsetHeight;
|
var currentHeight = embedElement.offsetHeight;
|
||||||
if (currentHeight !== lastHeight) {
|
if (currentHeight !== lastHeight && currentHeight > 0) {
|
||||||
lastHeight = currentHeight;
|
lastHeight = currentHeight;
|
||||||
window.parent.postMessage(['resizeIframe', { h: currentHeight, url: location.href }], '*');
|
window.parent.postMessage(['resizeIframe', { h: currentHeight, url: location.href }], '*');
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,58 +1,74 @@
|
||||||
/**
|
/**
|
||||||
* Drop-in replacement for Twitter's widgets.js
|
* Drop-in replacement for Twitter's widgets.js
|
||||||
* Converts twitter-tweet/twitter-video blockquotes to Nitter embeds
|
* Redirects twitter-tweet blockquotes to Nitter embeds
|
||||||
*
|
|
||||||
* Usage: LibRedirect can redirect platform.twitter.com/widgets.js to nitter.net/widgets.js
|
|
||||||
*/
|
*/
|
||||||
(function () {
|
(function () {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
// Idempotent - only run once
|
|
||||||
if (window.__nitterWidgets) return;
|
if (window.__nitterWidgets) return;
|
||||||
window.__nitterWidgets = true;
|
window.__nitterWidgets = true;
|
||||||
|
|
||||||
// Determine Nitter instance URL from script src, or fall back to current origin
|
|
||||||
var scripts = document.querySelectorAll('script[src*="widgets.js"]');
|
var scripts = document.querySelectorAll('script[src*="widgets.js"]');
|
||||||
var NITTER = scripts.length
|
var NITTER = scripts.length
|
||||||
? new URL(scripts[scripts.length - 1].src).origin
|
? new URL(scripts[scripts.length - 1].src).origin
|
||||||
: location.origin;
|
: location.origin;
|
||||||
|
|
||||||
var TWEET_PATTERN = /(?:twitter\.com|x\.com)\/([^\/]+)\/status\/(\d+)/i;
|
var TWEET_RE = /(?:twitter\.com|x\.com)\/([^\/]+)\/status\/(\d+)/i;
|
||||||
var SELECTOR = "blockquote.twitter-tweet, blockquote.twitter-video";
|
var SELECTOR = "blockquote.twitter-tweet, blockquote.twitter-video";
|
||||||
|
|
||||||
// Ready callback queue
|
|
||||||
var readyCallbacks = [];
|
var readyCallbacks = [];
|
||||||
|
var eventCallbacks = {};
|
||||||
var isReady = false;
|
var isReady = false;
|
||||||
|
|
||||||
|
function safeCall(fn, arg) {
|
||||||
|
try { fn(arg); } catch (e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
function fireEvent(name, data) {
|
||||||
|
(eventCallbacks[name] || []).forEach(function (cb) { safeCall(cb, data); });
|
||||||
|
}
|
||||||
|
|
||||||
function parseTweetUrl(url) {
|
function parseTweetUrl(url) {
|
||||||
if (!url) return null;
|
if (!url) return null;
|
||||||
var match = TWEET_PATTERN.exec(url);
|
var m = TWEET_RE.exec(url);
|
||||||
if (match) return { username: match[1], id: match[2] };
|
if (m) return { user: m[1], id: m[2] };
|
||||||
// Fallback: extract any large number (tweet ID)
|
m = url.match(/(\d{15,})/);
|
||||||
var idMatch = url.match(/(\d{15,})/);
|
return m ? { user: null, id: m[1] } : null;
|
||||||
return idMatch ? { username: null, id: idMatch[1] } : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createIframe(tweet, options) {
|
function createIframe(tweet, opts) {
|
||||||
var embedUrl = tweet.username
|
var url;
|
||||||
? NITTER + "/" + tweet.username + "/status/" + tweet.id + "/embed"
|
if (opts.videoOnly) {
|
||||||
: NITTER + "/i/status/" + tweet.id + "/embed";
|
url = NITTER + "/i/videos/tweet/" + tweet.id;
|
||||||
|
} else {
|
||||||
|
var path = tweet.user ? "/" + tweet.user : "/i";
|
||||||
|
url = NITTER + path + "/status/" + tweet.id + "/embed";
|
||||||
|
if (opts.theme) {
|
||||||
|
var theme = opts.theme === "dark" ? "nitter" :
|
||||||
|
opts.theme === "light" ? "twitter" : opts.theme;
|
||||||
|
url += "?theme=" + encodeURIComponent(theme);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var iframe = document.createElement("iframe");
|
var iframe = document.createElement("iframe");
|
||||||
iframe.src = embedUrl;
|
iframe.src = url;
|
||||||
iframe.className = "nitter-embed-frame";
|
iframe.className = "nitter-embed-frame";
|
||||||
|
iframe.loading = "lazy";
|
||||||
iframe.setAttribute("allowtransparency", "true");
|
iframe.setAttribute("allowtransparency", "true");
|
||||||
iframe.setAttribute("frameborder", "0");
|
iframe.setAttribute("frameborder", "0");
|
||||||
iframe.setAttribute("scrolling", "no");
|
iframe.setAttribute("scrolling", "no");
|
||||||
iframe.loading = "lazy";
|
if (opts.videoOnly) iframe.setAttribute("allowfullscreen", "true");
|
||||||
|
|
||||||
// Styling with data attribute support
|
var width = opts.width || 550;
|
||||||
var width = options.width || "550";
|
var margin = opts.align === "center" ? "10px auto" :
|
||||||
var align = options.align || "center";
|
opts.align === "right" ? "10px 0 10px auto" : "10px 0";
|
||||||
var margin = align === "center" ? "10px auto" :
|
iframe.style.cssText =
|
||||||
align === "right" ? "10px 0 10px auto" : "10px auto 10px 0";
|
"width:100%;max-width:" + width + "px;height:250px;" +
|
||||||
|
"border:none;display:block;margin:" + margin;
|
||||||
|
|
||||||
iframe.style.cssText = "width:100%;max-width:" + width + "px;height:250px;border:none;display:block;margin:" + margin;
|
iframe.addEventListener("load", function () {
|
||||||
|
fireEvent("rendered", { target: iframe });
|
||||||
|
});
|
||||||
|
|
||||||
return iframe;
|
return iframe;
|
||||||
}
|
}
|
||||||
|
|
@ -61,59 +77,41 @@
|
||||||
if (bq.dataset.nitterProcessed) return false;
|
if (bq.dataset.nitterProcessed) return false;
|
||||||
bq.dataset.nitterProcessed = "true";
|
bq.dataset.nitterProcessed = "true";
|
||||||
|
|
||||||
// Find tweet URL in links
|
|
||||||
var tweet = null;
|
var tweet = null;
|
||||||
var links = bq.querySelectorAll("a[href]");
|
var links = bq.querySelectorAll("a[href]");
|
||||||
for (var i = 0; i < links.length; i++) {
|
for (var i = 0; i < links.length && !tweet; i++) {
|
||||||
tweet = parseTweetUrl(links[i].href);
|
tweet = parseTweetUrl(links[i].href);
|
||||||
if (tweet) break;
|
|
||||||
}
|
}
|
||||||
|
if (!tweet) return false;
|
||||||
|
|
||||||
if (!tweet) {
|
var d = bq.dataset;
|
||||||
console.warn("[Nitter widgets.js] No tweet URL found in blockquote");
|
var iframe = createIframe(tweet, {
|
||||||
return false;
|
width: d.mediaMaxWidth || d.width,
|
||||||
}
|
align: d.align,
|
||||||
|
theme: d.theme,
|
||||||
|
videoOnly: d.mediaMaxWidth !== undefined
|
||||||
|
});
|
||||||
|
|
||||||
// Read Twitter's data attributes
|
|
||||||
var options = {
|
|
||||||
width: bq.dataset.width,
|
|
||||||
align: bq.dataset.align,
|
|
||||||
theme: bq.dataset.theme
|
|
||||||
};
|
|
||||||
|
|
||||||
var iframe = createIframe(tweet, options);
|
|
||||||
|
|
||||||
// Hide original (keep as fallback), insert iframe after
|
|
||||||
bq.style.display = "none";
|
bq.style.display = "none";
|
||||||
bq.parentNode.insertBefore(iframe, bq.nextSibling);
|
bq.parentNode.insertBefore(iframe, bq.nextSibling);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function processEmbeds(container) {
|
function processEmbeds(root) {
|
||||||
var root = container || document;
|
var bqs = (root || document).querySelectorAll(SELECTOR + ":not([data-nitter-processed])");
|
||||||
var blockquotes = root.querySelectorAll(SELECTOR + ":not([data-nitter-processed])");
|
for (var i = 0; i < bqs.length; i++) processBlockquote(bqs[i]);
|
||||||
var count = 0;
|
|
||||||
|
|
||||||
for (var i = 0; i < blockquotes.length; i++) {
|
|
||||||
if (processBlockquote(blockquotes[i])) count++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (count > 0) {
|
function handleResize(e) {
|
||||||
console.log("[Nitter widgets.js] Processed " + count + " embed(s)");
|
if (!Array.isArray(e.data) || e.data[0] !== "resizeIframe") return;
|
||||||
}
|
var h = e.data[1] && e.data[1].h;
|
||||||
}
|
|
||||||
|
|
||||||
function handleResize(event) {
|
|
||||||
if (!Array.isArray(event.data) || event.data[0] !== "resizeIframe") return;
|
|
||||||
var h = event.data[1] && event.data[1].h;
|
|
||||||
if (!h || h <= 0) return;
|
if (!h || h <= 0) return;
|
||||||
|
|
||||||
// Find iframe by matching contentWindow
|
var frames = document.querySelectorAll("iframe.nitter-embed-frame");
|
||||||
var iframes = document.querySelectorAll("iframe.nitter-embed-frame");
|
for (var i = 0; i < frames.length; i++) {
|
||||||
for (var i = 0; i < iframes.length; i++) {
|
if (frames[i].contentWindow === e.source) {
|
||||||
if (iframes[i].contentWindow === event.source) {
|
frames[i].style.height = h + "px";
|
||||||
iframes[i].style.height = h + "px";
|
return;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -121,43 +119,49 @@
|
||||||
function observeDOM() {
|
function observeDOM() {
|
||||||
if (!window.MutationObserver || !document.body) return;
|
if (!window.MutationObserver || !document.body) return;
|
||||||
|
|
||||||
new MutationObserver(function (mutations) {
|
function matches(el) {
|
||||||
for (var i = 0; i < mutations.length; i++) {
|
return el.matches(SELECTOR) || el.querySelector(SELECTOR);
|
||||||
var nodes = mutations[i].addedNodes;
|
|
||||||
for (var j = 0; j < nodes.length; j++) {
|
|
||||||
var node = nodes[j];
|
|
||||||
if (node.nodeType !== 1) continue;
|
|
||||||
if ((node.matches && node.matches(SELECTOR)) ||
|
|
||||||
(node.querySelector && node.querySelector(SELECTOR))) {
|
|
||||||
processEmbeds();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
new MutationObserver(function (muts) {
|
||||||
|
var found = muts.some(function (mut) {
|
||||||
|
return Array.prototype.some.call(mut.addedNodes, function (n) {
|
||||||
|
return n.nodeType === 1 && matches(n);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
if (found) processEmbeds();
|
||||||
}).observe(document.body, { childList: true, subtree: true });
|
}).observe(document.body, { childList: true, subtree: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
function fireReady() {
|
function embedTweet(id, container, opts) {
|
||||||
isReady = true;
|
if (!container) return Promise.reject("No container");
|
||||||
for (var i = 0; i < readyCallbacks.length; i++) {
|
var iframe = createIframe({ id: id, user: null }, opts || {});
|
||||||
try { readyCallbacks[i](window.twttr); } catch (e) {}
|
container.appendChild(iframe);
|
||||||
}
|
return Promise.resolve(iframe);
|
||||||
readyCallbacks = [];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Expose twttr API for compatibility
|
|
||||||
var prevTwttr = window.twttr;
|
var prevTwttr = window.twttr;
|
||||||
window.twttr = {
|
window.twttr = {
|
||||||
widgets: {
|
widgets: {
|
||||||
load: function (el) { processEmbeds(el); },
|
load: processEmbeds,
|
||||||
createTweet: function (id, container, opts) {
|
createTweet: embedTweet,
|
||||||
if (!container) return Promise.reject("No container");
|
createTweetEmbed: embedTweet,
|
||||||
var iframe = createIframe({ id: id, username: null }, opts || {});
|
createVideo: embedTweet,
|
||||||
container.appendChild(iframe);
|
|
||||||
return Promise.resolve(iframe);
|
|
||||||
},
|
|
||||||
loaded: true
|
loaded: true
|
||||||
},
|
},
|
||||||
|
events: {
|
||||||
|
bind: function (name, cb) {
|
||||||
|
if (typeof cb !== "function") return;
|
||||||
|
if (!eventCallbacks[name]) eventCallbacks[name] = [];
|
||||||
|
eventCallbacks[name].push(cb);
|
||||||
|
},
|
||||||
|
unbind: function (name, cb) {
|
||||||
|
if (!eventCallbacks[name]) return;
|
||||||
|
eventCallbacks[name] = cb
|
||||||
|
? eventCallbacks[name].filter(function (f) { return f !== cb; })
|
||||||
|
: [];
|
||||||
|
}
|
||||||
|
},
|
||||||
ready: function (cb) {
|
ready: function (cb) {
|
||||||
if (typeof cb !== "function") return;
|
if (typeof cb !== "function") return;
|
||||||
if (isReady) cb(window.twttr);
|
if (isReady) cb(window.twttr);
|
||||||
|
|
@ -166,27 +170,22 @@
|
||||||
_e: []
|
_e: []
|
||||||
};
|
};
|
||||||
|
|
||||||
// Process any callbacks queued before we loaded (twttr._e pattern)
|
// Process callbacks queued before load (twttr._e pattern)
|
||||||
if (prevTwttr && prevTwttr._e) {
|
if (prevTwttr && prevTwttr._e) {
|
||||||
for (var i = 0; i < prevTwttr._e.length; i++) {
|
prevTwttr._e.forEach(function (cb) { safeCall(cb); });
|
||||||
try { prevTwttr._e[i](); } catch (e) {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove Twitter scripts that might have snuck in
|
// Remove any Twitter scripts that snuck through
|
||||||
var twitterScripts = document.querySelectorAll(
|
document.querySelectorAll('script[src*="platform.twitter.com"], script[src*="platform.x.com"]')
|
||||||
'script[src*="platform.twitter.com"], script[src*="platform.x.com"]'
|
.forEach(function (s) { s.remove(); });
|
||||||
);
|
|
||||||
for (var i = 0; i < twitterScripts.length; i++) {
|
|
||||||
twitterScripts[i].remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize
|
|
||||||
function init() {
|
function init() {
|
||||||
window.addEventListener("message", handleResize);
|
window.addEventListener("message", handleResize);
|
||||||
processEmbeds();
|
processEmbeds();
|
||||||
observeDOM();
|
observeDOM();
|
||||||
fireReady();
|
isReady = true;
|
||||||
|
readyCallbacks.forEach(function (cb) { safeCall(cb, window.twttr); });
|
||||||
|
readyCallbacks = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (document.readyState === "loading") {
|
if (document.readyState === "loading") {
|
||||||
|
|
|
||||||
|
|
@ -2,16 +2,20 @@
|
||||||
@import "_mixins";
|
@import "_mixins";
|
||||||
|
|
||||||
.embed-video {
|
.embed-video {
|
||||||
|
width: 100%;
|
||||||
|
background-color: black;
|
||||||
|
|
||||||
.gallery-video {
|
.gallery-video {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
height: 100%;
|
|
||||||
position: absolute;
|
|
||||||
background-color: black;
|
|
||||||
top: 0%;
|
|
||||||
left: 0%;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.gallery-video > .attachment {
|
.gallery-video > .attachment {
|
||||||
max-height: unset;
|
max-height: unset;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
video {
|
||||||
|
width: 100%;
|
||||||
|
height: auto;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ proc renderHead*(prefs: Prefs; cfg: Config; req: Request; titleText=""; desc="";
|
||||||
let opensearchUrl = getUrlPrefix(cfg) & "/opensearch"
|
let opensearchUrl = getUrlPrefix(cfg) & "/opensearch"
|
||||||
|
|
||||||
buildHtml(head):
|
buildHtml(head):
|
||||||
link(rel="stylesheet", type="text/css", href="/css/style.css?v=84")
|
link(rel="stylesheet", type="text/css", href="/css/style.css?v=85")
|
||||||
link(rel="stylesheet", type="text/css", href="/css/fontello.css?v=7")
|
link(rel="stylesheet", type="text/css", href="/css/fontello.css?v=7")
|
||||||
|
|
||||||
if theme.len > 0:
|
if theme.len > 0:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue