Skip to content

Instantly share code, notes, and snippets.

@gjz010
Created June 2, 2022 08:12
Show Gist options
  • Save gjz010/fa25a7e7e6e85a3dbaea4ea7baf8df25 to your computer and use it in GitHub Desktop.
Save gjz010/fa25a7e7e6e85a3dbaea4ea7baf8df25 to your computer and use it in GitHub Desktop.
知乎防火星
// ==UserScript==
// @name 知乎防火星
// @version 1
// @grant none
// @include /^https?://www\.zhihu\.com/.*$/
// ==/UserScript==
const WARNING_HTML = (x)=>`<div class="QuestionStatus"><div class="QuestionStatus-bar"><div class="QuestionStatus-bar-inner" style="background-color:red;"><strong>这个问题是${x}前提问的!</strong><em>提问内容可能已过时或不适用于当下</em><div class="ztext" style="color:white;">转发时请注意自己别火星了</div></div></div><div><span style="position: absolute; top: -10000px; left: -10000px;" role="log" aria-live="assertive"></span></div><div><span style="position: absolute; top: -10000px; left: -10000px;" role="log" aria-live="assertive"></span></div></div>`
function createWarningTag(tag){
var div = document.createElement('div');
div.innerHTML = WARNING_HTML(tag).trim();
// Change this to div.childNodes to support multiple top-level nodes.
return div.firstChild;
}
function smartDate(x){
x=Math.round(x);
let unit = 1000*60*60*24*30*12;
let s = "";
s+=(x>=unit? `${Math.floor(x/unit)}年`: "");
x=x%unit; unit/=12;
s+=(x>=unit? `${Math.floor(x/unit)}个月`: "");
x=x%unit; unit/=30;
s+=(x>=unit? `${Math.floor(x/unit)}天`: "");
x=x%unit; unit/=24;
s+=(x>=unit? `${Math.floor(x/unit)}小时`: "");
x=x%unit; unit/=60;
return s;
}
const f = (()=>{
const createTime = document.querySelector("meta[itemprop='dateCreated']");
if(!createTime) return;
const delta = (new Date() - new Date(createTime.content));
if(delta>=30*24*60*60*1000){
document.querySelector('div[data-za-detail-view-path-module="QuestionDescription"]').parentNode.prepend(createWarningTag(`${smartDate(delta)}`))
}
})
window.addEventListener('load', f, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment