DEV: De-jQ click-track (#17539)

This commit is contained in:
Jarek Radosz 2022-07-19 13:45:20 +02:00 committed by GitHub
parent 0948a59c3f
commit 06135f3069
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 28 deletions

View File

@ -9,29 +9,44 @@ import { isTesting } from "discourse-common/config/environment";
import discourseLater from "discourse-common/lib/later";
import { selectedText } from "discourse/lib/utilities";
import { wantsNewWindow } from "discourse/lib/intercept-click";
import deprecated from "discourse-common/lib/deprecated";
export function isValidLink(link) {
// eslint-disable-next-line no-undef
if (link instanceof jQuery) {
link = link[0];
deprecated("isValidLink now expects an Element, not a jQuery object", {
since: "2.9.0.beta7",
});
}
export function isValidLink($link) {
// .hashtag == category/tag link
// .back == quote back ^ button
if ($link.is(".lightbox, .no-track-link, .hashtag, .back")) {
if (
["lightbox", "no-track-link", "hashtag", "back"].some((name) =>
link.classList.contains(name)
)
) {
return false;
}
if ($link.parents("aside.quote, .elided, .expanded-embed").length !== 0) {
const closest = link.closest("aside.quote, .elided, .expanded-embed");
if (closest && closest !== link) {
return false;
}
if ($link.closest(".onebox-result, .onebox-body").length) {
const $a = $link.closest(".onebox").find("header a");
if ($a[0] && $a[0].href === $link[0].href) {
if (link.closest(".onebox-result, .onebox-body")) {
const a = link.closest(".onebox")?.querySelector("header a");
if (a && a.href === link.href) {
return true;
}
}
return (
$link.hasClass("track-link") ||
$link.closest(".hashtag, .badge-category, .onebox-result, .onebox-body")
.length === 0
link.classList.contains("track-link") ||
!link.closest(".hashtag, .badge-category, .onebox-result, .onebox-body")
);
}
@ -85,24 +100,25 @@ export default {
}
}
const $link = $(e.currentTarget);
const tracking = isValidLink($link);
const link = e.currentTarget;
const tracking = isValidLink(link);
// Return early for mentions and group mentions
if ($link.is(".mention, .mention-group")) {
if (
["mention", "mention-group"].some((name) => link.classList.contains(name))
) {
return true;
}
let href = ($link.attr("href") || $link.data("href") || "").trim();
if (!href || href.indexOf("mailto:") === 0) {
let href = (link.getAttribute("href") || link.dataset.href || "").trim();
if (!href || href.startsWith("mailto:")) {
return true;
}
if ($link.hasClass("attachment")) {
if (link.classList.contains("attachment")) {
// Warn the user if they cannot download the file.
if (
siteSettings &&
siteSettings.prevent_anons_from_downloading_files &&
siteSettings?.prevent_anons_from_downloading_files &&
!User.current()
) {
bootbox.alert(I18n.t("post.errors.attachment_download_requires_login"));
@ -116,23 +132,27 @@ export default {
return false;
}
const $article = $link.closest(
const article = link.closest(
"article:not(.onebox-body), .excerpt, #revisions"
);
const postId = $article.data("post-id");
const topicId = $("#topic").data("topic-id") || $article.data("topic-id");
const userId = $link.data("user-id") || $article.data("user-id");
const ownLink = userId && userId === User.currentProp("id");
const postId = article.dataset.postId;
const topicId =
document.querySelector("#topic")?.dataset?.topicId ||
article.dataset.topicId;
const userId = link.dataset.userId || article.dataset.userId;
const ownLink = userId && parseInt(userId, 10) === User.currentProp("id");
// Update badge clicks unless it's our own.
if (tracking && !ownLink) {
const $badge = $("span.badge", $link);
if ($badge.length === 1) {
const html = $badge.html();
const badge = link.querySelector("span.badge");
if (badge) {
const html = badge.innerHTML;
const key = `${new Date().toLocaleDateString()}-${postId}-${href}`;
if (/^\d+$/.test(html) && !sessionStorage.getItem(key)) {
sessionStorage.setItem(key, true);
$badge.html(parseInt(html, 10) + 1);
badge.innerHTML = parseInt(html, 10) + 1;
}
}
}
@ -159,7 +179,7 @@ export default {
if (!wantsNewWindow(e)) {
if (shouldOpenInNewTab(href)) {
openLinkInNewTab($link[0]);
openLinkInNewTab(link);
} else {
trackPromise.finally(() => {
if (DiscourseURL.isInternal(href) && samePrefix(href)) {

View File

@ -138,7 +138,7 @@ export default class PostCooked {
}
// don't display badge counts on category badge & oneboxes (unless when explicitly stated)
if (valid && isValidLink($link)) {
if (valid && isValidLink($link[0])) {
const $onebox = $link.closest(".onebox");
if (
$onebox.length === 0 ||