Getting favicons

Viewing 0 reply threads
  • Author
    Posts
  • September 4, 2020 at 10:42 AM #41846

    cf
    Participant

    I’ve been using a form for storing bookmarks but text-only makes it hard to browse the list. Here’s a script that automatically fetches the favicon for each URL in a form. Obviously you would need to update with your own ids.

    // https://stackoverflow.com/a/54947757/952123
    const getHostname = (url) => {
      // run against regex
      const matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
      // extract hostname (will be null if no match is found)
      return matches && matches[1];
    }
    
    function favIconURL(domain) {
    	return https://www.google.com/s2/favicons?domain=${domain};
    }
    
    function getFavIconURL(url) {
    	return favIconURL(getHostname(url));
    }
    
    function getFavicon(r) {
    	const url = r.getFieldValue('fld-026ad10a88d74569a6d37b19aa7b77a6');
    	console.log(getFavIconURL(url));
    	r.addPhotoFromUrlToField(getFavIconURL(url), 'fld-42eeb21b65464cb39aa966772620acba', { "filename": "favicon", "prevent_duplicates": true})	
    }
    
    function getIfEmpty(r) {
    	const thumbnail = r.getFieldValue('fld-42eeb21b65464cb39aa966772620acba');
    	if (thumbnail.length) return;
    	return getFavicon(r);
    }
    
    form.getRecords().forEach(getIfEmpty);
    form.saveAllChanges();
    
Viewing 0 reply threads

You must be logged in to reply to this topic.