mirror of
https://github.com/MCKero6423/fuwari.git
synced 2026-10-02 03:15:35 +00:00
Merge pull request #7 from MCKero6423/copilot/fix-cookie-consent-loading
Fix post-merge regression: cookie consent acceptance does not trigger gated script loading
This commit is contained in:
3 files changed
+91
-22
No files matched your search
@@ -14,11 +14,31 @@ const config = {
|
||||
|
||||
<div id="tcomment"></div>
|
||||
<script define:vars={{ config }}>
|
||||
// Returns true when the user has granted functional consent.
|
||||
// TermsFeed stores cookie_consent_level as a JSON object, e.g.
|
||||
// '{"strictly-necessary":true,"functional":true}' or as a
|
||||
// comma-separated string – handle both gracefully.
|
||||
function _hasFunctionalConsent() {
|
||||
try {
|
||||
var raw = localStorage.getItem('cookie_consent_level') || '';
|
||||
if (!raw) return false;
|
||||
try {
|
||||
var parsed = JSON.parse(raw);
|
||||
if (parsed && typeof parsed === 'object') {
|
||||
var val = parsed['functional'];
|
||||
if (typeof val === 'boolean') return val;
|
||||
if (typeof val === 'string') return val === 'allow' || val === 'true';
|
||||
}
|
||||
} catch (e) { /* not JSON – fall through to string check */ }
|
||||
// Comma-separated fallback: "strictly-necessary,functional"
|
||||
return raw.indexOf('functional') >= 0;
|
||||
} catch (e) {
|
||||
return false; // localStorage unavailable
|
||||
}
|
||||
}
|
||||
|
||||
function loadTwikoo() {
|
||||
// Check functional consent via TermsFeed localStorage
|
||||
var level = '';
|
||||
try { level = localStorage.getItem('cookie_consent_level') || ''; } catch (e) { /* localStorage unavailable – default to no consent */ }
|
||||
if (!level.includes('functional')) {
|
||||
if (!_hasFunctionalConsent()) {
|
||||
// Record that a load was requested so we can retry when consent is granted
|
||||
window._twikooLoadPending = true;
|
||||
return;
|
||||
|
||||
@@ -145,6 +145,40 @@ const bannerOffset =
|
||||
<!-- TermsFeed Cookie Consent -->
|
||||
<script src="https://www.termsfeed.com/public/cookie-consent/4.2.0/cookie-consent.js" charset="UTF-8"></script>
|
||||
<script is:inline>
|
||||
// TermsFeed calls these global window functions when the user changes consent.
|
||||
// They must be defined BEFORE cookieconsent.run() so they are ready when the
|
||||
// library initialises and when the user interacts with the consent banner.
|
||||
(function () {
|
||||
function _dispatchConsentUpdated() {
|
||||
document.dispatchEvent(new CustomEvent('cookieConsentUpdated'));
|
||||
}
|
||||
window.cookieconsent_accept_all = _dispatchConsentUpdated;
|
||||
window.cookieconsent_reject_all = _dispatchConsentUpdated;
|
||||
window.cookieconsent_save = _dispatchConsentUpdated;
|
||||
|
||||
// Belt-and-suspenders: also listen for DOM-event dispatch used by some
|
||||
// TermsFeed versions, on both document and window.
|
||||
['cookieconsent_accept_all', 'cookieconsent_reject_all', 'cookieconsent_save'].forEach(function (evtName) {
|
||||
document.addEventListener(evtName, _dispatchConsentUpdated);
|
||||
window.addEventListener(evtName, _dispatchConsentUpdated);
|
||||
});
|
||||
|
||||
// Polling fallback: detect localStorage changes (e.g. consent set in
|
||||
// another tab or if the callback was not called for any reason).
|
||||
var _lastLevel = '';
|
||||
try { _lastLevel = localStorage.getItem('cookie_consent_level') || ''; } catch (e) {}
|
||||
var _pollTimer = setInterval(function () {
|
||||
var cur = '';
|
||||
try { cur = localStorage.getItem('cookie_consent_level') || ''; } catch (e) {}
|
||||
if (cur !== _lastLevel) {
|
||||
_lastLevel = cur;
|
||||
_dispatchConsentUpdated();
|
||||
}
|
||||
// Stop polling once a non-empty consent value is persisted.
|
||||
if (cur) clearInterval(_pollTimer);
|
||||
}, 500);
|
||||
}());
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
if (typeof cookieconsent === 'undefined') return;
|
||||
cookieconsent.run({
|
||||
@@ -159,12 +193,6 @@ const bannerOffset =
|
||||
"website_name": "MCKero blog",
|
||||
"website_privacy_policy_url": "https://blog.mckero.com/about/"
|
||||
});
|
||||
// Relay TermsFeed consent events as a single custom event so components can react
|
||||
['cookieconsent_accept_all', 'cookieconsent_reject_all', 'cookieconsent_save'].forEach(function (evtName) {
|
||||
window.addEventListener(evtName, function () {
|
||||
document.dispatchEvent(new CustomEvent('cookieConsentUpdated'));
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
|
||||
@@ -148,8 +148,10 @@ const jsonLd = {
|
||||
window.tianliGPT_Title = '智能摘要';
|
||||
window.tianliGPT_key = 'S-HMJ7UXQGBIAZ0U8Q';
|
||||
|
||||
// 立即尝试初始化
|
||||
function tryInitTianliGPT() {
|
||||
// Expose on window so the loader script's onload handler (in a different
|
||||
// inline script block) can always reach the latest version of this function
|
||||
// after Swup page transitions replace the DOM content.
|
||||
window._tryInitTianliGPT = function tryInitTianliGPT() {
|
||||
if (typeof window.tianliGPT === 'object' &&
|
||||
typeof window.tianliGPT.checkURLAndRun === 'function') {
|
||||
|
||||
@@ -166,33 +168,52 @@ const jsonLd = {
|
||||
}, 500);
|
||||
} else if (window.tianliGPT_scriptLoaded) {
|
||||
// 脚本已请求加载但尚未就绪,继续等待
|
||||
setTimeout(tryInitTianliGPT, 200);
|
||||
setTimeout(window._tryInitTianliGPT, 200);
|
||||
}
|
||||
// else: 脚本未加载(无功能性同意),停止轮询
|
||||
}
|
||||
};
|
||||
|
||||
// 开始尝试
|
||||
tryInitTianliGPT();
|
||||
window._tryInitTianliGPT();
|
||||
</script>
|
||||
|
||||
<!-- 主脚本 - 只在获得功能性同意后加载,且只加载一次 -->
|
||||
<script is:inline>
|
||||
(function () {
|
||||
if (window.tianliGPT_scriptLoaded) return;
|
||||
// Returns true when the user has granted functional consent.
|
||||
// TermsFeed stores cookie_consent_level as JSON or comma-separated string.
|
||||
function _hasFunctionalConsent() {
|
||||
try {
|
||||
var raw = localStorage.getItem('cookie_consent_level') || '';
|
||||
if (!raw) return false;
|
||||
try {
|
||||
var parsed = JSON.parse(raw);
|
||||
if (parsed && typeof parsed === 'object') {
|
||||
var val = parsed['functional'];
|
||||
if (typeof val === 'boolean') return val;
|
||||
if (typeof val === 'string') return val === 'allow' || val === 'true';
|
||||
}
|
||||
} catch (e) { /* not JSON – fall through */ }
|
||||
return raw.indexOf('functional') >= 0;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function doLoadTianliGPT() {
|
||||
if (window.tianliGPT_scriptLoaded) return;
|
||||
// Check functional consent via TermsFeed localStorage
|
||||
var level = '';
|
||||
try { level = localStorage.getItem('cookie_consent_level') || ''; } catch (e) { /* localStorage unavailable – default to no consent */ }
|
||||
if (!level.includes('functional')) return;
|
||||
if (window.tianliGPT_scriptLoaded) {
|
||||
// Script already loaded; just (re-)initialise for the current page.
|
||||
if (typeof window._tryInitTianliGPT === 'function') window._tryInitTianliGPT();
|
||||
return;
|
||||
}
|
||||
if (!_hasFunctionalConsent()) return;
|
||||
|
||||
window.tianliGPT_scriptLoaded = true;
|
||||
var script = document.createElement('script');
|
||||
script.src = 'https://ai.zhheo.com/static/public/tianli_gpt.min.js';
|
||||
script.async = true;
|
||||
script.onload = function () {
|
||||
if (typeof tryInitTianliGPT === 'function') tryInitTianliGPT();
|
||||
if (typeof window._tryInitTianliGPT === 'function') window._tryInitTianliGPT();
|
||||
};
|
||||
document.body.appendChild(script);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user