LoginSignup
3
3

More than 3 years have passed since last update.

🔗 ブックマークレット - ページ内の複数のリンクを一括で開く

Last updated at Posted at 2019-06-19

お久しぶりです ✋

Qiita のメルマガに載ってある記事を一括で開きたかったのでブックマークレットを作りました。他のサイトでも使えます。

javascript: {
  const color = '#D35658';
  const d = document;
  const els = new Set();

  const toggle = el => {
    const isSelected = els.has(el);
    isSelected ? els.delete(el) : els.add(el);
    el.style.color = isSelected ? null : color;
  };

  const handler = e => {
    if (e.target instanceof HTMLAnchorElement) {
      toggle(e.target);
      e.preventDefault();
    }
  };
  d.addEventListener('click', handler);

  const button = d.createElement('button');
  button.innerHTML = 'すべて開く';
  button.onclick = () => {
    els.forEach(el => {
      window.open(el.href, '_blank');
      toggle(el);
    });
    d.removeEventListener('click', handler);
    d.body.removeChild(button);
  };
  button.style = `
position: fixed;
cursor: pointer;
z-index: 1000000000;
top: 16px;
right: 16px;
border: none;
border-radius: 6px;
padding: 8px 16px;
background: ${color};
color: white;
`;
  d.body.appendChild(button);
}

使い方

  1. ブックマークレットを実行するとページ内の a タグのリンクが無効化され、選択モードに変わります。

  2. 開きたいリンクを順にクリックして選択状態にします。(リンクのテキストが赤くなります)

  3. 右上の すべて開く をクリックすると選択したすべてのリンクが新しいタブで開かれます。

  4. 開いたあとはページ内のリンクが再度有効になります。

3
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3