1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
| const summaryData = new DataProxy('', []) const config = { summaryElConfig: { appendBeforeEl: '#post #article-container', buttons: [{ text: '重新生成', bgColor: 'rgba(12,107,166,0.7)', onClick: async () => { const postContent = getPostContent(config.postContentConfig) if (config.sparkConfig.stream) { getSummaryFromSparkStream(postContent, config.sparkConfig, summaryData) } else { const summary = await getSummaryFromSpark(postContent, config.sparkConfig) summaryData.setValue(summary) } } }, { text: '介绍自己', bgColor: 'rgba(40,167,69,0.7)', onClick: () => { aiIntroduce(config.sparkConfig, summaryData) } }, ], }, postContentConfig: { titleEl: 'h1', headEl: 'h1,h2,h3,h4,h5,h6', contentEl: '#post #article-container', }, sparkConfig: {
apiUrl: 'https://spark-api.xxx.xxx', apiPassword: 'xxxxxxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxxxxxx', model: 'lite', stream: true, }, urlValidList: [ 'posts', ], urlBlacklist: [ 'e8598403', 'a15ff462', 'b4c68f75', '9e7fad42', 'd7252e66', '17241f42', 'e8b927d9', ], } init(config).then((flag) => { if (flag) { console.log(`%cSpark-Lite-Post-AI InsectMk摘要工具测试版:%chttps://insectmk.cn%c`, `border:1px solid transparent;background:linear-gradient(45deg, #ff7e5f, #feb47b);border-right:0;border-radius:5px 0 0 5px;padding: 5px 10px;color:white;font-weight:bold;margin:10px 0;`, `border:1px solid transparent;background:linear-gradient(45deg, #fbc2eb, #a6c1ee);border-left:0;border-radius:0 5px 5px 0;padding: 5px 10px;color:white;font-weight:bold;`, ''); } })
async function init(config) { const href = window.location.href if (!config.urlValidList.some(item => href.includes(item)) || config.urlBlacklist.some(item => href.includes(item))) { return false } const summaryEl = createSummaryEl(config.summaryElConfig) const postContent = getPostContent(config.postContentConfig) summaryData.addEl(summaryEl) if (config.sparkConfig.stream) { getSummaryFromSparkStream(postContent, config.sparkConfig, summaryData) } else { const summary = await getSummaryFromSpark(postContent, config.sparkConfig) summaryData.setValue(summary) } return true }
function aiIntroduce(sparkConfig, summaryData) { chatWithSparkStream(sparkConfig.apiUrl, getSparkFetchOptions(sparkConfig, '你是一个智能摘要生成工具,专注于从给定的文章内容中提取并总结关键信息。你的任务是简洁明了地介绍自己的功能和目的,不要换行,不要超过200字,不需要提出建议和缺少的东西,请用中文回答。'), summaryData) }
function getSparkFetchOptions(sparkConfig, content) { return { method: 'POST', headers: { 'Authorization': `Bearer ${sparkConfig.apiPassword}`, 'Content-Type': 'application/json' }, body: JSON.stringify({ model: sparkConfig.model, messages: [ { role: 'user', content: content } ], stream: sparkConfig.stream }) } }
function getSparkSummaryFetchOptions(postContent, sparkConfig) { return getSparkFetchOptions(sparkConfig, `你是一个摘要生成工具,你需要解释我发送给你的内容,不要换行,不要超过200字,只需要介绍文章的内容,不需要提出建议和缺少的东西,请用中文回答。文章标题为:${postContent.title},小结标题为${postContent.heads.join('\t')}内容为:${postContent.content}`) }
function getSummaryFromSparkStream(postContent, sparkConfig, summaryData) { chatWithSparkStream(sparkConfig.apiUrl, getSparkSummaryFetchOptions(postContent, sparkConfig), summaryData) }
function chatWithSparkStream(apiUrl, fetchOptions, summaryData) { fetch(apiUrl, fetchOptions).then(response => { if (!response.ok) throw new Error('讯飞响应错误!') const reader = response.body.getReader() const decoder = new TextDecoder() let summary = '' function readStream() { reader.read().then(({ done, value }) => { if (done) return let _value = value ? decoder.decode(value, { stream: true }) : '' let _data = _value?.split('\n') || [] for (const it of _data) { let _it = it.replace(/^(data: \[DONE\])|(data:)/, '') if (_it) { summary += JSON.parse(_it).choices[0].delta.content summaryData.setValue(summary) } } readStream() }).catch(err => { console.error('读取流失败:', err) }) } readStream() }).catch(err => { console.error('请求失败:', err) }) }
async function getSummaryFromSpark(postContent, sparkConfig) { try { const response = await fetch(sparkConfig.apiUrl, getSparkSummaryFetchOptions(postContent, sparkConfig)) const data = await response.json() return data.choices[0].message.content } catch (error) { throw error } }
function getPostContent(postContentConfig) { const title = postContentConfig.titleEl ? document.querySelector(postContentConfig.titleEl).textContent : document.title const headEls = document.querySelectorAll(postContentConfig.headEl ? postContentConfig.headEl : 'h1,h2,h3,h4,h5,h6') const heads = Array.from(headEls).map(heading => heading.textContent) const content = document.querySelector(postContentConfig.contentEl ? postContentConfig.contentEl : '#post #article-container').textContent return { title, heads, content, } }
function createSummaryEl(summaryElConfig) { let targetEl = document.querySelector(summaryElConfig.appendBeforeEl || '#post #article-container') let summaryEl = document.createElement('div') summaryEl.style.border = '1px solid #ccc' summaryEl.style.padding = '5px 16px 0 16px' summaryEl.style.marginBottom = '8px' summaryEl.style.borderRadius = '8px' summaryEl.style.backgroundColor = 'rgba(247, 247, 249, 0.5)' summaryEl.style.boxShadow = '0 2px 4px rgba(0, 0, 0, 0.1)' summaryEl.style.fontFamily = 'Arial, sans-serif' let titleEl = document.createElement('span') titleEl.innerText = '文章摘要' titleEl.style.fontSize = '13px' titleEl.style.fontWeight = '600' titleEl.style.marginBottom = '12px' titleEl.style.color = '#333' summaryEl.appendChild(titleEl) let contentEl = document.createElement('p') contentEl.style.minHeight = '80px' contentEl.style.margin = '5px 0' contentEl.style.padding = '5px' contentEl.style.fontSize = '14px' summaryEl.style.border = '1px solid #ccc' contentEl.style.backgroundColor = 'rgba(255,255,255,0.5)' contentEl.style.borderRadius = '8px' contentEl.style.color = '#666' contentEl.style.lineHeight = '1.6' summaryEl.appendChild(contentEl) if (Array.isArray(summaryElConfig.buttons) && summaryElConfig.buttons.length > 0) { let buttonContainer = document.createElement('div') summaryElConfig.buttons.forEach(buttonConfig => { let buttonEl = document.createElement('button') buttonEl.innerText = buttonConfig.text || '按钮' buttonEl.style.padding = '5px' buttonEl.style.margin = '0 5px 5px 0' buttonEl.style.fontSize = '12px' buttonEl.style.color = '#fff' buttonEl.style.backgroundColor = buttonConfig.bgColor || 'rgba(0,123,255,0.7)' buttonEl.style.border = 'none' buttonEl.style.borderRadius = '4px' buttonEl.style.cursor = 'pointer'; buttonEl.addEventListener('click', buttonConfig.onClick) buttonContainer.appendChild(buttonEl) }); summaryEl.appendChild(buttonContainer) } targetEl.insertBefore(summaryEl, targetEl.firstChild); return contentEl }
function DataProxy(value, els) { this.value = value ? value : [] this.els = els ? els : [] this.setValue = function(value) { this.value = value this.els.forEach(el => {el.innerText = this.value}) } this.getValue = function() { return this.value } this.addEl = function(el) { this.els.push(el) el.innerText = this.value } this.removeEl = function(el) { this.els.splice(this.els.indexOf(el), 1) } for (let el of this.els) { el.innerText = this.value } }
|