weihl_test

open one's eyes to see the world.

  1. // ==UserScript==
  2. // @name weihl_test
  3. // @namespace http://tampermonkey.net/
  4. // @version V0.1
  5. // @description open one's eyes to see the world.
  6. // @author weihule
  7. // @match https://pmos.sd.sgcc.com.cn:18080/trade/main/index.do*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
  9. // @grant GM_download
  10. // @grant GM_xmlhttpRequest
  11. // @license AGPL-3.0
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. function doMYExport() {
  18. let iframe = document.getElementsByClassName('container-fluid')[2].querySelector('iframe');
  19. console.log('进入 iframe 时未报错')
  20.  
  21. let iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
  22. console.log('这里也没报错')
  23.  
  24. setTimeout(function() {
  25. let dialog_header = iframeDocument.getElementsByClassName('modal-dialog modal-lg')[1].getElementsByClassName('modal-content')[0].getElementsByClassName('modal-header')[0];
  26. dialog_header.getElementsByClassName('close')[0].click();
  27. }, 3000);
  28.  
  29. // 定位数据表单控件
  30. iframeDocument.getElementsByClassName('dataTables_scroll')[0]
  31. let table = iframeDocument.querySelector('.dataTables_scrollBody table');
  32. let headers = [];
  33. let headerCells = table.querySelectorAll('thead th');
  34. headerCells.forEach(cell => {
  35. headers.push(cell.textContent.trim());
  36. });
  37. let table_data = [];
  38. let rows = table.querySelectorAll('tbody tr');
  39. rows.forEach(row => {
  40. let rowData = [];
  41. let cells = row.querySelectorAll('td');
  42. cells.forEach(cell => {
  43. rowData.push(cell.textContent.trim());
  44. });
  45. table_data.push(rowData);
  46. });
  47.  
  48. console.log('表头:', headers);
  49. console.log('数据:', table_data);
  50.  
  51. table_data = JSON.stringify(table_data)
  52.  
  53. console.log('---数据:', table_data);
  54. const data2 = {"key": "value","key1": "value","key11": "value"};
  55. GM_xmlhttpRequest({
  56. method: "POST",
  57. url: "http://192.168.1.76:8000/",
  58. headers: {
  59. "Content-Type": "application/json;charset=utf-8"
  60. },
  61. data:table_data,
  62. onload: function(response){
  63. console.log("请求成功");
  64. console.log(response.responseText);
  65. },
  66. onerror: function(response){
  67. console.log("请求失败");
  68. }
  69. });
  70. }
  71.  
  72. function addButton() {
  73. let iframe = document.getElementsByClassName('container-fluid')[2].querySelector('iframe');
  74. if (!iframe) return;
  75.  
  76. let iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
  77. if (!iframeDocument) return;
  78.  
  79. let targetParentDiv = iframeDocument.getElementsByClassName('paper-top');
  80. let secondChildDiv = iframeDocument.getElementsByClassName('fbutton paperBut');
  81.  
  82. if (!targetParentDiv.length ||!secondChildDiv.length) return;
  83.  
  84. // 创建新的 div
  85. let newDiv = iframeDocument.createElement('div');
  86. newDiv.className = 'customDiv';
  87. newDiv.style.marginTop = '5px';
  88. // newDiv.style.width = '90px';
  89.  
  90. const button = iframeDocument.createElement('button');
  91. button.type = 'button';
  92. button.className = 'btn btn-primary hover';
  93. button.textContent = '导出-MY';
  94.  
  95. // 添加点击事件监听器
  96. button.addEventListener('click', doMYExport);
  97.  
  98. // 将按钮添加到新创建的 div 中
  99. newDiv.appendChild(button);
  100.  
  101. targetParentDiv[0].insertBefore(newDiv, secondChildDiv[0]);
  102. }
  103.  
  104. window.addEventListener('load', function() {
  105. addButton();
  106.  
  107. setInterval(() => {
  108. let iframe = document.getElementsByClassName('container-fluid')[2].querySelector('iframe');
  109. if (!iframe) return;
  110. let iframeDocument = iframe.contentDocument || iframe.contentWindow.document;
  111. if (!iframeDocument) return;
  112. let targetElement = iframeDocument.getElementsByClassName('customDiv');
  113. if (targetElement.length === 0){
  114. console.log("需要添加")
  115. addButton()
  116. }
  117. else{
  118. console.log("无需添加")
  119. }
  120. }, 1000);
  121. });
  122. })();