()
| 1276 | |
| 1277 | // 直达问题按钮 |
| 1278 | function addToQuestion() { |
| 1279 | if (!menu_value('menu_toQuestion')) return |
| 1280 | |
| 1281 | // 一开始加载的信息流 + 添加按钮样式 |
| 1282 | if (location.pathname === '/search') { |
| 1283 | document.lastChild.appendChild(document.createElement('style')).textContent = `a.zhihu_e_toQuestion {font-size: 13px !important;font-weight: normal !important;padding: 1px 6px 0 !important;border-radius: 2px !important;display: inline-block !important;vertical-align: top !important;height: 20.67px !important;line-height: 20.67px !important;margin-top: 2px !important;}`; |
| 1284 | addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)'); |
| 1285 | } else { |
| 1286 | document.lastChild.appendChild(document.createElement('style')).textContent = `a.zhihu_e_toQuestion {font-size: 13px !important;font-weight: normal !important;padding: 1px 6px 0 !important;border-radius: 2px !important;display: inline-block !important;vertical-align: top !important;margin-top: 4px !important;}`; |
| 1287 | document.querySelectorAll('h2.ContentItem-title a:not(.zhihu_e_tips)').forEach(function(item){addTypeTips_(item);}) |
| 1288 | } |
| 1289 | |
| 1290 | // 后续加载的信息流 |
| 1291 | const observer = new MutationObserver(mutationsList => { |
| 1292 | for (const mutation of mutationsList) { |
| 1293 | for (const target of mutation.addedNodes) { |
| 1294 | if (target.nodeType != 1) return |
| 1295 | addTypeTips_(target.querySelector('h2.ContentItem-title a:not(.zhihu_e_tips)')); |
| 1296 | } |
| 1297 | } |
| 1298 | }); |
| 1299 | observer.observe(document, { childList: true, subtree: true }); |
| 1300 | |
| 1301 | window.addEventListener('urlchange', function(){ |
| 1302 | addSetInterval_('h2.ContentItem-title a:not(.zhihu_e_tips)'); |
| 1303 | }) |
| 1304 | |
| 1305 | function addTypeTips_(titleA) { |
| 1306 | if (!titleA) return // 判断是否为真 |
| 1307 | if (titleA.parentElement.querySelector('a.zhihu_e_toQuestion')) return // 判断是否已添加 |
| 1308 | if (titleA.textContent.indexOf('?') != -1) { // 把问题末尾英文问好 [?] 的替换为中文问好 [?],这样按钮与标题之间的间距就刚刚好~ |
| 1309 | titleA.innerHTML = titleA.innerHTML.replace('?', "?") |
| 1310 | } |
| 1311 | if (/answer\/\d+/.test(titleA.href)) { // 如果是指向回答的问题(而非指向纯问题的链接) |
| 1312 | const titleA_meta = titleA.parentElement.parentElement.querySelector('meta[itemprop="url"]'); // 获取该问题页地址 |
| 1313 | if (!titleA_meta) return // 判断元素是否存在(针对的是部分搜索词下搜索页开头的 "最新讨论" 之类的非常规元素) |
| 1314 | titleA.insertAdjacentHTML('afterend', `<a class="zhihu_e_toQuestion VoteButton" href="${titleA_meta.content}" target="_blank">直达问题</a>`); |
| 1315 | } |
| 1316 | } |
| 1317 | |
| 1318 | function addSetInterval_(A) { |
| 1319 | let timer = setInterval(function(){ |
| 1320 | let aTag = document.querySelectorAll(A); |
| 1321 | if (aTag.length > 0) { |
| 1322 | clearInterval(timer); |
| 1323 | aTag.forEach(function(item){addTypeTips_(item);}) |
| 1324 | } |
| 1325 | }); |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | |
| 1330 | // 展开问题描述 |
no test coverage detected