No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

143 líneas
5.0KB

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>操作书签</title>
  6. <style type="text/css">
  7. .content {
  8. font-size: 15px;
  9. color: darkslategray;
  10. margin: 5px;
  11. }
  12. .content_red {
  13. font-weight: bold;
  14. color: red;
  15. }
  16. .li {
  17. cursor: pointer;
  18. color: blueviolet;
  19. margin: 10px;
  20. }
  21. .def_control {
  22. height: 100%;
  23. width: 100%;
  24. font-size: 18px;
  25. }
  26. .btn_box {
  27. float: left;
  28. line-height: 3.3em;
  29. margin-left: 2%;
  30. }
  31. </style>
  32. </head>
  33. <body onload="bookMarkList()">
  34. <script type="text/javascript" src='js/main.js'></script>
  35. <div class="content">这是一个网页,按f12可以打开调试器</div>
  36. <div class="content">书签列表如下:</div>
  37. <div id="bookMarkDiv">
  38. </div>
  39. <div id="repleaseBookMarkDiv" class="content">
  40. <div class="content">选择的书签是:</div>
  41. <div class="content_red" id="selectBookMarkId"></div>
  42. <div class="content">替换的内容是:</div>
  43. <div class="content">
  44. <textarea id="repleaseBookMarkContent" style="margin: 0px; width: 220px; height: 90px;"></textarea></div>
  45. <div class="btn_box">
  46. <button class="def_control" type="button" id="repleaseBookMarkButton"
  47. onclick="repleaseBookMarkClick()">书签内容替换</button>
  48. </div>
  49. <div class="btn_box">
  50. <button class="def_control" type="button" id="insertLocalFileButton"
  51. onclick="insertLocalFileClick()">本地文件替换已选择书签</button>
  52. </div>
  53. </div>
  54. </body>
  55. </html>
  56. <script>
  57. wps.ApiEvent.AddApiEventListener("WindowActivate", onDocActiveChange) //当前文档切换后的事件回调通知
  58. //当前文档切换到其它文档时,重新获取新的文档的书签列表
  59. function onDocActiveChange(doc, docwin){
  60. bookMarkList()
  61. clearSelectionBookmark()
  62. }
  63. //获取文档中的书签列表并定义列表项的点击事件
  64. function bookMarkList(){
  65. let doc = wps.WpsApplication().ActiveDocument
  66. if (!doc)
  67. return
  68. document.getElementById('bookMarkDiv').innerHTML = ""
  69. let innerHtml = ""
  70. let bookMarkCount = doc.Bookmarks.Count
  71. for (let i = 1; i <= bookMarkCount; ++i) {
  72. let bookMakrItem = doc.Bookmarks.Item(i)
  73. let bookMarkName = bookMakrItem.Name
  74. innerHtml = innerHtml + "<li class='li' onclick=\"bookMarkClick('" + bookMarkName + "')\">" + bookMarkName +
  75. "</li>"
  76. }
  77. document.getElementById('bookMarkDiv').innerHTML = innerHtml
  78. }
  79. //书签列表项的点击事件
  80. function bookMarkClick(bookMarkName) {
  81. let doc = wps.WpsApplication().ActiveDocument
  82. document.getElementById('selectBookMarkId').innerHTML = bookMarkName
  83. if (!doc)
  84. return
  85. let bookMark = doc.Bookmarks.Item(bookMarkName)
  86. if (bookMark)
  87. bookMark.Range.Select()
  88. }
  89. //书签内容替换按钮点击事件
  90. function repleaseBookMarkClick() {
  91. getPositionInsert('text')
  92. }
  93. //本地文件替换已选择书签点击事件
  94. function insertLocalFileClick() {
  95. getPositionInsert('file')
  96. }
  97. //定位到选择的书签位置的最末,插入文本内容或本地文件
  98. function getPositionInsert(textOrFile) {
  99. let doc = wps.WpsApplication().ActiveDocument
  100. let bookMarks = doc.Bookmarks
  101. let selectBookMarkId = document.getElementById("selectBookMarkId").innerHTML
  102. let textareaStr = document.getElementById("repleaseBookMarkContent").value
  103. if (!doc)
  104. return
  105. if (selectBookMarkId.length == 0) {
  106. alert('请选择需要替换的书签')
  107. return
  108. } else {
  109. let bookMarkItem = bookMarks.Item(selectBookMarkId)
  110. if (bookMarkItem) {
  111. let bookMarkName = bookMarkItem.Name
  112. let bookMarkRange = bookMarkItem.Range
  113. if (textOrFile == 'text') {
  114. bookMarkRange.Text = textareaStr
  115. bookMarkRange.Select() //内容替换后不会刷新,让wps作一次刷新,这可能是wps的bug
  116. } else {
  117. OpenLocalFile()
  118. }
  119. //检测一下bookmark是不是失效了
  120. if (!bookMarks.Item(bookMarkName)){
  121. bookMarks.Add(bookMarkName, bookMarkRange)
  122. }
  123. bookMarkList() //执行完内容替换后刷新任务窗格的书签列表
  124. }
  125. }
  126. }
  127. //清空选择的书签
  128. function clearSelectionBookmark(){
  129. document.getElementById('selectBookMarkId').innerHTML = ''
  130. }
  131. </script>