{"size": 845, "ext": "js", "max_stars_repo_path": "source/popup/index.js", "max_stars_repo_name": "kraklin/elm-debug-extension", "max_stars_repo_stars_event_min_datetime": "2020-10-04T21:32:19.000Z", "max_stars_repo_stars_event_max_datetime": "2022-01-15T05:03:06.000Z", "max_issues_repo_path": "source/popup/index.js", "max_issues_repo_name": "kraklin/elm-debug-extension", "max_issues_repo_issues_event_min_datetime": "2020-10-27T17:49:25.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-01T19:13:29.000Z", "max_forks_repo_path": "source/popup/index.js", "max_forks_repo_name": "kraklin/elm-debug-extension", "max_forks_repo_forks_event_min_datetime": "2020-10-05T00:00:59.000Z", "max_forks_repo_forks_event_max_datetime": "2021-11-21T21:07:16.000Z"}
{"max_stars_count": 29, "max_issues_count": 2, "max_forks_count": 2, "avg_line_length": 24.1428571429, "max_line_length": 72, "alphanum_fraction": 0.6426035503}
import browser from 'webextension-polyfill';
import {version} from '../../package.json'
import {Elm} from './Popup.elm';

browser.tabs.query({active: true, currentWindow: true}).then((tabs) => {
  const currentTab = tabs[0];

  const app = Elm.Popup.init({
    node: document.getElementById('elm-popup'),
    flags: {version: version}
  });

  app.ports.sendRequest.subscribe((request) => {
    send(request);
  })

  app.ports.openOptionsPage.subscribe(() => {
    browser.runtime.openOptionsPage();
  })

  function handleError(error) {
    console.error('Error:', error.message);
  }

  const send = async (request) => {
    const sending = browser.tabs.sendMessage(currentTab.id, request);

    sending.then((message) => {
      if(message && message.opts){
        app.ports.receive.send(message.opts);
      }
    }, handleError);
  }
});
