Favicon as icon for bookmark/web archive?

Just for the heck of it: Here’s a JXA script that sets the thumbnail of the currently selected bookmark(s) to the favicon of the site (if there is one). It recognizes link rel="icon" elements in the HTML head, “favicon.*” images and images ending in “.ico”. The first one found is set as thumbnail.
NOTE: This script can not be used in a smart rule (yet), since JXA in DT smart rules is still broken. A fix is announced for one of the next DT versions.

(() => {
  
  const app = Application("DEVONthink 3");
  app.includeStandardAdditions = true;
  const records = app.selectedRecords.whose( {_match: [ObjectSpecifier().type, "bookmark"]})();
  records.forEach(r => {
    let favicon = "";
    let found = false;
    const URL = r.url();
    const domain = URL.split('/').slice(0,3).join('/');
    
    const HTML = app.downloadMarkupFrom(URL);
    const embImages = app.getEmbeddedImagesOf(HTML, { baseURL: domain});
    
    if (embImages.length > 0) {
      for (let img of embImages) {
        
        /*
        if img ends with ".ico" or img contains "/favicon" 
            or img is part of a <link rel="icon" ...> element in the header, then
          set favicon to img
          exit loop
        */
        found = /favicon|\.ico$/.test(img) || RegExp(`<link.*rel="icon".*${img}`).test(HTML);
        if (found) {
          favicon = img;
          break; /* exit the loop here */
        }
      }
      if (found && favicon !== "") {
        r.thumbnail = favicon;
      }
    }
  });
})()
1 Like