mirror of
https://github.com/zedeus/nitter.git
synced 2026-03-04 13:19:57 -05:00
Fix search repeating when the end has been reached
This commit is contained in:
@@ -1,77 +1,82 @@
|
|||||||
// @license http://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
|
// @license http://www.gnu.org/licenses/agpl-3.0.html AGPL-3.0
|
||||||
// SPDX-License-Identifier: AGPL-3.0-only
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||||||
function insertBeforeLast(node, elem) {
|
function insertBeforeLast(node, elem) {
|
||||||
node.insertBefore(elem, node.childNodes[node.childNodes.length - 2]);
|
node.insertBefore(elem, node.childNodes[node.childNodes.length - 2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLoadMore(doc) {
|
function getLoadMore(doc) {
|
||||||
return doc.querySelector(".show-more:not(.timeline-item)");
|
return doc.querySelector(".show-more:not(.timeline-item)");
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDuplicate(item, itemClass) {
|
function isDuplicate(item, itemClass) {
|
||||||
const tweet = item.querySelector(".tweet-link");
|
const tweet = item.querySelector(".tweet-link");
|
||||||
if (tweet == null) return false;
|
if (tweet == null) return false;
|
||||||
const href = tweet.getAttribute("href");
|
const href = tweet.getAttribute("href");
|
||||||
return document.querySelector(itemClass + " .tweet-link[href='" + href + "']") != null;
|
return (
|
||||||
|
document.querySelector(itemClass + " .tweet-link[href='" + href + "']") !=
|
||||||
|
null
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
window.onload = function () {
|
window.onload = function () {
|
||||||
const url = window.location.pathname;
|
const url = window.location.pathname;
|
||||||
const isTweet = url.indexOf("/status/") !== -1;
|
const isTweet = url.indexOf("/status/") !== -1;
|
||||||
const containerClass = isTweet ? ".replies" : ".timeline";
|
const containerClass = isTweet ? ".replies" : ".timeline";
|
||||||
const itemClass = containerClass + " > div:not(.top-ref)";
|
const itemClass = containerClass + " > div:not(.top-ref)";
|
||||||
|
|
||||||
var html = document.querySelector("html");
|
var html = document.querySelector("html");
|
||||||
var container = document.querySelector(containerClass);
|
var container = document.querySelector(containerClass);
|
||||||
var loading = false;
|
var loading = false;
|
||||||
|
|
||||||
function handleScroll(failed) {
|
function handleScroll(failed) {
|
||||||
if (loading) return;
|
if (loading) return;
|
||||||
|
|
||||||
if (html.scrollTop + html.clientHeight >= html.scrollHeight - 3000) {
|
if (html.scrollTop + html.clientHeight >= html.scrollHeight - 3000) {
|
||||||
loading = true;
|
loading = true;
|
||||||
var loadMore = getLoadMore(document);
|
var loadMore = getLoadMore(document);
|
||||||
if (loadMore == null) return;
|
if (loadMore == null) return;
|
||||||
|
|
||||||
loadMore.children[0].text = "Loading...";
|
loadMore.children[0].text = "Loading...";
|
||||||
|
|
||||||
var url = new URL(loadMore.children[0].href);
|
var url = new URL(loadMore.children[0].href);
|
||||||
url.searchParams.append("scroll", "true");
|
url.searchParams.append("scroll", "true");
|
||||||
|
|
||||||
fetch(url.toString()).then(function (response) {
|
fetch(url.toString())
|
||||||
if (response.status === 404) throw "error";
|
.then(function (response) {
|
||||||
|
if (response.status > 299) throw "error";
|
||||||
|
return response.text();
|
||||||
|
})
|
||||||
|
.then(function (html) {
|
||||||
|
var parser = new DOMParser();
|
||||||
|
var doc = parser.parseFromString(html, "text/html");
|
||||||
|
loadMore.remove();
|
||||||
|
|
||||||
return response.text();
|
for (var item of doc.querySelectorAll(itemClass)) {
|
||||||
}).then(function (html) {
|
if (item.className == "timeline-item show-more") continue;
|
||||||
var parser = new DOMParser();
|
if (isDuplicate(item, itemClass)) continue;
|
||||||
var doc = parser.parseFromString(html, "text/html");
|
if (isTweet) container.appendChild(item);
|
||||||
loadMore.remove();
|
else insertBeforeLast(container, item);
|
||||||
|
}
|
||||||
|
|
||||||
for (var item of doc.querySelectorAll(itemClass)) {
|
loading = false;
|
||||||
if (item.className == "timeline-item show-more") continue;
|
const newLoadMore = getLoadMore(doc);
|
||||||
if (isDuplicate(item, itemClass)) continue;
|
if (newLoadMore == null) return;
|
||||||
if (isTweet) container.appendChild(item);
|
if (isTweet) container.appendChild(newLoadMore);
|
||||||
else insertBeforeLast(container, item);
|
else insertBeforeLast(container, newLoadMore);
|
||||||
}
|
})
|
||||||
|
.catch(function (err) {
|
||||||
|
console.warn("Something went wrong.", err);
|
||||||
|
if (failed > 3) {
|
||||||
|
loadMore.children[0].text = "Error";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
loading = false;
|
loading = false;
|
||||||
const newLoadMore = getLoadMore(doc);
|
handleScroll((failed || 0) + 1);
|
||||||
if (newLoadMore == null) return;
|
});
|
||||||
if (isTweet) container.appendChild(newLoadMore);
|
|
||||||
else insertBeforeLast(container, newLoadMore);
|
|
||||||
}).catch(function (err) {
|
|
||||||
console.warn("Something went wrong.", err);
|
|
||||||
if (failed > 3) {
|
|
||||||
loadMore.children[0].text = "Error";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
loading = false;
|
|
||||||
handleScroll((failed || 0) + 1);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
window.addEventListener("scroll", () => handleScroll());
|
window.addEventListener("scroll", () => handleScroll());
|
||||||
};
|
};
|
||||||
// @license-end
|
// @license-end
|
||||||
|
|||||||
@@ -168,6 +168,11 @@ proc getGraphTweetSearch*(query: Query; after=""): Future[Timeline] {.async.} =
|
|||||||
result = parseGraphSearch[Tweets](js, after)
|
result = parseGraphSearch[Tweets](js, after)
|
||||||
result.query = query
|
result.query = query
|
||||||
|
|
||||||
|
# when no more items are available the API just returns the last page in
|
||||||
|
# full. this detects that and clears the page instead.
|
||||||
|
if after.len > 0 and after[0..<64] == result.bottom[0..<64]:
|
||||||
|
result.content.setLen(0)
|
||||||
|
|
||||||
proc getGraphUserSearch*(query: Query; after=""): Future[Result[User]] {.async.} =
|
proc getGraphUserSearch*(query: Query; after=""): Future[Result[User]] {.async.} =
|
||||||
if query.text.len == 0:
|
if query.text.len == 0:
|
||||||
return Result[User](query: query, beginning: true)
|
return Result[User](query: query, beginning: true)
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ proc createStatusRouter*(cfg: Config) =
|
|||||||
if @"scroll".len > 0:
|
if @"scroll".len > 0:
|
||||||
let replies = await getReplies(id, getCursor())
|
let replies = await getReplies(id, getCursor())
|
||||||
if replies.content.len == 0:
|
if replies.content.len == 0:
|
||||||
resp Http404, ""
|
resp Http204
|
||||||
resp $renderReplies(replies, prefs, getPath())
|
resp $renderReplies(replies, prefs, getPath())
|
||||||
|
|
||||||
let conv = await getTweet(id, getCursor())
|
let conv = await getTweet(id, getCursor())
|
||||||
|
|||||||
@@ -129,7 +129,8 @@ proc createTimelineRouter*(cfg: Config) =
|
|||||||
if @"scroll".len > 0:
|
if @"scroll".len > 0:
|
||||||
if query.fromUser.len != 1:
|
if query.fromUser.len != 1:
|
||||||
var timeline = await getGraphTweetSearch(query, after)
|
var timeline = await getGraphTweetSearch(query, after)
|
||||||
if timeline.content.len == 0: resp Http404
|
if timeline.content.len == 0:
|
||||||
|
resp Http204
|
||||||
timeline.beginning = true
|
timeline.beginning = true
|
||||||
resp $renderTweetSearch(timeline, prefs, getPath())
|
resp $renderTweetSearch(timeline, prefs, getPath())
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user