const script = document.createElement("script");
script.src = chrome.runtime.getURL("provider.js");
script.onload = () => script.remove();
(document.head || document.documentElement).appendChild(script);

window.addEventListener("message", (event) => {
  if (event.source !== window || event.data?.source !== "contractless-page") return;
  chrome.runtime.sendMessage(
    {
      type: "provider-request",
      method: event.data.method,
      params: event.data.params
    },
    (response) => {
      window.postMessage(
        {
          source: "contractless-wallet",
          id: event.data.id,
          response
        },
        window.location.origin
      );
    }
  );
});
