- 积分
- 912
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2019-11-18
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
本帖最后由 沐雨淋觞 于 2025-10-23 10:50 编辑
(额,点错区了,已经发了)
该脚本仅适用于Swift3.0实况统计-统计报表。
主要内容为:1.删除表格明细的站号列。2.自设添加水文站的镇街(方便服务)。3.删除要素分级。4.删除极大风中的镇街最大风力表。
代码添加到油猴插件后,会在右上角出现设置的按钮
初次使用需要自行勾选功能内容
其中设置对应的功能为:
要添加水文站的镇街需要自行在代码中添加。格式为const shuiwen = {"站点":"对应镇街",}
完整油猴代码如下
- // ==UserScript==
- // [url=home.php?mod=space&uid=93431]@name[/url] swift3.0删除
- // @namespace http://tampermonkey.net/
- // @version 0.1
- // @description try to take over the world!
- // @author 沐雨淋觞
- // @match http://10.148.16.72:83/swift/index.html
- // @match http://10.148.8.131/swift/*
- // @icon https://www.google.com/s2/favicons?domain=16.72
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- const SettingData = [false,false,false,false]
- //-----------------------设置按钮-------------------//
- // 立即创建设置按钮,不等待DOMContentLoaded
- createSettingsButton();
- function loadSettings() {
- // 删除站号
- const Mode1 = localStorage.getItem('tm_Mode1') === 'true';
- document.getElementById('tm-mode-1').checked = Mode1;
- applyMode(Mode1,0);
- // 添加水文镇街
- const Mode2 = localStorage.getItem('tm_Mode2') === 'true';
- document.getElementById('tm-mode-2').checked = Mode2;
- applyMode(Mode2,1);
- // 删除要素分级
- const Mode3 = localStorage.getItem('tm_Mode3') === 'true';
- document.getElementById('tm-mode-3').checked = Mode3;
- applyMode(Mode3,2);
- // 删除各县最大风力表
- const Mode4 = localStorage.getItem('tm_Mode4') === 'true';
- document.getElementById('tm-mode-4').checked = Mode4;
- applyMode(Mode4,3);
- }
- // 设置启用和关闭
- function applyMode(enabled,num) {
- if (enabled) {
- SettingData[num] = enabled;
- } else {
- SettingData[num] = enabled;
- }
- }
- // 设置按钮设计
- function createSettingsButton() {
- console.log('创建设置按钮'); // 调试信息
- // 创建样式
- const style = document.createElement('style');
- style.textContent = `
- .tm-settings-btn {
- position: fixed;
- top: 0px;
- right: 100px;
- width: 50px;
- height: 50px;
- background: #2196F3;
- color: white;
- border: none;
- border-radius: 50%;
- cursor: pointer;
- font-size: 20px;
- box-shadow: 0 4px 12px rgba(0,0,0,0.3);
- z-index: 10000;
- transition: all 0.3s ease;
- }
- .tm-settings-btn:hover {
- transform: scale(1.1);
- background: #1976D2;
- }
- .tm-settings-panel {
- position: fixed;
- top: 50%;
- left: 50%;
- transform: translate(-50%, -50%);
- width: 350px;
- background: white;
- border-radius: 12px;
- box-shadow: 0 10px 30px rgba(0,0,0,0.3);
- z-index: 10002;
- display: none;
- font-family: Arial, sans-serif;
- }
- .tm-settings-panel.active {
- display: block;
- }
- .tm-settings-header {
- display: flex;
- justify-content: space-between;
- align-items: center;
- padding: 15px;
- border-bottom: 1px solid #eee;
- background: #f5f5f5;
- border-radius: 12px 12px 0 0;
- }
- .tm-settings-content {
- padding: 15px;
- }
- .tm-setting-item {
- display: flex;
- justify-content: space-between;
- align-items: center;
- margin-bottom: 15px;
- padding: 8px;
- }
- .tm-switch {
- position: relative;
- display: inline-block;
- width: 50px;
- height: 24px;
- }
- .tm-switch input {
- opacity: 0;
- width: 0;
- height: 0;
- }
- .tm-slider {
- position: absolute;
- cursor: pointer;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background-color: #ccc;
- transition: .4s;
- border-radius: 24px;
- }
- .tm-slider:before {
- position: absolute;
- content: "";
- height: 16px;
- width: 16px;
- left: 4px;
- bottom: 4px;
- background-color: white;
- transition: .4s;
- border-radius: 50%;
- }
- input:checked + .tm-slider {
- background-color: #2196F3;
- }
- input:checked + .tm-slider:before {
- transform: translateX(26px);
- }
- .tm-overlay {
- position: fixed;
- top: 0;
- left: 0;
- width: 100%;
- height: 100%;
- background: rgba(0,0,0,0.5);
- z-index: 10001;
- display: none;
- }
- .tm-overlay.active {
- display: block;
- }
- `;
- document.head.appendChild(style);
- // 创建设置按钮
- const settingsBtn = document.createElement('button');
- settingsBtn.className = 'tm-settings-btn';
- settingsBtn.innerHTML = '⚙';
- settingsBtn.title = '设置';
- settingsBtn.id = 'tm-settings-btn';
- document.body.appendChild(settingsBtn);
- // 添加点击事件
- settingsBtn.addEventListener('click', function() {
- console.log('设置按钮被点击'); // 调试信息
- createSettingsPanel();
- });
- console.log('设置按钮创建完成'); // 调试信息
- }
- // 设置页面的功能及设计
- function createSettingsPanel() {
- console.log('创建设置面板'); // 调试信息
- // 如果面板已存在,先移除
- const existingPanel = document.getElementById('tm-settings-panel');
- const existingOverlay = document.getElementById('tm-overlay');
- if (existingPanel) existingPanel.remove();
- if (existingOverlay) existingOverlay.remove();
- // 创建设置面板
- const settingsPanel = document.createElement('div');
- settingsPanel.id = 'tm-settings-panel';
- settingsPanel.className = 'tm-settings-panel';
- settingsPanel.innerHTML = `
- <div class="tm-settings-header">
- <h3 style="margin: 0; font-size: 16px;">网页设置</h3>
- <button id="tm-close-settings" style="background: none; border: none; font-size: 20px; cursor: pointer; padding: 0; width: 30px; height: 30px;">×</button>
- </div>
- <div class="tm-settings-content">
- <div class="tm-setting-item">
- <span style="font-size: 14px;">删除站号</span>
- <label class="tm-switch">
- <input type="checkbox" id="tm-mode-1">
- <span class="tm-slider"></span>
- </label>
- </div>
- <div class="tm-setting-item">
- <span style="font-size: 14px;">添加水文镇街</span>
- <label class="tm-switch">
- <input type="checkbox" id="tm-mode-2">
- <span class="tm-slider"></span>
- </label>
- </div>
- <div class="tm-setting-item">
- <span style="font-size: 14px;">删除要素分级</span>
- <label class="tm-switch">
- <input type="checkbox" id="tm-mode-3">
- <span class="tm-slider"></span>
- </label>
- </div>
- <div class="tm-setting-item">
- <span style="font-size: 14px;">删除各县最大风力表</span>
- <label class="tm-switch">
- <input type="checkbox" id="tm-mode-4">
- <span class="tm-slider"></span>
- </label>
- </div>
- </div>
- `;
- // 创建遮罩层
- const overlay = document.createElement('div');
- overlay.id = 'tm-overlay';
- overlay.className = 'tm-overlay';
- document.body.appendChild(settingsPanel);
- document.body.appendChild(overlay);
- // 显示面板和遮罩
- settingsPanel.classList.add('active');
- overlay.classList.add('active');
- // 添加事件监听
- const closeSettings = document.getElementById('tm-close-settings');
- const overlayEl = document.getElementById('tm-overlay');
- closeSettings.addEventListener('click', closeSettingsPanel);
- overlayEl.addEventListener('click', closeSettingsPanel);
- // 加载设置状态
- loadSettings();
- // 添加设置变化监听
- document.getElementById('tm-mode-1').addEventListener('change', function() {
- localStorage.setItem('tm_Mode1', this.checked);
- applyMode(this.checked,0);
- });
- document.getElementById('tm-mode-2').addEventListener('change', function() {
- localStorage.setItem('tm_Mode2', this.checked);
- applyMode(this.checked,1);
- });
- document.getElementById('tm-mode-3').addEventListener('change', function() {
- localStorage.setItem('tm_Mode3', this.checked);
- applyMode(this.checked,2);
- });
- document.getElementById('tm-mode-4').addEventListener('change', function() {
- localStorage.setItem('tm_Mode4', this.checked);
- applyMode(this.checked,3);
- });
- function closeSettingsPanel() {
- settingsPanel.classList.remove('active');
- overlay.classList.remove('active');
- // 1秒后移除元素
- setTimeout(() => {
- settingsPanel.remove();
- overlay.remove();
- }, 300);
- }
- }
- //-----------------------统计处理-------------------//
- // 存储已处理的iframe,避免重复处理
- const processedIframes = new WeakSet();
- // 主要删除函数
- function deleteSecondH2(element) {
- const container = element || document;
- const shuiwen = {'站点': '对应镇街',}
- const collectDiv = container.querySelector('div.collect');
- const statistDiv = container.querySelector('div#rainStatisticDetail');
- const regexes = [/雨量站[^0-9]*(\d+)/ , /观测站[^0-9]*(\d+)/];
- // 站点总数
- let station = null;
- // 处理表格站号
- const raintable = container.querySelector('#rainStatisticDetail table#rainStatisticTable');
- if (raintable){
- const thead = raintable.querySelector('thead tr');
- const header = thead.querySelectorAll('td');
- const tbody = raintable.querySelector('tbody');
- const rows = tbody.querySelectorAll('tr');
- // 必须添加列数判定,不然全删了......
- if (header[1].textContent.trim() === "站号" ){
- // 判定删除站号列的表头
- if (SettingData[0]){header[1].remove();}
- rows.forEach((row) => {
- const tdcell = row.querySelectorAll('td');
- // 删除站号功能
- if (SettingData[0]){tdcell[1].remove();}
- // 添加水文镇街
- if (SettingData[1] && tdcell[2].textContent in shuiwen) {
- tdcell[2].textContent = `${shuiwen[tdcell[2].textContent]} | ${tdcell[2].textContent}`;
- }
- });
- }
- }
- // 针对雨量、极值温度等级
- // 删除分级
- if (SettingData[2]){
- if (collectDiv){
- const section = collectDiv.querySelector('div.section');
- for (const regex of regexes) {
- const match = section.textContent.match(regex);
- if (match) {
- station = match[1]; // 赋值给外部变量
- break; // 找到就停止
- }
- }
- console.log(station);
- // 获取所有h2标签
- const h2Elements = collectDiv.querySelectorAll('h2');
- // 如果存在至少2个h2标签,则删除第二个
- if (h2Elements.length >= 2) {
- //console.log('找到第二个h2标签,正在删除...', h2Elements[1]);
- h2Elements[1].remove();
- const fenjitable = collectDiv.querySelectorAll('table');
- fenjitable[0].remove();
- collectDiv.classList.add('collect-h2-removed');
- }
- }
- }
- // 针对极大风的
- if (statistDiv){
- const h2Elements = statistDiv.querySelectorAll('h2');
- const tableElements = statistDiv.querySelectorAll('table');
- if (h2Elements.length === 3) {
- if (SettingData[2]){h2Elements[1].remove();}
- if (SettingData[3]){h2Elements[0].remove();}
- }
- if (tableElements.length === 3) {
- if (SettingData[2]){tableElements[1].remove();}
- if (SettingData[3]){tableElements[0].remove();}
- }
- }
- }
- // 处理单个iframe
- function processIframe(iframe) {
- if (processedIframes.has(iframe)) {
- return;
- }
- try {
- // 检查iframe是否已加载内容
- if (iframe.contentDocument && iframe.contentDocument.readyState === 'complete') {
- //console.log('处理iframe:', iframe);
- deleteSecondH2(iframe.contentDocument);
- setupIframeObserver(iframe.contentDocument);
- processedIframes.add(iframe);
- } else {
- // 如果iframe未加载完成,等待其加载
- iframe.addEventListener('load', function onLoad() {
- iframe.removeEventListener('load', onLoad);
- console.log('iframe加载完成,开始处理:', iframe);
- deleteSecondH2(iframe.contentDocument);
- setupIframeObserver(iframe.contentDocument);
- processedIframes.add(iframe);
- });
- }
- } catch (error) {
- // 处理跨域iframe访问限制
- if (error.name === 'SecurityError') {
- console.warn('无法访问跨域iframe:', iframe.src);
- } else {
- console.error('处理iframe时出错:', error);
- }
- }
- }
- // 为iframe设置MutationObserver
- function setupIframeObserver(doc) {
- try {
- const iframeObserver = new MutationObserver(function(mutations) {
- let shouldCheck = false;
- for (let mutation of mutations) {
- if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
- shouldCheck = true;
- break;
- }
- if (mutation.type === 'attributes' && mutation.target.classList && mutation.target.classList.contains('collect')) {
- shouldCheck = true;
- break;
- }
- }
- if (shouldCheck) {
- setTimeout(() => deleteSecondH2(doc), 100);
- }
- });
- iframeObserver.observe(doc.body, {
- childList: true,
- subtree: true,
- attributes: true,
- attributeFilter: ['class']
- });
- } catch (error) {
- console.warn('无法在iframe中设置Observer:', error);
- }
- }
- // 查找并处理所有iframe
- function processAllIframes() {
- const iframes = document.querySelectorAll('iframe');
- //console.log(`找到 ${iframes.length} 个iframe`);
- iframes.forEach(iframe => {
- processIframe(iframe);
- });
- }
- // 主文档的MutationObserver
- const mainObserver = new MutationObserver(function(mutations) {
- let iframeAdded = false;
- for (let mutation of mutations) {
- if (mutation.type === 'childList') {
- for (let node of mutation.addedNodes) {
- // 检查新增的节点是否是iframe,或者包含iframe
- if (node.nodeName === 'IFRAME') {
- iframeAdded = true;
- setTimeout(() => processIframe(node), 100);
- } else if (node.nodeType === 1 && node.querySelector) {
- const newIframes = node.querySelectorAll('iframe');
- if (newIframes.length > 0) {
- iframeAdded = true;
- setTimeout(() => {
- newIframes.forEach(processIframe);
- }, 100);
- }
- }
- }
- }
- }
- if (iframeAdded) {
- setTimeout(processAllIframes, 200);
- } else {
- // 检查普通DOM变化
- let shouldCheck = false;
- for (let mutation of mutations) {
- if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
- shouldCheck = true;
- break;
- }
- if (mutation.type === 'attributes' && mutation.target.classList && mutation.target.classList.contains('collect')) {
- shouldCheck = true;
- break;
- }
- }
- if (shouldCheck) {
- setTimeout(() => deleteSecondH2(document), 100);
- }
- }
- });
- // 初始化和启动监控
- function init() {
- console.log('开始监控iframe和DOM变化');
- // 初始处理主文档
- deleteSecondH2(document);
- // 初始处理所有已存在的iframe
- processAllIframes();
- // 启动主文档观察器
- mainObserver.observe(document.body, {
- childList: true,
- subtree: true,
- attributes: true,
- attributeFilter: ['class']
- });
- // 监听页面加载完成
- window.addEventListener('load', function() {
- setTimeout(() => {
- deleteSecondH2(document);
- processAllIframes();
- }, 1000);
- });
- // 定期检查新iframe(针对动态加载的内容)
- setInterval(processAllIframes, 3000);
- }
- // 等待DOM准备就绪
- if (document.readyState === 'loading') {
- document.addEventListener('DOMContentLoaded', init);
- } else {
- init();
- const Mode1 = localStorage.getItem('tm_Mode1') === 'true';
- const Mode2 = localStorage.getItem('tm_Mode2') === 'true';
- const Mode3 = localStorage.getItem('tm_Mode3') === 'true';
- const Mode4 = localStorage.getItem('tm_Mode4') === 'true';
- applyMode(Mode1,0);
- applyMode(Mode2,1);
- applyMode(Mode3,2);
- applyMode(Mode4,3);
- }
- })();
复制代码 适用于360浏览器的油猴插件及代码,详见附件。
|
|