URL.canParse Polyfill

Polyfill for URL.canParse() in older browsers

  1. // ==UserScript==
  2. // @name URL.canParse Polyfill
  3. // @version 0.1.0
  4. // @description Polyfill for URL.canParse() in older browsers
  5. // @author dragonish
  6. // @namespace https://github.com/dragonish
  7. // @license GNU General Public License v3.0 or later
  8. // @match *://*/*
  9. // @compatible chrome version < 120
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. if (typeof URL.canParse !== 'function') {
  15. URL.canParse = function (url, base) {
  16. try {
  17. const _fullUrl = base ? new URL(url, base) : new URL(url);
  18. return true;
  19. } catch (e) {
  20. return false;
  21. }
  22. };
  23. }
  24. })();