mirror of
https://github.com/zedeus/nitter
synced 2026-09-05 14:49:32 +00:00
Improve video embed overlay and proxy behavior
This commit is contained in:
parent
b76e3c3913
commit
5fa4eefb9d
5 changed files with 82 additions and 32 deletions
|
|
@ -1,25 +1,41 @@
|
|||
(function() {
|
||||
var embedElement = document.querySelector('.embed-wrapper, .tweet-embed, .embed-video');
|
||||
(function () {
|
||||
var embedElement = document.querySelector(
|
||||
".embed-wrapper, .tweet-embed, .embed-video",
|
||||
);
|
||||
if (!embedElement) return;
|
||||
|
||||
// Video play state for overlay (hidden while playing, visible on hover/pause)
|
||||
var video = embedElement.querySelector("video");
|
||||
if (video) {
|
||||
video.onplay = function () {
|
||||
embedElement.classList.add("video-playing");
|
||||
};
|
||||
video.onpause = video.onended = function () {
|
||||
embedElement.classList.remove("video-playing");
|
||||
};
|
||||
}
|
||||
|
||||
var lastHeight = 0;
|
||||
|
||||
function sendHeight() {
|
||||
var currentHeight = embedElement.offsetHeight;
|
||||
if (currentHeight !== lastHeight && currentHeight > 0) {
|
||||
lastHeight = currentHeight;
|
||||
window.parent.postMessage(['resizeIframe', { h: currentHeight, url: location.href }], '*');
|
||||
window.parent.postMessage(
|
||||
["resizeIframe", { h: currentHeight, url: location.href }],
|
||||
"*",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Respond to height requests from parent via MessageChannel
|
||||
window.addEventListener('message', function(event) {
|
||||
window.addEventListener("message", function (event) {
|
||||
if (event.source === window.parent && event.ports && event.ports[0]) {
|
||||
event.ports[0].postMessage(embedElement.offsetHeight);
|
||||
}
|
||||
});
|
||||
|
||||
window.addEventListener('load', sendHeight);
|
||||
window.addEventListener("load", sendHeight);
|
||||
new ResizeObserver(sendHeight).observe(embedElement);
|
||||
|
||||
// Expose for embedTweet.js
|
||||
|
|
|
|||
|
|
@ -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 = [];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue