Improve video embed overlay and proxy behavior

This commit is contained in:
Zed 2026-08-12 01:36:40 +07:00
commit 5fa4eefb9d
5 changed files with 82 additions and 32 deletions

View file

@ -8,10 +8,7 @@
if (window.__nitterWidgets) return;
window.__nitterWidgets = true;
var scripts = document.querySelectorAll('script[src*="widgets.js"]');
var NITTER = scripts.length
? new URL(scripts[scripts.length - 1].src).origin
: location.origin;
var NITTER = new URL(document.currentScript.src).origin;
var TWEET_RE = /(?:twitter\.com|x\.com)\/([^\/]+)\/status\/(\d+)/i;
var SELECTOR = "blockquote.twitter-tweet, blockquote.twitter-video";
@ -21,11 +18,15 @@
var isReady = false;
function safeCall(fn, arg) {
try { fn(arg); } catch (e) {}
try {
fn(arg);
} catch (e) { }
}
function fireEvent(name, data) {
(eventCallbacks[name] || []).forEach(function (cb) { safeCall(cb, data); });
(eventCallbacks[name] || []).forEach(function (cb) {
safeCall(cb, data);
});
}
function parseTweetUrl(url) {
@ -44,8 +45,12 @@
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;
var theme =
opts.theme === "dark"
? "nitter"
: opts.theme === "light"
? "twitter"
: opts.theme;
url += "?theme=" + encodeURIComponent(theme);
}
}
@ -60,11 +65,18 @@
if (opts.videoOnly) iframe.setAttribute("allowfullscreen", "true");
var width = opts.width || 550;
var margin = opts.align === "center" ? "10px auto" :
opts.align === "right" ? "10px 0 10px auto" : "10px 0";
var margin =
opts.align === "center"
? "10px auto"
: opts.align === "right"
? "10px 0 10px auto"
: "10px 0";
iframe.style.cssText =
"width:100%;max-width:" + width + "px;height:250px;" +
"border:none;display:block;margin:" + margin;
"width:100%;max-width:" +
width +
"px;height:300px;" +
"border:none;display:block;margin:" +
margin;
iframe.addEventListener("load", function () {
fireEvent("rendered", { target: iframe });
@ -89,7 +101,7 @@
width: d.mediaMaxWidth || d.width,
align: d.align,
theme: d.theme,
videoOnly: d.mediaMaxWidth !== undefined
videoOnly: d.mediaMaxWidth !== undefined,
});
bq.style.display = "none";
@ -98,7 +110,9 @@
}
function processEmbeds(root) {
var bqs = (root || document).querySelectorAll(SELECTOR + ":not([data-nitter-processed])");
var bqs = (root || document).querySelectorAll(
SELECTOR + ":not([data-nitter-processed])",
);
for (var i = 0; i < bqs.length; i++) processBlockquote(bqs[i]);
}
@ -147,7 +161,7 @@
createTweet: embedTweet,
createTweetEmbed: embedTweet,
createVideo: embedTweet,
loaded: true
loaded: true,
},
events: {
bind: function (name, cb) {
@ -158,33 +172,44 @@
unbind: function (name, cb) {
if (!eventCallbacks[name]) return;
eventCallbacks[name] = cb
? eventCallbacks[name].filter(function (f) { return f !== cb; })
? eventCallbacks[name].filter(function (f) {
return f !== cb;
})
: [];
}
},
},
ready: function (cb) {
if (typeof cb !== "function") return;
if (isReady) cb(window.twttr);
else readyCallbacks.push(cb);
},
_e: []
_e: [],
};
// Process callbacks queued before load (twttr._e pattern)
if (prevTwttr && prevTwttr._e) {
prevTwttr._e.forEach(function (cb) { safeCall(cb); });
prevTwttr._e.forEach(function (cb) {
safeCall(cb);
});
}
// Remove any Twitter scripts that snuck through
document.querySelectorAll('script[src*="platform.twitter.com"], script[src*="platform.x.com"]')
.forEach(function (s) { s.remove(); });
document
.querySelectorAll(
'script[src*="platform.twitter.com"], script[src*="platform.x.com"]',
)
.forEach(function (s) {
s.remove();
});
function init() {
window.addEventListener("message", handleResize);
processEmbeds();
observeDOM();
isReady = true;
readyCallbacks.forEach(function (cb) { safeCall(cb, window.twttr); });
readyCallbacks.forEach(function (cb) {
safeCall(cb, window.twttr);
});
readyCallbacks = [];
}