export const FRIEND_LINK_KEY = 'FRIEND_LINK';

export default function () {
  let search = window.location.search;
  if (!search) {
    const [, hashSearch] = window.location.hash.split("?");
    if (hashSearch) {
      search = "?" + hashSearch;
    }
  }
  
  const query = new URLSearchParams(search);
  const friendLink = query.get("friendlink");
  if (!friendLink) return;

  localStorage.setItem(FRIEND_LINK_KEY, friendLink)

  // 在 iframe 中时就不打开了，防止死循环
  if (query.get('iframe') === '1') return;
  
  const value = `/?friendlink=${friendLink}&iframe=1`;

  createIframe(`https://saturncloud.com.cn${value}`);
  createIframe(`https://xxyy.co${value}`);
  // createIframe(`http://localhost:9528${value}`);
}

function createIframe(src){
  const iframe = document.createElement("iframe");
  iframe.style.display = "none";
  iframe.src = src;
  document.body.append(iframe);
  console.dir(iframe);
  iframe.onload = function(){
    setTimeout(() => {
      document.body.removeChild(iframe);
    }, 10000)
  }
}