URL API parser alternative for Google Scripts

715 Views Asked by At

The javascript used by Google Scripts does not include the URI API library for parsing URLs. It also does not support complex (perl-like backwards looking) regular expressions. As far as I know you can't import public libraries. This makes it hard, verbose and unreliable to parse out URL elements.

However it does support web calls through URLFetchApp and the REST API. Is there a parsing server out in the internet that hosts the URI API, which can be called by URLFetchApp or using the built in REST API? I can not find one easily. Other solutions welcome.

I have a working solution only for US based URLs. International URLs break my regEx. I prefer using a robust solution not dependent on regEx.

If you want to know the problem dealing with.... I need to compare two URLs and see if the 2nd url is a on a subdomain, or directory or same as the home page.

function scoreURL (urlOne,urlTwo){

  let regexSubdomain = /(?:http[s]?:\/\/)?([^\/\s]+)(\/.*)?/;

  var urlOneArray = urlOne.split(regexSubdomain);
  var urlTwoArray = urlTwo.split(regexSubdomain);
  var subdomainOne = urlOneArray[1].replace(new RegExp('www.','i'),'')
  var subdomainTwo = urlTwoArray[1].replace(new RegExp('www.','i'),'')

// return -1 if landing page is on sub domais, 0 if landing page is separate page , 1 if landing page is home page
  if (subdomainOne === subdomainTwo) {
    if (urlOneArray[2] === urlTwoArray[2])
      {return (1);} else {return(0);}
    } else return (-1);
}
1

There are 1 best solutions below

3
TheMaster On
  • The basic URL api links to a polyfill core-js module.

  • The URL polyfill uses multiple require statements that are not directly supported in apps script.

  • You can manually copy paste all required files from the parent directory and remove all required dependencies OR

  • Use webpack in your local to transpile the polyfill

    • install webpack and corejs

      mkdir webpack
      cd webpack
      npm init -y
      npm install webpack webpack-cli --save-dev
      npm install --save [email protected]
      
    • src/index.js:

      import 'core-js/web/url'
      
    • Bundle with webpack

      npx webpack
      
    • Copy the resulting bundled js(in dist/main.js) to a file(url.gs) in apps script.

  • You'll now be able to use URL, URLSearchParams in global scope.