PageSourceSearch

https://yjho.info/wp-content/plugins/sc-icg-compat/assets/js/elw-nonce-refresh.js?ver=1.4.0

js yjho.info collected 2026-10-02 01:57:26 UTC 2,461 bytes, 80 lines download raw bytes

1/**
2 * Easy Login WooCommerce × 整頁快取 nonce 換發。
3 *
4 * 訪客開啟登入 / 註冊框時取一次新鮮 nonce,覆寫被整頁快取凍結的舊值。
5 * 詳細背景見 inc/elw-nonce-refresh.php 檔頭。
6 */
7( function ( $ ) {
8	'use strict';
9
10	if ( typeof window.xoo_el_localize === 'undefined' || typeof window.scIcgCompatElw === 'undefined' ) {
11		return;
12	}
13
14	var lastRefresh = 0;
15	var inFlight = false;
16	var maxAgeMs = ( parseInt( window.scIcgCompatElw.maxAge, 10 ) || 1800 ) * 1000;
17
18	/**
19	 * 換發 nonce。
20	 *
21	 * @param {boolean} force 略過新鮮度檢查(AJAX 已被 403 擋下時使用)。
22	 */
23	function refreshNonce( force ) {
24		if ( inFlight ) {
25			return;
26		}
27		if ( ! force && lastRefresh && Date.now() - lastRefresh < maxAgeMs ) {
28			return;
29		}
30
31		inFlight = true;
32
33		$.post( window.scIcgCompatElw.ajaxUrl, { action: 'sc_icg_compat_elw_nonce' } )
34			.done( function ( res ) {
35				if ( res && res.success && res.data && res.data.nonce ) {
36					window.xoo_el_localize.nonce = res.data.nonce;
37					lastRefresh = Date.now();
38				}
39			} )
40			.always( function () {
41				inFlight = false;
42			} );
43	}
44
45	// 主要觸發點:ELW 開啟或切換登入 / 註冊 / 忘記密碼表單時(xoo-el-js.js 的公開事件)。
46	$( document.body ).on( 'xoo_el_form_toggled', function () {
47		refreshNonce( false );
48	} );
49
50	// 保險觸發點一:inline 表單不一定觸發 toggled,改聽觸發器點擊。
51	$( document ).on(
52		'click',
53		'.xoo-el-login-tgr, .xoo-el-reg-tgr, .xoo-el-lostpw-tgr, .xoo-el-sing-tgr',
54		function () {
55			refreshNonce( false );
56		}
57	);
58
59	// 保險觸發點二:使用者已開始輸入才載入本腳本的情境(如 JS delay)。
60	$( document ).on( 'focus', '.xoo-el-form-container input', function () {
61		refreshNonce( false );
62	} );
63
64	/**
65	 * 保險觸發點三:ELW 的請求真的被 403 擋下時強制換發。
66	 *
67	 * ELW 失敗後會自動重送 2 次,jQuery 的全域 ajaxError 早於它的 local complete 執行,
68	 * 因此這裡發出的換發請求有機會趕在第 2 次重送前完成,讓流程自癒。
69	 * 屬 best-effort:即使沒趕上,使用者重新送出時 nonce 也已是新的。
70	 */
71	$( document ).ajaxError( function ( event, jqxhr, settings ) {
72		if ( ! settings || typeof settings.url !== 'string' ) {
73			return;
74		}
75		if ( settings.url.indexOf( 'admin-ajax.php' ) === -1 || jqxhr.status !== 403 ) {
76			return;
77		}
78		refreshNonce( true );
79	} );
80} )( jQuery );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.