nullify-external-links

Only allow links that have the same base URL as the current page.

目前为 2015-07-14 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name nullify-external-links
  3. // @namespace https://github.com/ahuanguchi
  4. // @version 1.0.0
  5. // @description Only allow links that have the same base URL as the current page.
  6. // @author ahuanguchi
  7. // @match http*://*/*
  8. // @grant none
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. window.addEventListener("load", function () {
  13. function splitHref(givenHref) {
  14. return givenHref.replace(/^https?:\/\//, "").split("/");
  15. }
  16. var i, currentA, currentHref, anchorParts, locationParts;
  17. var anchors = document.getElementsByTagName("a");
  18. var numAnchors = anchors.length;
  19. for (i = 0; i < numAnchors; i += 1) {
  20. currentA = anchors[i];
  21. currentHref = currentA.href;
  22. anchorParts = splitHref(currentHref);
  23. locationParts = splitHref(window.location.href);
  24. if (currentHref.slice(0, 4) === "http" && anchorParts[0] !== locationParts[0]) {
  25. currentA.removeAttribute("href");
  26. }
  27. }
  28. });