1// amazonLoginInit.js 2// AmazonPayã§ã®ãã°ã¤ã³ãã¿ã³ã®åæåãè¡ã 3// 4// ãµã¤ã³ã¤ã³ãã¿ã³å®è£ ããã¥ã¡ã³ã 5// https://amazonpay-onboarding.com/other_features/signIn/integration/ 6 7const RENDERED_ATTR = 'data-amazon-rendered'; 8 9// Amazon Pay SDK ã®æºåå®äºãå¾ ã¤ã¦ã¼ãã£ãªã㣠10function ensureAmazonReady(cb) { 11 if (window.amazon?.Pay?.renderButton) cb(); 12 else setTimeout(() => ensureAmazonReady(cb), 50); 13} 14 15// åä¸è¦ç´ ã«å¯¾ã㦠Amazon ãµã¤ã³ã¤ã³ãã¿ã³ãæç»ããï¼åå©ç¨å¯è½ï¼ 16function renderAmazonButton(el) { 17 if (!el) return; 18 // ã¬ã³ããªã³ã°æ¸ã¿ã§ããã°ä½ãããªã 19 if (el.getAttribute(RENDERED_ATTR) === '1') return; 20 // è¤æ°å¯¾å¿ï¼IDãç¡ããã°ä¸æã®IDãä»ä¸ 21 if (!el.id) { 22 el.id = 'AmazonSignInButton_' + Math.random().toString(36).slice(2); 23 } 24 // ãªãã·ã§ã³ããã¼ã¹ 25 let options = {}; 26 try { 27 options = JSON.parse(el.getAttribute('data-amazon-options') || '{}'); 28 } catch (e) { 29 console && console.error && console.error('[Amazon Signin] options JSON parse error:', e, el.getAttribute('data-amazon-options')); 30 return; 31 } 32 // SDK æºåå®äºå¾ã«æç» 33 ensureAmazonReady(() => { 34 try { 35 amazon.Pay.renderButton('#' + el.id, options); 36 el.setAttribute(RENDERED_ATTR, '1'); 37 } catch (e) { 38 console && console.error && console.error('[Amazon Signin] renderButton failed:', e, options); 39 } 40 }); 41} 42 43// Amazonãã°ã¤ã³ãã¿ã³ãæç»ãã data-amazon-signin ã®è¦ç´ ãåå¾ 44function collectAmazonSigninNodes(root, includeRendered = true) { 45 const nodes = []; 46 const rootMatches = root.matches?.('[data-amazon-signin]') ?? false; 47 48 if (rootMatches) { 49 const alreadyRendered = root.getAttribute(RENDERED_ATTR) === '1'; 50 if (includeRendered || !alreadyRendered) { 51 nodes.push(root); 52 } 53 } 54 55 if (root.querySelectorAll) { 56 const selector = includeRendered 57 ? '[data-amazon-signin]' 58 : '[data-amazon-signin]:not([' + RENDERED_ATTR + '])'; 59 60 root.querySelectorAll(selector).forEach((el) => { 61 if (!nodes.includes(el)) { 62 nodes.push(el); 63 } 64 }); 65 } 66 67 return nodes; 68} 69 70// ã¾ã ãã°ã¤ã³ãã¿ã³ããªãè¦ç´ ãæ¢ãã¦æç» 71function initAmazonButtons(root = document) { 72 const nodes = collectAmazonSigninNodes(root, false); 73 if (!nodes.length) return; 74 nodes.forEach(renderAmazonButton); 75} 76 77// æç»æ¸ã¿ãã¿ã³è¦ç´ ãåæç» 78function rerenderAmazonButtons(root = document) { 79 const nodes = collectAmazonSigninNodes(root, true); 80 if (!nodes.length) return; 81 82 nodes.forEach((el) => { 83 const parent = el.parentNode; 84 if (!parent) return; 85 86 // ãã¿ã³ã®IDãæç»ã¹ãã¼ã¿ã¹ãåé¤ãã¦åæç» 87 const newEl = el.cloneNode(false); 88 newEl.removeAttribute('id'); 89 newEl.removeAttribute(RENDERED_ATTR); 90 parent.replaceChild(newEl, el); 91 renderAmazonButton(newEl); 92 }); 93} 94 95// ãã¼ã¸è¡¨ç¤ºæã«AmazonLoginãã¿ã³ãå«ã¾ããæã®ã¤ãã³ã 96if (document.readyState === 'loading') { 97 document.addEventListener('DOMContentLoaded', () => initAmazonButtons()); 98} else { 99 initAmazonButtons(); 100} 101 102// DOMå¤åãç£è¦ãã¦èªååæå (XHRã§ã®ãã¼ã¸å¤æ´æã®ã¤ãã³ã) 103const mo = new MutationObserver((mutations) => { 104 for (const m of mutations) { 105 m.addedNodes.forEach((n) => { 106 if (n.nodeType !== 1) return; 107 if (n.matches?.('[data-amazon-signin]') || n.querySelector?.('[data-amazon-signin]')) { 108 initAmazonButtons(n); 109 } 110 }); 111 } 112}); 113mo.observe(document.documentElement, { childList: true, subtree: true }); 114 115// å¤é¨ããåæç»ãè¦æ±ããããã®ã¤ãã³ããã㯠116// document.dispatchEvent(new CustomEvent('amazon:signin:refresh', { detail: { root: container, options: { ... } } })); 117// detail.options ãæ¸¡ãããå ´å㯠data 屿§ã䏿¸ããã¦ããåæç»ãã¾ãã 118document.addEventListener('amazon:signin:refresh', (e) => { 119 const scope = e.detail?.root || document; 120 const incomingOpts = e.detail?.options; 121 if (incomingOpts) { 122 const json = JSON.stringify(incomingOpts); 123 scope.querySelectorAll('[data-amazon-signin]').forEach((el) => { 124 el.setAttribute('data-amazon-options', json); 125 }); 126 } 127 rerenderAmazonButtons(scope); 128});
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.