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 discourseLater from "discourse-common/lib/later";
import { selectedText } from "discourse/lib/utilities"; import { selectedText } from "discourse/lib/utilities";
import { wantsNewWindow } from "discourse/lib/intercept-click"; 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 // .hashtag == category/tag link
// .back == quote back ^ button // .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; 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; return false;
} }
if ($link.closest(".onebox-result, .onebox-body").length) { if (link.closest(".onebox-result, .onebox-body")) {
const $a = $link.closest(".onebox").find("header a"); const a = link.closest(".onebox")?.querySelector("header a");
if ($a[0] && $a[0].href === $link[0].href) {
if (a && a.href === link.href) {
return true; return true;
} }
} }
return ( return (
$link.hasClass("track-link") || link.classList.contains("track-link") ||
$link.closest(".hashtag, .badge-category, .onebox-result, .onebox-body") !link.closest(".hashtag, .badge-category, .onebox-result, .onebox-body")
.length === 0
); );
} }
@ -85,24 +100,25 @@ export default {
} }
} }
const $link = $(e.currentTarget); const link = e.currentTarget;
const tracking = isValidLink($link); const tracking = isValidLink(link);
// Return early for mentions and group mentions // 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; return true;
} }
let href = ($link.attr("href") || $link.data("href") || "").trim(); let href = (link.getAttribute("href") || link.dataset.href || "").trim();
if (!href || href.indexOf("mailto:") === 0) { if (!href || href.startsWith("mailto:")) {
return true; return true;
} }
if ($link.hasClass("attachment")) { if (link.classList.contains("attachment")) {
// Warn the user if they cannot download the file. // Warn the user if they cannot download the file.
if ( if (
siteSettings && siteSettings?.prevent_anons_from_downloading_files &&
siteSettings.prevent_anons_from_downloading_files &&
!User.current() !User.current()
) { ) {
bootbox.alert(I18n.t("post.errors.attachment_download_requires_login")); bootbox.alert(I18n.t("post.errors.attachment_download_requires_login"));
@ -116,23 +132,27 @@ export default {
return false; return false;
} }
const $article = $link.closest( const article = link.closest(
"article:not(.onebox-body), .excerpt, #revisions" "article:not(.onebox-body), .excerpt, #revisions"
); );
const postId = $article.data("post-id"); const postId = article.dataset.postId;
const topicId = $("#topic").data("topic-id") || $article.data("topic-id"); const topicId =
const userId = $link.data("user-id") || $article.data("user-id"); document.querySelector("#topic")?.dataset?.topicId ||
const ownLink = userId && userId === User.currentProp("id"); 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. // Update badge clicks unless it's our own.
if (tracking && !ownLink) { if (tracking && !ownLink) {
const $badge = $("span.badge", $link); const badge = link.querySelector("span.badge");
if ($badge.length === 1) {
const html = $badge.html(); if (badge) {
const html = badge.innerHTML;
const key = `${new Date().toLocaleDateString()}-${postId}-${href}`; const key = `${new Date().toLocaleDateString()}-${postId}-${href}`;
if (/^\d+$/.test(html) && !sessionStorage.getItem(key)) { if (/^\d+$/.test(html) && !sessionStorage.getItem(key)) {
sessionStorage.setItem(key, true); 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 (!wantsNewWindow(e)) {
if (shouldOpenInNewTab(href)) { if (shouldOpenInNewTab(href)) {
openLinkInNewTab($link[0]); openLinkInNewTab(link);
} else { } else {
trackPromise.finally(() => { trackPromise.finally(() => {
if (DiscourseURL.isInternal(href) && samePrefix(href)) { 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) // 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"); const $onebox = $link.closest(".onebox");
if ( if (
$onebox.length === 0 || $onebox.length === 0 ||