SetUserName

set username

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         SetUserName
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  set username
// @author       Minilong
// @match        https://*.tswjs.org/log/view/*
// @exclude      https://*.tswjs.org/log/view/xxx
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    app();
})();

// 创建元素插入
function app() {
    const root = document.createElement('div');
    setElement(root);
    root.innerHTML = `
    <a>修改测试号码</a>
  `
  const viewTitle = document.querySelector('.view-title');
    viewTitle.insertBefore(root, viewTitle.firstChild);
}

// 设置元素
function setElement(element) {
    let userName, url;
    Object.assign(element.style, getRootStyle());
    element.setAttribute('class', 'button r');
    element.addEventListener("click", function(){
        userName = saveUserName();
        if(!userName) return;
        url = getTargetURL(userName);
        redirectToMyLog(url);
    })
}

// 获取根元素样式
function getRootStyle() {
    return {
        cursor: 'pointer',
    }
}

// 设置用户名
function saveUserName() {
    const userName = prompt('输入测试号码', '');
    if(!userName) return;
    localStorage.setItem("tUserName",userName);
    return userName;
}

// 获取目标url
function getTargetURL(username) {
    const curURL = `${window.location.origin}/log/view/${username}`
  return curURL
}

// 重定向到目标url
function redirectToMyLog(url) {
    window.location.replace(url)
}