Advertisement
jcunews

add-wps-doc-download-btn.user.js

Jul 23rd, 2024
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Add WPS document download shortcut button
  3. // @namespace    https://greasyfork.org/en/users/85671-jcunews
  4. // @version      1.0.1
  5. // @license      AGPL v3
  6. // @author       jcunews
  7. // @description  Add a shortcut button for the Download function of the main menu onto the document share page and the document page.
  8. // @match        https://*.docs.wps.com/l/*
  9. // @match        https://*.docs.wps.com/module/common/loadPlatform/*
  10. // @grant        none
  11. // ==/UserScript==
  12.  
  13. (() => {
  14.   function check(btn, container) {
  15.     if (location.pathname.startsWith("/module/") && (btn = document.querySelector('.btn.open-browser'))) { //share page
  16.       btn.insertAdjacentHTML("beforeend", `<button id=dlbtn style="float:right">Download</button>`);
  17.       btn.lastChild.onclick = () => sessionStorage.download = 1
  18.     } else if (location.pathname.startsWith("/l/") && (container = document.querySelector('.component-header-file-info>.file-name'))) { //document page
  19.       container.insertAdjacentHTML("beforeend", `<button id=dlbtn style="margin-left:.5em;padding:0 .3em">Download</button>`);
  20.       container.lastChild.onclick = () => {
  21.         document.querySelector('#component-header-left .header-more-btn>button').click();
  22.         (function waitmenu() {
  23.           if (btn = document.querySelector('.file-more-panel .menu-text[data-key="Download"]')) {
  24.             btn.click()
  25.           } else setTimeout(waitmenu, 200)
  26.         })()
  27.       };
  28.       if (sessionStorage.download) {
  29.         delete sessionStorage.download;
  30.         (function waitmenubtn() {
  31.           if (btn = document.querySelector('#component-header-left .header-more-btn>button')) {
  32.             dlbtn.click();
  33.             if (!btn.matches('.is-selected')) setTimeout(waitmenubtn, 200)
  34.           } else setTimeout(waitmenubtn, 200)
  35.         })()
  36.       }
  37.     }
  38.   }
  39.   check();
  40.   (new MutationObserver(recs => {
  41.     if (!window.dlbtn && recs.some(rec => rec.addedNodes.length > 0)) check()
  42.   })).observe(document.body, {childList: true, subtree: true})
  43. })()
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement