function 添加样式(css,id='') {//植入css
const style = document.createElement('style');
style.type='text/css';
id!=''?style.id = id:0;
style.textContent = css;
const h0 =document.querySelector('html');
h0.insertBefore(style,h0.children[0]);
}
function seconds2date(seconds){//秒数转日期//getDateStr(1524637692)>"2018-4-25 14:28:12"
var date = new Date(seconds*1000)
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var currentTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
return currentTime
}
/*HTMLElement.prototype.appendChilds=function(){//DOM对象的appendChilds方法:实现批量添加nodes
const args = arguments;
let documentFragment = document.createDocumentFragment();
for(let i=0;i<args.length;i++){
documentFragment.appendChild(args[i]);
}
this.appendChild(documentFragment);
}*/
//document.body.appendChilds(document.createElement('fuck'),document.createElement('suck'),document.createElement('crap'))
//刚发现一坑:不能用这种方法调用这个函数,除非在油猴里使用。
function 追加子元素(父亲,标签,内容) {//追加子元素(document.body,'标签','内容','idk','true','data-s')
const 新元素 = document.createElement(标签)
新元素.appendChild(document.createTextNode(内容));
const 长 = arguments.length;
if (长>3) {
for(let i=3;i<长;i+=2){
新元素.setAttribute([arguments[i]],arguments[i+1])
}
}
父亲.appendChild(新元素);
return 新元素;
}
function 追加兄弟元素(哥哥,标签,内容){//紧跟其后追加元素//追加兄弟元素(document.body,'div','','suck','1','dick')
const 弟弟 = document.createElement(标签);
弟弟.appendChild(document.createTextNode(内容));
const 长 = arguments.length;
if (长>3) {
for(let i=3;i<长;i+=2){
弟弟.setAttribute([arguments[i]],arguments[i+1])
}
}
return 哥哥.parentNode.insertBefore(弟弟,哥哥.nextElementSibling);
}
function fuckTiebaLogin(userName,password){//跳出登录(不可用)框后再执行
document.getElementById('TANGRAM__PSP_5__footerULoginBtn').click();
document.getElementById('TANGRAM__PSP_5__userName').outerHTML+='';
document.getElementById('TANGRAM__PSP_5__password').outerHTML+='';
document.getElementById('TANGRAM__PSP_5__userName').value=userName;
document.getElementById('TANGRAM__PSP_5__password').value=password;
return document.getElementById('TANGRAM__PSP_5__submit').click();
}
function xb_getValue(k, aDefault) {
let val = localStorage.getItem('_GM_STORAGE_PREFIX_' + k)
if (null === val && 'undefined' != typeof aDefault) return aDefault;
return val;
}
function xb_setValue(k, v) {
localStorage.setItem('_GM_STORAGE_PREFIX_' + k, v);
}//xb_setValue('k', 'v')
function xb_deleteValue(k) {
localStorage.removeItem('_GM_STORAGE_PREFIX_' + k);
}
function needGoogleTranslate(){
添加样式(`
#google_translate_element {
right: 0px;
transform: translateX(85%);
}
.recoverPage {
right: 0px;
transform: translateX(73%);
}
@media handheld, only screen and (max-width: 768px) {
.recoverPage {
transform: translateX(0);
}
}
`)
// 自定义样式,隐藏顶部栏
添加样式(`
html,body{
top: 0!important;
}
#google_translate_element {
position: fixed;
bottom: 30px;
height: 21px;
border-radius: 11px;
z-index: 10000000;
overflow: hidden;
box-shadow: 1px 1px 3px 0 #888;
opacity: .5;
transition: all .3s;
}
#google_translate_element .goog-te-gadget-simple {
border: 0;
}
#google_translate_element .goog-te-gadget-simple span {
margin-right: 0;
border-radius: 11px;
}
#lb {
display: inline-block;
}
.recoverPage {
width: 4em;
background-color: #fff;
position: fixed;
z-index: 10000000;
bottom: 60px;
user-select: none;
text-align: center;
font-size: small;
line-height: 2em;
border-radius: 1em;
box-shadow: 1px 1px 3px 0 #888;
opacity: .5;
transition: all .3s;
}
#google_translate_element:hover, .recoverPage:hover {
opacity: 1;
transform: translateX(0);
}
.recoverPage:active {
box-shadow: 1px 1px 3px 0 #888 inset;
}
#google_translate_element .goog-te-gadget-simple {
width: 100%;
}
.goog-te-banner-frame.skiptranslate {
display: none
}
@media handheld, only screen and (max-width: 768px) {
#google_translate_element {
width: 104px;
}
#google_translate_element .goog-te-combo {
margin: 0;
padding-top: 2px;
border: none;
}
#goog-gt-tt {
visibility: hidden!important;
display: none!important;
}
.goog-text-highlight {
background-color: inherit!important;
box-shadow: 0 0 0 0 transparent!important;
}
.recoverPage {
width: 1.5em;
line-height: 1.5em;
}
}
`)
// 创建容器
追加子元素(document.body, 'div', '','id','google_translate_element')
// 初始化
追加子元素(document.head,'script',`
function googleTranslateElementInit() {
let google_translate_element = document.getElementById('google_translate_element')
let timer = setInterval(function () {
let google_translate_element = document.getElementById('google_translate_element')
if (google_translate_element) {
clearInterval(timer)
new google.translate.TranslateElement(
{
pageLanguage: 'auto',
includedLanguages: 'zh-CN,en,ja',
layout: 2,
},
'google_translate_element'
)
// 清除图片的请求,加快访问速度
let img = [].slice.call(document.querySelectorAll('#goog-gt-tt img,#google_translate_element img'));
img.forEach(function(v) {
const a = v
a.src = ''
let b = a.outerHTML.replace(/<img(.*?)>/, () => {
return '<span id="lb"' + RegExp.$1 +'></span>'
})
const c = document.createElement('div')
c.innerHTML = b
a.parentNode.insertBefore(c.children[0], a.parentNode.children[0])
a.remove()
});
const recoverPage = document.createElement('div')
recoverPage.setAttribute('class', 'notranslate recoverPage')
recoverPage.innerText = 'BACK'
document.body.appendChild(recoverPage)
// 点击恢复原网页
recoverPage.onclick = () => {
const PCRecoverIframe = document.getElementById(':2.container') // PC端
if (PCRecoverIframe) {
PCRecoverIframe.contentWindow.document.getElementById(':2.restore').click()
}
}
}
}, 300)
}
`
)
追加子元素(document.head,
'script','',
'src','//translate.google.cn/translate_a/element.js?&cb=googleTranslateElementInit'
)
// 追加子元素('//cdn.jsdelivr.net/gh/lindongbin/gt/element.js','script','src', head)
// 排除一些代码的翻译
const noTranslateArray = ['pre','code']//
noTranslateArray.forEach(selectorName => {
;[...document.querySelectorAll(selectorName)].forEach(node => {
if (node.className.indexOf('notranslate') === -1) {
node.classList.add('notranslate')
}
})
})
}