summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/snippet/snippet_embed.js
blob: 28a5ed306bd99c6e7a8745a8b14c8421e5bf810d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
export default () => {
  const { protocol, host, pathname } = location;
  const shareBtn = document.querySelector('#share-btn');
  const embedBtn = document.querySelector('#embed-btn');
  const snippetUrlArea = document.querySelector('#snippet-url-area');
  const embedAction = document.querySelector('#embed-action');

  shareBtn.addEventListener('click', (event) => {
    shareBtn.classList.add('is-active');
    embedBtn.classList.remove('is-active');
    snippetUrlArea.value = `${protocol}//${host + pathname}`;
    embedAction.innerHTML = 'Share';
  });

  embedBtn.addEventListener('click', (event) => {
    embedBtn.classList.add('is-active');
    shareBtn.classList.remove('is-active');
    const scriptTag = `<script src="${protocol}//${host + pathname}.js"></script>`;
    snippetUrlArea.value = scriptTag;
    embedAction.innerHTML = 'Embed';
  });
};