Skip to content

Latest commit

 

History

History
274 lines (165 loc) · 12.3 KB

README.md

File metadata and controls

274 lines (165 loc) · 12.3 KB

Function Gallery

Notice

  • These links work if you installed this extension.
  • Working with redirector to link with special url scheme chrome-extension:// from github.
  • Welcome your addition. Edit ./gallery.yaml & create a pull request.

Built-Ins

/**
 * Copy link as Markdown.
 */
({title, url}) => {
  const escaped = title.replace(/[\[\]]/g, '\\$&');
  return `[${escaped}](${url})`;
};
/**
 * Copy as anchor element.
 *
 * You can use mustache template with `render` function.
 * @see https://github.com/janl/mustache.js
 */
page => render('<a href="{{&url}}">{{title}}</a>', page);

Notations

Link notations for various formats and services.

({title, url}) => `[${title.replace(/\s*[\[\]]\s*/g, ' ')} ${url}]`;
/**
 * Copy as はてな記法
 * @see https://help.hatenablog.com/entry/text-hatena-list
 */
({title, url}) => `[${url}:title=${title}]`;

Google Cloud Platform

Linking to a resource considers whether should have `?project=` parameters or not.

Document

/**
 * Create link to GCP document for Scrapbox
 */
({title, url, content, selectingText}) => {

  // prepend heading to title if url has anchor
  const hash = new URL(url).hash;
  if (hash) {
    const doc = new DOMParser().parseFromString(content, 'text/html');
    const heading = doc.querySelector(hash);
    if (heading) {
      title = `${heading.getAttribute('data-text')} - ${title}`;
    }
  }

  // append quote if selecting text
  let quote;
  if (selectingText) {
    quote = '> ' + selectingText.replace(/\n/g, ' ');
  }

  const link = `[${title.replace(/[\[\]]/g, '\\$&')}](${url})`;
  return quote ? [link, quote].join('\n') : link;
}
/**
 * Create link to GCP document for Scrapbox
 */
({title, url, content, selectingText}) => {

  // prepend heading to title if url has anchor
  const hash = new URL(url).hash;
  if (hash) {
    const doc = new DOMParser().parseFromString(content, 'text/html');
    const heading = doc.querySelector(hash);
    if (heading) {
      title = `${heading.getAttribute('data-text')} - ${title}`;
    }
  }

  // append quote if selecting text
  let quote;
  if (selectingText) {
    quote = '> ' + selectingText.replace(/\n/g, ' ');
  }

  const link = `[${title.replace(/\s*[\[\]]\s*/g, ' ')} ${url}]`;
  return quote ? [link, quote].join('\n') : link;
}

Cloud Storage

/**
 * Create link to the GCS browser with gs://... title
 */
({title, url, content, selectingText}) => {
  const u = new URL(url);
  const m = u.pathname.match(/\/storage\/browser\/(?:_details\/)?(?<path>[^;]+)(?:;tab=.+)?/);
  const path = m.groups.path;
  if (!path)throw new Error('unexpected pathname on GCS');

  return `[gs://${path} ${u.origin}${u.pathname}]`;
}

BigQuery

/**
 * Copy project.dataset.table as Markdown
 * 
 * This removes the working project(`?project=`) params.
 */
(page) => {
  const orig = new URL(page.url);
  const url = new URL(orig.path, orig.origin);
  const parts = [];
  ['p', 'd', 't'].forEach(k => {
    const param = orig.searchParams.get(k);
    parts.push(param);
    url.searchParams.set(k, param);
  });
  return `[${parts.join('.')} - BigQuery](${url.toString()})`;
}

Other Web Sites

Amazon.co.jp

/**
 * Copy simplified amazon.co.jp item URL.
 *
 * Returning falsy value doesn't overwrite your clipboard.
 */
({url, modifier}) => {
  const match = url.match(/(\/dp\/\w+|\/gp\/product\/\w+)[/?]?/);
  if (!match) return undefined;
  const u = new URL(url).origin + match[1];
  return modifier.shift ? u + '?tag=pokutuna-22' : u;
}

YouTube

/**
 * Copy a YouTube video URL with current playging position.
 * Getting seek position from DOM needs to show the player interface once.
 */
({url, content}) => {
  const doc = new DOMParser().parseFromString(content, 'text/html');
  const pos = doc.querySelector('.ytp-time-current').textContent;

  const unit = ['s', 'm', 'h'];
  const time = pos.split(':')
    .reverse()
    .map((n, i) => parseInt(n, 10) + unit[i])
    .reverse()
    .join('');
  
  const orig = new URL(url);
  const u = new URL('/watch', orig.origin);
  u.searchParams.set('v', orig.searchParams.get('v'));
  u.searchParams.set('t', time);
  return u.toString();
}