装好后 点我测试 http://www.baidu.com
<script>
class NewTarget {
cookieName = 'new_target';
isOpen = Boolean(this.getCookie(this.cookieName));
init() {
if (location.pathname.includes('link.url.')) {
const a = document.querySelector('.content a');
const {href} = a;
a.href = "javascript:void(0);";
a.onclick = () => {
this.open(href);
};
const checkBox = document.createElement('input');
checkBox.type = 'checkbox';
if (this.isOpen) {
checkBox.checked = true;
} else {
delete checkBox.checked;
}
checkBox.onchange = ({target: {checked}}) => {
this.setCookie(this.cookieName, checked);
this.isOpen = checked;
}
const div = document.createElement('div');
div.innerText = '是否新窗口打开:'
div.appendChild(checkBox)
a.parentNode.appendChild(div)
}
}
open(href) {
window.open(href, this.isOpen ? "_blank" : "_self");
if (this.isOpen) {
history.go(-1);
}
}
/**
* 设置cookie
*/
setCookie(name, value) {
const Days = 30;
const exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
/**
* 读取cookie
*/
getCookie(name) {
let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) return unescape(arr[2]);
else return null;
}
}
document.addEventListener('DOMContentLoaded', () => {
var nt = new NewTarget();
nt.init();
})
</script>
小米MIX2s(白)
@老虎会游泳 反馈bug:
代码片段中的 @xxx 被翻译
小米MIX2s(白)