Last active
December 26, 2021 14:26
-
-
Save Misaka-0x447f/109b0bf3034c4b052eb90b8efdffd9cf to your computer and use it in GitHub Desktop.
自动扫描中国电信精品网 ip
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name 自动扫描中国电信精品网 | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description 自动扫描中国电信精品网 ip (58.32.xx.xx) | |
// @author misaka.org | |
// @match http://10.0.0.1/cgi-bin/luci/admin/network/network | |
// @icon https://www.google.com/s2/favicons?domain=0.1 | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
'use strict'; | |
const htmlStringToNode = (html) => { | |
var frag = document.createDocumentFragment(), | |
tmp = document.createElement('body'), child; | |
tmp.innerHTML = html; | |
// Append elements in a loop to a DocumentFragment, so that the browser does | |
// not re-render the document for each node | |
while (child = tmp.firstChild) { | |
frag.appendChild(child); | |
} | |
return frag; // Now, append all elements at once | |
frag = tmp = null; | |
} | |
const installCSS = (cssSrc) => { | |
document.head.appendChild( | |
htmlStringToNode(`<link rel="stylesheet" type="text/css" href="${cssSrc}" media="screen" />`) | |
) | |
} | |
const appendCSS = (cssText) => { | |
const styleSheet = document.createElement("style") | |
styleSheet.innerText = cssText | |
document.head.appendChild(styleSheet) | |
} | |
const assertOnlyOneSelector = (parent, selector) => { | |
const res = parent.querySelectorAll(selector) | |
if (res.length === 1) return res[0] | |
const msg = `${selector} 选中的元素数量不是 1` | |
alert(msg) | |
throw new Error(msg) | |
} | |
let enable = true | |
let match = false | |
const ipIsOptimized = (ip) => { | |
if (!ip?.match(/^58\.32\.4\d*\.\d+/) && ip?.match(/^58\.32\.\d+\.\d+/)) { | |
return true | |
} | |
return false | |
} | |
const worker = (param) => { | |
const next = (time) => setTimeout(() => worker(param), time) | |
const { getCurrentIp, action } = param | |
if (!enable || !getCurrentIp()) { | |
next(1000) | |
return | |
} | |
if (ipIsOptimized(getCurrentIp())) { | |
next(10000) | |
return | |
} | |
if (getCurrentIp()) { | |
action().then(() => next(10000)) | |
} | |
} | |
const register = (actionFunction) => { | |
document.body.querySelector('#cbi-network').insertBefore(htmlStringToNode(` | |
<h2 id="autoSearchPanel" name="content" style="width: 100%; display: flex; align-items: center; justify-content: space-between; padding: 8px 24px;"> | |
<div style="display: flex"> | |
<span>当前 ip 地址:</span> | |
<span class="current-ip">...</span> | |
<span class="refresh-indicator"> (尚未最佳化)</span> | |
</div> | |
</div> | |
</div> | |
<div> | |
<input type="button" class="cbi-button cbi-button-edit start" style="width:100px;" title="开始监视" value="开始监视" size="0"/> | |
<input type="button" class="cbi-button cbi-button-remove stop" style="width:100px;" title="停止监视" value="停止监视" size="0"/> | |
</div> | |
</h2> | |
`), document.body.querySelector('#cbi-network .cbi-map')) | |
document.querySelector('#autoSearchPanel .cbi-button').onclick = () => { | |
enable = !enable | |
} | |
const pppoeDesc = assertOnlyOneSelector(document, '#PPPOE-ifc-description') | |
const pppoeReconnectButton = assertOnlyOneSelector(pppoeDesc.nextElementSibling, '.cbi-button-reload') | |
const container = assertOnlyOneSelector(document, '#autoSearchPanel') | |
const getCurrentIp = () => Array.from(pppoeDesc.children)?.find(el => el.innerText.match(/IPv4/))?.nextSibling?.nodeValue.trim() | |
const refresh = () => { | |
if (enable) { | |
container.querySelector('.start').style.display = "none" | |
container.querySelector('.stop').style.display = "block" | |
} else { | |
container.querySelector('.start').style.display = "block" | |
container.querySelector('.stop').style.display = "none" | |
} | |
if (match) { | |
container.querySelector('.refresh-indicator').style.display = "none" | |
} else { | |
container.querySelector('.refresh-indicator').style.display = "block" | |
} | |
container.querySelector('.current-ip').innerText = getCurrentIp() || '...' | |
match = ipIsOptimized(getCurrentIp()) | |
} | |
container.querySelector('.start').onclick = () => { | |
enable = true | |
refresh() | |
} | |
container.querySelector('.stop').onclick = () => { | |
enable = false | |
refresh() | |
} | |
setInterval(refresh, 333) | |
worker({ | |
getCurrentIp, | |
action: () => new Promise(resolve => { | |
pppoeReconnectButton.onclick() | |
const timer = setInterval(() => { | |
if (getCurrentIp()) { | |
resolve() | |
clearInterval(timer) | |
} | |
}, 1000) | |
}) | |
}) | |
} | |
register() | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment