// ==UserScript== // @name 知乎-匿名提问者标注 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 在问题页, 标注匿名提问, 防止钓鱼 // @author C4r // @match https://www.zhihu.com/* // @require https://cdn.jsdelivr.net/npm/jquery@3.5.0/dist/jquery.min.js // @grant none // @downloadURL none // ==/UserScript== (function() { 'use strict'; function isHome(){ return $("#TopstoryContent").length > 0 } function isQuestionPage(){ return $('.QuestionPage').length > 0 } function getLogURL(questionURL){ // return new URL('log', questionURL).href return questionURL + '/log' } function httpGetAsync(theUrl, callback) { var xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) callback(xmlHttp.responseText); } xmlHttp.open("GET", theUrl, true); // true for asynchronous xmlHttp.send(null); } function getAuthorUrl(logURL){ return new Promise((resolve, reject) => { httpGetAsync(logURL, (responseText) => { // console.log('get response') let repHTML = $.parseHTML(responseText) let author = $(repHTML).find('.zm-item:last > div > a').attr('href') if(author != undefined){ let userInfo = { 'name': $(repHTML).find('.zm-item:last > div > a').text(), 'url': $(repHTML).find('.zm-item:last > div > a').get(0).href, 'a': $(repHTML).find('.zm-item:last > div > a').get(0).outerHTML } resolve(userInfo) }else{ // console.log( '匿名提问 : ', undefined) resolve(undefined) } }) }) } function topic(){ return '\
\ \ \
\
\
\
\
\ ' } function noteQuestionPage(content, jump){ return '\
\
\
\
\
\ \
\
\
\
\
' } function addNoteQuestionPage(content, jump){ if($('[AnonymousNote]').length > 0){ $('[AnonymousNote] .PositiveLabelBar-title' ).empty() $('[AnonymousNote] .PositiveLabelBar-title' ).append(content) $('[AnonymousNote] .PositiveLabelBar-side' ).empty() $('[AnonymousNote] .PositiveLabelBar-side' ).append(jump) }else{ console.log('插入') $('.QuestionHeader h1.QuestionHeader-title').after( noteQuestionPage(content, jump) ); } } $(document).ready(()=>{ if(isHome()){ // console.log("主页") }else if(isQuestionPage()){ let questionURL = $('.QuestionPage >meta[itemprop="url"]').attr('content') let logURL = getLogURL(questionURL) // console.log('问题页 ', logURL) addNoteQuestionPage('读取日志中...', '问题日志') getAuthorUrl(logURL).then(authorInfo =>{ if(authorInfo == undefined){ addNoteQuestionPage('⚠ 注意 : 这是一篇匿名提问', '问题日志') let oText = $('.PageHeader h1.QuestionHeader-title').text() $('.PageHeader h1.QuestionHeader-title').text( '[⚠ 匿名]' + oText ) }else{ // console.log('找到题主 : ', authorInfo) addNoteQuestionPage('题主 : ' + authorInfo.a, '问题日志') let oText = $('.PageHeader h1.QuestionHeader-title').text() $('.PageHeader h1.QuestionHeader-title').text('[🗹]' + oText ) } }) }else{ // console.log('unknown Page') } }) })();