aliyun-no-new-tabs.user.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // ==UserScript==
  2. // @name 阿里云掌控版
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.1
  5. // @description try to take over the world!
  6. // @author wd
  7. // @match https://*.aliyun.com/*
  8. // @match https://www.tapd.cn/*
  9. // @icon https://www.google.com/s2/favicons?domain=aliyun.com
  10. // @grant none
  11. // @run-at document-start
  12. // ==/UserScript==
  13. const SELF = '_self';
  14. const BLANK = '_blank';
  15. (function() {
  16. 'use strict';
  17. let isPressed = false;
  18. const makeKeyListener = (state) => {
  19. return e => {
  20. if (['Meta', 'Control'].includes(e.key)) {
  21. isPressed = state
  22. // console.log(state, e, isPressed);
  23. }
  24. };
  25. }
  26. window.addEventListener('keydown', makeKeyListener(true));
  27. window.addEventListener('keyup', makeKeyListener(false));
  28. // window.open
  29. const navigate = (href) => Object.assign(document.createElement("a"), { href, target: isPressed ? BLANK : SELF }).click();
  30. window.open = (url) => navigate(url);
  31. // anchor
  32. setInterval(() => {
  33. document.querySelectorAll("a").forEach(e => { e.target = SELF });
  34. });
  35. const observer = new MutationObserver(mutationList => {
  36. mutationList.forEach(mutation => {
  37. // console.log(mutation.addedNodes);
  38. if (mutation.addedNodes.length) {
  39. mutation.addedNodes.forEach(node => {
  40. if (node instanceof HTMLAnchorElement) node.target = SELF;
  41. })
  42. }
  43. })
  44. });
  45. const checkReady = () => {
  46. const body = document.getElementsByTagName("body")[0];
  47. if (!body) return setTimeout(checkReady, 1000);
  48. observer.observe(body, { childList: true });
  49. }
  50. checkReady();
  51. })();