cms backend management

cms内部管理

目前为 2022-04-12 提交的版本。查看 最新版本

  1. /*
  2. * @Description: File Description
  3. * @FilePath: /study/temperMonkey/backend_cms_mangement/index.js
  4. * @LastEditors: zzz
  5. * @LastEditTime: 2022-04-12 21:48:15
  6. */
  7. // ==UserScript==
  8. // @name cms backend management
  9. // @name:zh-CN cms内部管理
  10. // @namespace http://tampermonkey.net/
  11. // @version 0.5.1
  12. // @description cms内部管理
  13. // @author zzailianlian
  14. // @require https://code.jquery.com/jquery-3.5.1.min.js
  15. // @match http://backend.meitun-test.com/index
  16. // @icon https://www.google.com/s2/favicons?sz=64&domain=meitun-test.com
  17. // @license MIT
  18. // @run-at document-idle
  19. // @grant GM_setValue
  20. // @grant GM_getValue
  21. // @grant GM_addElement
  22. // ==/UserScript==
  23.  
  24. (function () {
  25. 'use strict';
  26.  
  27.  
  28. var sc = document.createElement("script");
  29. sc.setAttribute("type", "text/javascript");
  30. sc.src = "https://code.jquery.com/jquery-3.5.1.min.js";
  31. // document.getElementsByTagName('body')[0].appendChild(sc);
  32.  
  33. if (window.ActiveXObject || "ActiveXObject" in window) { //判断是否是ie
  34. if (sc.readyState) { //判断是否支持readyState
  35. sc.onreadystatechange = function () {
  36. if (this.readyState == "loaded" || this.readyState == "complete") {
  37. console.log("ie10及以下加载完成");
  38. onloaded()
  39. }
  40. }
  41. } else {
  42. sc.onload = function () {
  43. console.log("ie11及Edge加载完成");
  44. onloaded()
  45. }
  46. }
  47. } else { //不是ie
  48. sc.onload = function () {
  49. console.log('非ie浏览器加载完成');
  50. onloaded()
  51. }
  52. }
  53.  
  54. function clearActivitedTab() {
  55. if ($('#menubar_tabs span[title="关闭"]').length) {
  56. $.map($('#menubar_tabs span[title="关闭"]'), function (item) {
  57. item.click()
  58. })
  59. }
  60. }
  61.  
  62. var onloaded = function () {
  63. var jqVersion = $.fn.property || $().property || jQuery.fn.jquery
  64. console.log('version版本:', jqVersion)
  65.  
  66. const wrapperToolsContainer = $('<div id="wrapperToolsContainer">更新的页面id:<input type="number" style="margin-bottom:8px;"/></div>')
  67.  
  68.  
  69. const MODULE_ID = wrapperToolsContainer.find('input').attr('value') || '783'
  70. wrapperToolsContainer.css({
  71. position: 'fixed',
  72. top: 50,
  73. right: 100,
  74. padding: '12px',
  75. background: '#3498db',
  76. borderRadius: '4px',
  77. fontSize: '14px',
  78. zIndex: 999,
  79. opacity: 0.1,
  80. });
  81. wrapperToolsContainer.hover(function () { $(this).css({ opacity: 1 }); }, function () { $(this).css({ opacity: .1 }); })
  82. const initCMSModule = $('<div>初始化cms页面</div>')
  83. initCMSModule.css({
  84. padding: '12px',
  85. color: 'white',
  86. background: '#2ecc71',
  87. borderRadius: '4px',
  88. fontSize: '14px',
  89. border: '1px solid #27ae60',
  90. zIndex: 999,
  91. marginBottom: '24px',
  92. cursor: 'pointer'
  93. });
  94.  
  95. const syncConfig = $('<div>同步配置到页面</div>')
  96. syncConfig.css({
  97. padding: '12px',
  98. color: 'white',
  99. background: '#2ecc71',
  100. borderRadius: '4px',
  101. fontSize: '14px',
  102. border: '1px solid #27ae60',
  103. zIndex: 999,
  104. cursor: 'pointer'
  105.  
  106. });
  107.  
  108. wrapperToolsContainer.append(initCMSModule).append(syncConfig)
  109. $('body').append(wrapperToolsContainer)
  110.  
  111. const CMS_PAGE_TAB_ID = 'CMS_PAGE_TAB_ID'
  112. const CMS_MODULE_TAB_ID = 'CMS_MODULE_TAB_ID'
  113. function getLastTabId() {
  114. return $('#menubar_tabs').find('h3').last().find('a').attr('id')
  115. }
  116. initCMSModule.on('click', () => {
  117. clearActivitedTab();
  118. // 初始化cms管理tab
  119. //$("a:contains('v2.0CMS页面管理')").click()
  120. // console.log('我是 jjjj',$("a:contains('v2.0CMS页面管理')").attr('id').split('_').slice(-1)[0])
  121. $("a:contains('v2.0CMS页面管理')").get(0).click()
  122. GM_setValue(CMS_PAGE_TAB_ID, getLastTabId());
  123. $("a:contains('v2.0CMS系统模板')").get(0).click()
  124. GM_setValue(CMS_MODULE_TAB_ID, getLastTabId());
  125. console.log('我是GM_GETVAULE', GM_getValue(CMS_PAGE_TAB_ID), GM_getValue(CMS_MODULE_TAB_ID))
  126. })
  127.  
  128. function getFrameJQ(id) {
  129. // mainIframe_modifyPage
  130. // mainIframe_tabli_
  131. return $(document.getElementById(id).contentWindow.document.body)
  132. }
  133. function getIdFromTab(tabidArr) {
  134. if (!tabidArr || tabidArr instanceof Array) {
  135. return ''
  136. }
  137. return tabidArr.split('_').slice(-1) || ''
  138. }
  139. function loop(judgeFn = () => { }, callbackFn = () => { }, delay = 1000) {
  140. let threshold = 10 * 1000; // 10s
  141. console.log("interval start")
  142. let startTimeStamp = 0;
  143. const intervalTimer = window.setInterval(function () {
  144. console.log("interval running")
  145. startTimeStamp += 1000;
  146. if (judgeFn()) {
  147. console.log("interval end")
  148. callbackFn()
  149. window.clearInterval(intervalTimer)
  150. return;
  151. }
  152. if (startTimeStamp >= threshold) {
  153. window.clearInterval(intervalTimer)
  154. console.error('loop超时')
  155. return new Error('loop超时')
  156. }
  157. }, 1000)
  158. }
  159.  
  160.  
  161.  
  162. syncConfig.on('click', () => {
  163. // 同步配置模板到页面
  164. const pendingSyncList = $('button:contains("同步线上")')
  165. console.log('我是同步列表', pendingSyncList, window.performance)
  166. const cmsPageFrameJq = getFrameJQ('mainIframe_tabli_' + getIdFromTab(GM_getValue(CMS_PAGE_TAB_ID)))
  167. // 编辑页面
  168. cmsPageFrameJq.find('tr').find('td:contains(' + MODULE_ID + ')').parent().find('button:contains("编辑")').click()
  169.  
  170. loop(function () {
  171. return !!$('#mainIframe_modifyPage' + MODULE_ID).length
  172. }, function () {
  173. // 存在该iframe说明已经【编辑点击之后调起iframe】
  174. const cmsPageModalFrameJq = getFrameJQ('mainIframe_modifyPage' + MODULE_ID)
  175. loop(function () {
  176. // 副标题存在数据,说明【编辑操作】掉接口回填成功
  177. return !!$(getFrameJQ('mainIframe_modifyPage' + MODULE_ID)).find('input[placeholder="如:APP新客活动"]').last().attr('value')
  178. }, function () {
  179. // 下一步
  180. $(getFrameJQ('mainIframe_modifyPage' + MODULE_ID)).find('input[value="下一步"]').last().click();
  181. loop(function () {
  182. // 如果模拟器中存在模板数据,说明【下一步操作】掉接口回填成功
  183. return !!$(getFrameJQ('mainIframe_modifyPage' + MODULE_ID)).find('.cms-show').length
  184. },
  185. function () {
  186. // 保存
  187. $(getFrameJQ('mainIframe_modifyPage' + MODULE_ID)).find('input[value="保存"]').last().click()
  188. loop(function () {
  189. // 如果存在同步线上按钮,说明【保存操作】掉接口回填成功
  190. return !!$(getFrameJQ('mainIframe_tabli_' + getIdFromTab(GM_getValue(CMS_PAGE_TAB_ID)))).find('tr').find('td:contains(' + MODULE_ID + ')').parent().find('button:contains("同步线上")').length
  191. }, function () {
  192. // 同步线上
  193. $(getFrameJQ('mainIframe_tabli_' + getIdFromTab(GM_getValue(CMS_PAGE_TAB_ID)))).find('tr').find('td:contains(' + MODULE_ID + ')').parent().find('button:contains("同步线上")').click()
  194. })
  195. })
  196. })
  197. })
  198. })
  199. }
  200. onloaded()
  201. })();