open discord link in app

use discord app to open discord link instead of open in browser

  1. // ==UserScript==
  2. // @name open discord link in app
  3. // @namespace https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
  4. // @version 0.02
  5. // @description use discord app to open discord link instead of open in browser
  6. // @author x94fujo6
  7. // @match *://*/*
  8. // ==/UserScript==
  9. /* jshint esversion: 9 */
  10.  
  11. (async function () {
  12. const script_name = "open discord link in app";
  13. let count = 0, id;
  14.  
  15. await wait_tab();
  16.  
  17. id = setInterval(() => {
  18. let result = main();
  19. if (!result) {
  20. count++;
  21. }
  22. if (count >= 10) {
  23. clearInterval(id);
  24. }
  25. }, 1000);
  26.  
  27. function main() {
  28. let reg = /https\:\/\/(discordapp\.com\/channels.*|discord\.com\/channels.*)/,
  29. links = document.querySelectorAll("a"),
  30. discord_links;
  31. if (links.length > 0) {
  32. discord_links = [...links].filter(a => a.href.match(reg));
  33. if (discord_links.length > 0) {
  34. [...discord_links].forEach(a => a.href = a.href.replace(reg, "discord://$1"));
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40.  
  41. function wait_tab() {
  42. return new Promise(resolve => {
  43. if (document.visibilityState === "visible") return resolve();
  44. document.addEventListener("visibilitychange", () => {
  45. if (document.visibilityState === "visible") {
  46. return resolve();
  47. }
  48. });
  49. });
  50. }
  51.  
  52. })();