QUOTE: Let your heart guide you always.

pinpot

A little bookmark tool

content.js (1089B)


      1function extract_info(options) {
      2  let value;
      3  for (const [query, property] of options) {
      4    if (value) {
      5      break;
      6    }
      7    value = document.querySelector(query)?.[property];
      8  }
      9  return value;
     10}
     11
     12
     13browser.runtime.onMessage.addListener((message, _sender, sendResponse) => {
     14  if (message.action === 'getPageData') {
     15    const title = extract_info([
     16      ['meta[property="og:title"]', 'content'],
     17      ['meta[property="twitter:title"]', 'content'],
     18      ['head title', 'textContent'],
     19    ]);
     20    const description = extract_info([
     21      ['meta[property="og:description"]', 'content'],
     22      ['meta[property="twitter:description"]', 'content'],
     23      ['meta[name="description"]', 'content'],
     24    ]);
     25    const image = extract_info([
     26      ['meta[property="og:image"]', 'content'],
     27      ['meta[property="twitter:image"]', 'content'],
     28      ['link[rel="icon"]', 'href'],
     29      ['link[rel="shortcut icon"]', 'href'],
     30    ]);
     31    const data = {
     32      title,
     33      description,
     34      image,
     35      url: location.href,
     36      host: location.host,
     37    };
     38    sendResponse(data);
     39  }
     40});