1

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);
}
ThePirate
  • 33
  • 5
  • Can you provide the sample input and output values you expect? – Tanaike Oct 14 '21 at 23:11
  • Input is two url strings with the base same domain. Example "http://offer.website.com/signup", "website.com". Output -1 if one of the string is using a subdomain, 0 if direct match, 1 if a subdirectory is used in one of the string, I should add -2 if domains don't match. The output is not so important as the ability to get the domain and subdomain in each string. – ThePirate Oct 15 '21 at 22:28
  • 1
    Thank you for replying. Now I noticed that an answer has already been posted and the discussions have been advanced. In this case, I would like to respect the existing answer and discussions. – Tanaike Oct 15 '21 at 23:27

1 Answers1

0
  • 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 core-js@3.18.3
      
    • 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.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Thank you for your help. I'm using Google Scripts, which means it is hosted at Google. This does not allow for installing new libraries or importing them. It why I asked if there are parsing servers which can be connected to using the REST API. – ThePirate Oct 15 '21 at 22:23
  • 1
    @ThePirate Transpilation is the process of converting libraries to pure JavaScript. I tested this method and I can use `new URL()` in apps script. – TheMaster Oct 15 '21 at 22:33
  • Alright I'll look into it before I ask more questions. Thank you. – ThePirate Oct 15 '21 at 22:38