1document.addEventListener('DOMContentLoaded', function (event) { 2 (function () { 3 var checkPaidInterval = null; 4 var wp_rest_base_url = LN_Paywall.rest_base; 5 var LN_Paywall_Spinner = 6 '<svg class="LNP_spinner" viewBox="0 0 50 50"><circle class="LNP_path" cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle></svg>'; 7 var LN_Paywall_Copy = 8 '<svg xmlns="http://www.w3.org/2000/svg" class="LNP_copy" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"><path class="LNP_path" stroke-linecap="round" stroke-linejoin="round" d="M15.75 17.25v3.375c0 .621-.504 1.125-1.125 1.125h-9.75a1.125 1.125 0 01-1.125-1.125V7.875c0-.621.504-1.125 1.125-1.125H6.75a9.06 9.06 0 011.5.124m7.5 10.376h3.375c.621 0 1.125-.504 1.125-1.125V11.25c0-4.46-3.243-8.161-7.5-8.876a9.06 9.06 0 00-1.5-.124H9.375c-.621 0-1.125.504-1.125 1.125v3.5m7.5 10.375H9.375a1.125 1.125 0 01-1.125-1.125v-9.25m12 6.625v-1.875a3.375 3.375 0 00-3.375-3.375h-1.5a1.125 1.125 0 01-1.125-1.125v-1.5a3.375 3.375 0 00-3.375-3.375H9.75" /></svg>'; 9 10 function pay(invoice, options) { 11 if (!window.webln) { 12 showQRCode(invoice, options); 13 return startWatchingForPayment(invoice); 14 } 15 return window.webln 16 .enable() 17 .then(function () { 18 window.webln 19 .sendPayment(invoice.payment_request) 20 .then((response) => { 21 window.LNP_CURRENT_PREIMAGE = 22 response.preimage || response.payment_preimage; 23 }) 24 .catch(function (e) { 25 console.error(e); 26 showQRCode(invoice, options); 27 }); 28 return startWatchingForPayment(invoice); 29 }) 30 .catch(function (err) { 31 console.error(err); 32 showQRCode(invoice, options); 33 return startWatchingForPayment(invoice); 34 }); 35 } 36 37 function startWatchingForPayment(invoice) { 38 stopWatchingForPayment(); 39 return new Promise(function (resolve, reject) { 40 checkPaidInterval = setInterval( 41 checkPaymentStatus(invoice, resolve), 42 2500 43 ); 44 }); 45 } 46 47 function stopWatchingForPayment() { 48 window.LNP_CURRENT_PREIMAGE = null; 49 if (checkPaidInterval) { 50 clearTimeout(checkPaidInterval); 51 checkPaidInterval = null; 52 } 53 } 54 55 function checkPaymentStatus(invoice, callback) { 56 if (!invoice || !invoice.token) { 57 console.log('Ligthning invoice missing'); 58 return; 59 } 60 return function () { 61 let body = { 62 token: invoice.token, 63 preimage: window.LNP_CURRENT_PREIMAGE, 64 t: Date.now(), 65 }; 66 fetch(wp_rest_base_url + '/paywall/verify', { 67 method: 'POST', 68 credentials: 'same-origin', 69 cache: 'no-cache', 70 body: JSON.stringify(body), 71 headers: { 72 'Content-Type': 'application/json', 73 }, 74 }).then(function (response) { 75 if (response.ok) { 76 response.json().then(function (content) { 77 stopWatchingForPayment(); 78 callback(content, invoice); 79 }); 80 } 81 }); 82 }; 83 } 84 85 function showQRCode(invoice, options) { 86 var button = options.target.querySelector('button.wp-lnp-btn'); 87 button.outerHTML = `<div class="wp-lnp-qrcode"> 88 <a href="lightning:${ 89 invoice.payment_request 90 }">
90<img src="https://quickchart.io/chart?cht=qr&chs=200x200&chl=${ 91 invoice.payment_request 92 }"></a> 93 <br /> 94 <a href="lightning:${ 95 invoice.payment_request 96 }">${invoice.payment_request.substr( 97 0, 98 36 99 )}...</a><span onClick="navigator.clipboard.writeText('${ 100 invoice.payment_request 101 }');" class="wp-lnp-copy">${LN_Paywall_Copy}</span> 102 </div>`; 103 } 104 105 function requestPayment(params, options) { 106 return fetch(wp_rest_base_url + '/paywall/pay', { 107 method: 'POST', 108 credentials: 'same-origin', 109 cache: 'no-cache', 110 body: JSON.stringify(params), 111 headers: { 112 'Content-Type': 'application/json', 113 }, 114 }) 115 .then(function (resp) { 116 return resp.json(); 117 }) 118 .then(function (invoice) { 119 if (!invoice || !invoice.payment_request) { 120 console.error( 121 'Failed to generate lightning invoice', 122 invoice 123 ); 124 throw new Error( 125 'Failed to generate lightning invoice ' + 126 JSON.stringify(invoice) 127 ); 128 } 129 return pay(invoice, options); 130 }); 131 } 132 133 function initPostPaywalls() { 134 var buttons = [].slice.call( 135 document.querySelectorAll('.wp-lnp-btn') 136 ); 137 if (buttons.length === 0) { 138 return; 139 } 140 buttons.forEach(function (button) { 141 button.addEventListener('click', function (e) { 142 e.preventDefault(); 143 this.setAttribute('disabled', ''); 144 145 this.innerHTML = LN_Paywall_Spinner; 146 var wrapper = this.closest('.wp-lnp-wrapper'); 147 148 requestPayment( 149 { post_id: parseInt(window.LN_Paywall.post_id) }, 150 { target: wrapper } 151 ) 152 .then(function (content, invoice) { 153 wrapper.outerHTML = content; 154 }) 155 .catch(function (e) { 156 console.log(e); 157 alert('sorry, something went wrong.'); 158 }); 159 }); 160 }); 161 } 162 163 initPostPaywalls(); 164 })(); 165});
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.