-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtianli_gpt.min.js
1 lines (1 loc) · 8.18 KB
/
tianli_gpt.min.js
1
var tianliGPTIsRunning=!1;function tianliGPT(t){function e(t){!function(){const t=document.querySelector(".post-TianliGPT");t&&t.parentElement.removeChild(t)}();let e=document.querySelector(t);if(!e){const n=2e4,i=300;let o=0;const a=setInterval((()=>{if(o+=i,e=document.querySelector(t),e)clearInterval(a);else if(o>=n)return void clearInterval(a)}),i)}const n=document.createElement("div");n.className="post-TianliGPT";const i=document.createElement("div");i.className="tianliGPT-title",n.appendChild(i);const o=document.createElement("i");o.className="tianliGPT-title-icon",i.appendChild(o),o.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="48px" height="48px" viewBox="0 0 48 48">\n <g id="机器人" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">\n <path d="M34.717885,5.03561087 C36.12744,5.27055371 37.079755,6.60373651 36.84481,8.0132786 L35.7944,14.3153359 L38.375,14.3153359 C43.138415,14.3153359 47,18.1768855 47,22.9402569 L47,34.4401516 C47,39.203523 43.138415,43.0650727 38.375,43.0650727 L9.625,43.0650727 C4.861585,43.0650727 1,39.203523 1,34.4401516 L1,22.9402569 C1,18.1768855 4.861585,14.3153359 9.625,14.3153359 L12.2056,14.3153359 L11.15519,8.0132786 C10.920245,6.60373651 11.87256,5.27055371 13.282115,5.03561087 C14.69167,4.80066802 16.024865,5.7529743 16.25981,7.16251639 L17.40981,14.0624532 C17.423955,14.1470924 17.43373,14.2315017 17.43948,14.3153359 L30.56052,14.3153359 C30.56627,14.2313867 30.576045,14.1470924 30.59019,14.0624532 L31.74019,7.16251639 C31.975135,5.7529743 33.30833,4.80066802 34.717885,5.03561087 Z M38.375,19.4902885 L9.625,19.4902885 C7.719565,19.4902885 6.175,21.0348394 6.175,22.9402569 L6.175,34.4401516 C6.175,36.3455692 7.719565,37.89012 9.625,37.89012 L38.375,37.89012 C40.280435,37.89012 41.825,36.3455692 41.825,34.4401516 L41.825,22.9402569 C41.825,21.0348394 40.280435,19.4902885 38.375,19.4902885 Z M14.8575,23.802749 C16.28649,23.802749 17.445,24.9612484 17.445,26.3902253 L17.445,28.6902043 C17.445,30.1191812 16.28649,31.2776806 14.8575,31.2776806 C13.42851,31.2776806 12.27,30.1191812 12.27,28.6902043 L12.27,26.3902253 C12.27,24.9612484 13.42851,23.802749 14.8575,23.802749 Z M33.1425,23.802749 C34.57149,23.802749 35.73,24.9612484 35.73,26.3902253 L35.73,28.6902043 C35.73,30.1191812 34.57149,31.2776806 33.1425,31.2776806 C31.71351,31.2776806 30.555,30.1191812 30.555,28.6902043 L30.555,26.3902253 C30.555,24.9612484 31.71351,23.802749 33.1425,23.802749 Z" id="形状结合" fill="#444444" fill-rule="nonzero"></path>\n </g>\n </svg>';const a=document.createElement("div");a.className="tianliGPT-title-text","undefined"==typeof tianliGPT_Title?a.textContent="AI摘要":a.textContent=tianliGPT_Title,i.appendChild(a);const l=document.createElement("div");l.className="tianliGPT-tag",l.id="tianliGPT-tag","undefined"==typeof tianliGPT_Name?l.textContent="TianliGPT":l.textContent=tianliGPT_Name,i.appendChild(l);const r=document.createElement("div");r.className="tianliGPT-explanation",r.innerHTML='生成中...<span class="blinking-cursor"></span>',n.appendChild(r),e.insertBefore(n,e.firstChild)}var n={getTitleAndContent:function(){try{const t=document.title,e=document.querySelector(tianliGPT_postSelector);if(!e)return"";const n=e.getElementsByTagName("p"),i=e.querySelectorAll("h1, h2, h3, h4, h5");let o="";for(let t of i)o+=t.innerText+" ";for(let t of n){o+=t.innerText.replace(/https?:\/\/[^\s]+/g,"")}let a=1e3;"undefined"!=typeof tianliGPT_wordLimit&&(a=tianliGPT_wordLimit);return(t+" "+o).slice(0,a)}catch(t){return""}},fetchTianliGPT:async function(t){if(!tianliGPT_key){let t="没有获取到key,代码可能没有安装正确。如果你需要在tianli_gpt文件引用前定义tianliGPT_key变量。详细请查看文档。";return n.aiShowAnimation(t),t}if("5Q5mpqRK5DkwT1X9Gi5e"===tianliGPT_key){let t="请购买 key 使用,如果你能看到此条内容,则说明代码安装正确。";return n.aiShowAnimation(t),t}window.location.href;document.title;try{const e=new AbortController,i=setTimeout((()=>e.abort()),2e4),o=await fetch("https://summary.tianli0.top/",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({content:t,key:tianliGPT_key,url:window.location.href,title:document.title}),signal:e.signal});if(clearTimeout(i),o.ok){return{summary:(await o.json()).summary}}{let t="";const e=await o.json();if(514===o.status)return t="TianliGPT is only available in mainland China, and is not yet open to overseas users, so stay tuned!",n.aiShowAnimation(t),t;if(403===o.status)switch(e.err_code){case 1:return t='你的网站设置了Referrer-Policy为same-origin,这会导致Tianli无法验证你的请求来源。TianliGPT依赖refer进行来源判断,特别是meta标签的referrer属性需要修改,至少为origin。例如:<meta name="referrer" content="origin">',n.aiShowAnimation(t),t;case 2:return t="你正在使用的tianliGPT_key没有绑定当前网站,请检查当前的密钥是否绑定了当前网站地址。可以到summary.zhheo.com中绑定。",n.aiShowAnimation(t),t;case 3:return t="参数缺失,请检查是否正确配置tianliGPT_key",n.aiShowAnimation(t),t;case 4:throw document.querySelectorAll(".post-TianliGPT").forEach((t=>{t.style.display="none"})),t="Key错误或余额不足,请充值后请求新的文章",new Error("TianliGPT:"+t);case 5:return t=e.err_msg,n.aiShowAnimation(t),t;case 6:return t=e.err_msg,n.aiShowAnimation(t),t;case 7:return t=e.err_msg,n.aiShowAnimation(t),t;default:return n.aiShowAnimation("未知错误,请检查API文档"),"未知错误,请检查API文档"}}}catch(t){return"AbortError"===t.name?"localhost"===window.location.hostname?"获取文章摘要超时。请勿在本地主机上测试 API 密钥。":"获取文章摘要超时。当你出现这个问题时,可能是key或者绑定的域名不正确。也可能是因为文章过长导致的 AI 运算量过大,您可以稍等一下然后刷新页面重试。":"获取文章摘要失败,请稍后再试。"}},aiShowAnimation:function(t){const e=document.querySelector(".tianliGPT-explanation");if(!e)return;if(tianliGPTIsRunning)return;if("undefined"!=typeof tianliGPT_typingAnimate&&!tianliGPT_typingAnimate)return void(e.innerHTML=t);tianliGPTIsRunning=!0;e.style.display="block",e.innerHTML='生成中...<span class="blinking-cursor"></span>';document.querySelector(".tianliGPT-tag").classList.add("loadingAI");let n=!0,i=0,o=performance.now();const a=()=>{if(i<t.length&&n){const n=performance.now(),r=n-o,s=t.slice(i,i+1);if(r>=(/[,。!、?,.!?]/.test(s)?150:25))if(e.innerText=t.slice(0,i+1),o=n,i++,i<t.length)e.innerHTML=t.slice(0,i)+'<span class="blinking-cursor"></span>';else{e.innerHTML=t,e.style.display="block",tianliGPTIsRunning=!1,l.disconnect();document.querySelector(".tianliGPT-tag").classList.remove("loadingAI")}requestAnimationFrame(a)}},l=new IntersectionObserver((t=>{let e=t[0].isIntersecting;n=e,n&&setTimeout((()=>{requestAnimationFrame(a)}),200)}),{threshold:0});let r=document.querySelector(".post-TianliGPT");l.observe(r)}};function i(){if("undefined"==typeof tianliGPT_postSelector)return;e(tianliGPT_postSelector);const t=n.getTitleAndContent();n.fetchTianliGPT(t).then((t=>{const e=t.summary;n.aiShowAnimation(e)}))}function o(){if("undefined"!=typeof tianliGPT_postURL)try{const t=t=>new RegExp("^"+t.split(/\*+/).map(e).join(".*")+"$"),e=t=>t.replace(/[|\\{}()[\]^$+*?.]/g,"\\$&"),n=t(tianliGPT_postURL),i=window.location.href;n.test(i)&&function(){const t=20;let e=0;const n=setInterval((()=>{try{a(),clearInterval(n)}catch(i){e>=t&&clearInterval(n),e++}}),200)}()}catch(t){}else a()}function a(){"undefined"!=typeof tianliGPT_blacklist&&tianliGPT_blacklist?fetch(tianliGPT_blacklist).then((t=>t.json())).then((t=>{const e=t.blackurls;let n=window.location.href;e.some((t=>new RegExp("^"+t.replace(/\*/g,".*")+"$").test(n)))||i()})).catch((t=>{i()})):i()}t?o():document.addEventListener("DOMContentLoaded",(function(){o()}))}tianliGPT(!1),document.addEventListener("pjax:complete",(function(){tianliGPT(!0)}))((function(t){var e=t.pushState;t.pushState=function(n){return"function"==typeof t.onpushstate&&setTimeout((function(){t.onpushstate({state:n})}),100),e.apply(t,arguments)}}))(window.history),window.history.onpushstate=function(t){tianliGPT(!0)};