diff --git a/searx/webapp.py b/searx/webapp.py index f018dd5bb..487c9a5a4 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -1486,3304 +1486,6 @@ function modal_open(url, num) }); - modalele = ['这是一个PDF文档'] - sentencesContent = '' - for (let i = 0; i < sentences.length; i++) { - sentencesContent += sentences[i][1]; - } - article = {'textContent':sentencesContent,'title':iframe.contentWindow.PDFViewerApplication._title} - - - }).catch(function(error) { - console.error(error); //处理错误情况 - }); - - } - else - { - modalele = eleparse(iframe.contentDocument); - article = new Readability(iframe.contentDocument.cloneNode(true)).parse(); - } - resolve("success"); - }) - }).then( - () => { - fulltext = article.textContent; - fulltext = fulltext.replaceAll("\n\n","\n").replaceAll("\n\n","\n"); - const delimiter = /[?!;\?\n。;!………]/g - fulltext = fulltext.split(delimiter); - fulltext = fulltext.filter((item) => { - // 使用正则表达式匹配仅由数字、逗号和空格组成的字符串 - const regex = /^[0-9,\s]+$/; - return !regex.test(item); - }); - fulltext = fulltext.filter(function(item) { - return item && item.trim(); // 去掉空值和空格 - }); - optkeytext = { - method: "POST", - headers: headers, - body: JSON.stringify({'text':fulltext.join("\n")}) - }; - fetchRetry('https://search.kg/keytext',3,optkeytext) - .then(response => response.json()) - .then(data => - { - keytextres = unique(data); - - promptWebpage = '网页标题:'+ article.title +'\n'+'网页布局:\n' - for (el in modalele) - { - if((promptWebpage + modalele[el] + '\n').length <400) - promptWebpage = promptWebpage + modalele[el] + '\n'; - } - promptWebpage = promptWebpage +'网页内容:\n' - keySentencesCount = 0; - for (st in keytextres) - { - if((promptWebpage + keytextres[st] + '\n').length <1200) - promptWebpage = promptWebpage + keytextres[st] + '\n'; - keySentencesCount = keySentencesCount+1; - } - promptWeb = [{'role':'assistant','content':promptWebpage},{'role':'user','content':'总结网页内容,发表带emoji的评论'}] - const options = { - method: "POST", - headers: headers, - body: b64EncodeUnicode( JSON.stringify({ - "messages": promptWeb.concat(add_system), - "max_tokens": 1000, - "temperature": 0.9, - "top_p": 1, - "frequency_penalty": 0, - "presence_penalty": 0, - "stream": true - }) ) - }; - - chatTemp = "" - text_offset = -1; - prev_chat = document.getElementById('chat_talk').innerHTML; - - fetch("https://search.kg/completions", options) - .then((response) => { - const reader = response.body.getReader(); - let result = ''; - let half = ''; - reader.read().then(function processText({ done, value }) { - if (done) return; - const text = new TextDecoder('utf-8').decode(value); - text.trim().split('\n').forEach(function(v) { - try{document.querySelector("#chat_talk").scrollTop = document.querySelector("#chat_talk").scrollHeight}catch(e){} - result = '' - if(v.length>6) result = v.slice(6); - if(result == "[DONE]") - { - lock_chat=0 - return; - } - let choices; - try - { - try{choices=JSON.parse(half+result)['choices'];half = '';} - catch(e){choices=JSON.parse(result)['choices'];half = '';} - }catch(e){half+=result} - if(choices && choices.length>0 && choices[0].delta.content) - { - chatTemp+=choices[0].delta.content - } - chatTemp=chatTemp.replaceAll("\n\n","\n").replaceAll("\n\n","\n") - document.querySelector("#prompt").innerHTML=""; - markdownToHtml(beautify(chatTemp), document.querySelector("#prompt")) - document.getElementById('chat_talk').innerHTML = prev_chat+'
'+document.querySelector("#prompt").innerHTML+"
"; - - }) - return reader.read().then(processText); - }); - }) - .catch((error) => { - console.error('Error:', error); - }); - - - - - }) - - - - - }, - error => { - console.log(error); - } - ); -} - - -function eleparse(doc) -{ - // 获取页面元素 -const elements = doc.querySelectorAll("*"); - -// 定义位置常量 -const POSITION = { - TOP_LEFT: "左上", - TOP_MIDDLE: "上中", - TOP_RIGHT: "右上", - MIDDLE_LEFT: "左中", - CENTER: "中间", - MIDDLE_RIGHT: "右中", - BOTTOM_LEFT: "左下", - BOTTOM_MIDDLE: "下中", - BOTTOM_RIGHT: "右下", -}; - -// 定义颜色名称映射表 -const COLOR_NAMES = { - "#000000": "黑色", - "#ffffff": "白色", - "#ff0000": "红色", - "#00ff00": "绿色", - "#0000ff": "蓝色", - // 可以添加更多颜色 -}; - -// 创建描述文本 -let description=[]; -let seen = []; -let dismiss = ['up vote', 'down vote', 'dismiss', 'github license', 'npm version', 'circleci', 'site'] -for (let i = 0; i < elements.length; i++) { - const element = elements[i]; - let elementDescription = ""; - - // 判断元素是否可见 - if (element.offsetWidth > 0 || element.offsetHeight > 0) { - - // 获取元素类型 - let elementType = element.tagName.toLowerCase(); - if (elementType === "input" && (element.type === "search" || (element.getAttribute('aria-label') && element.getAttribute('aria-label').toLowerCase().indexOf("search") !== -1))) { - elementType = "搜索框"; - } else if (elementType === "input" || elementType === "select" || elementType === "textarea") { - elementType = "输入框"; - } else if (elementType.indexOf("button") !== -1 || element.id.indexOf("button") !== -1) { - elementType = "按钮"; - } else if (elementType === "img") { - elementType = "图片"; - } else if (elementType === "form") { - elementType = "表单"; - } else if (elementType === "pre" || elementType === "code") { - elementType = "代码块"; - } - else { - elementType = null; - } - - // 如果是可识别的元素类型,则生成元素描述 - if (elementType && (elementType == "代码块" || element.title||element.alt||element.getAttribute('aria-label'))){ - elementDescription += elementType; - if (element.title) { - if(element.title.indexOf('avatar') != -1 || dismiss.includes((element.title.toLowerCase()))) continue; - elementDescription += `:“${element.title}”`; - } - else if (element.alt||element.getAttribute('aria-label')) { - if(seen.includes(element.alt||element.getAttribute('aria-label'))) continue; - if((element.alt||element.getAttribute('aria-label')).includes('avatar') || dismiss.includes((element.alt||element.getAttribute('aria-label')).toLowerCase())) continue; - elementDescription += `:“${element.alt||element.getAttribute('aria-label')}”`; - seen.push(element.alt||element.getAttribute('aria-label')) - } - if ((element.style.color||window.getComputedStyle(element).backgroundColor||window.getComputedStyle(element).color) && (`${element.style.color||window.getComputedStyle(element).backgroundColor||window.getComputedStyle(element).color}`.indexOf( "255, 255, 255") == -1 ) && (`${element.style.color||window.getComputedStyle(element).backgroundColor||window.getComputedStyle(element).color}`.indexOf( "0, 0, 0") == -1 ) ) { - elementDescription += `,颜色:${element.style.color||window.getComputedStyle(element).backgroundColor||window.getComputedStyle(element).color}`; - } - const elementPosition = getElementPosition(element); - elementDescription += `,位于${elementPosition}`; - // if (element.offsetWidth && element.offsetHeight) { - // elementDescription += `,大小为${element.offsetWidth}像素 x ${element.offsetHeight}像素`; - // } - } - } - if(elementDescription&&elementDescription!='') - description.push(elementDescription); -} -return (unique(description)); -} - -function unique (arr) { - return Array.from(new Set(arr)) - } - -// 输出描述文本 - - -/** - * 获取元素相对位置 - */ -function getElementPosition(element) { - const rect = element.getBoundingClientRect(); - const x = rect.left + rect.width / 2; - const y = rect.top + rect.height / 2; - let position = ""; - - if (x < window.innerWidth / 3) { - position += "左"; - } else if (x > window.innerWidth * 2 / 3) { - position += "右"; -} else { -position += "中"; -} - -if (y < window.innerHeight / 3) { -position += "上"; -} else if (y > window.innerHeight * 2 / 3) { -position += "下"; -} else { -position += "中"; -} - -return position; -} - - -//rsa -function stringToArrayBuffer(str){ - if(!str) return; - try{ - var buf = new ArrayBuffer(str.length); - var bufView = new Uint8Array(buf); - for (var i=0, strLen=str.length; i setTimeout(resolve, delay)); -} - -function fetchRetry(url, tries, fetchOptions = {}) { - function onError(err){ - triesLeft = tries - 1; - if(!triesLeft){ - throw err; - } - return wait(500).then(() => fetchRetry(url, triesLeft, fetchOptions)); - } - return fetch(url,fetchOptions).catch(onError); -} -function send_webchat(elem) -{ - if(lock_chat!=0) return; - lock_chat = 1; - knowledge = document.querySelector("#chat").innerHTML.replace(/.*?<\/a.*?>/g, '').replace(/]+>/g,"").replace(/\n\n/g,"\n") - if(knowledge.length>400)knowledge.slice(400) - knowledge += "\n以上是“" + original_search_query + "”的搜索结果\n" - let word = document.querySelector("#chat_input").value; - if(elem){word = elem.textContent;elem.remove(); chatmore();} - if(word.length==0 || word.length > 140) return; - fetchRetry('https://search.kg/search?q='+encodeURIComponent(word)+'&language=zh-CN&time_range=&safesearch=0&categories=general&format=json',3) - .then(response => response.json()) - .then(data => { - prompt = JSON.parse(atob( (/