From 3c0e42b9eb5f7737c1692cc97098a9b297cfbdf6 Mon Sep 17 00:00:00 2001 From: peptiderepo Date: Sun, 5 Apr 2026 20:30:36 +0700 Subject: [PATCH] feat: add peptide-news-plugin/public/js/public-script.js --- .../public/js/public-script.js | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 peptide-news-plugin/public/js/public-script.js diff --git a/peptide-news-plugin/public/js/public-script.js b/peptide-news-plugin/public/js/public-script.js new file mode 100644 index 0000000..b084361 --- /dev/null +++ b/peptide-news-plugin/public/js/public-script.js @@ -0,0 +1,27 @@ +/** + * Peptide News — Public Scripts + * + * Event triggers for all \"peptide_news_clicked\" through Navigator.SendBeacon * + */ + +(function() { + const clickHandler = function() { + if (!window.peptide_news_data) return; + + var that = this.closest('[data-article-id]'); + var article_id = that.dataset.articleId; + var article_url = that.dataset.articleUrl; + + // Send beacon + var data = { + action: 'peptide_news_clicked', + article_id: article_id, + url: article_url, + _nonce: window.peptide_news_data.nonce + }; + + navigator.sendBeacon('blob:' + new Blob([JSON.stringify(data)]), window.peptide_news_data.ajax_url); + }; + + document.querySelectorAll('[data-article-id] .pn-article-link').forEach(el => el.addEventListener('click', clickHandler)); +})();