(function(){
  var KEY='kr_author_unlock';
  var params=new URLSearchParams(location.search);
  var unlock=params.get('unlock');
  if(unlock==='master' || unlock==='author'){
    try{ localStorage.setItem(KEY,'master'); sessionStorage.setItem(KEY,'master'); }catch(e){}
  }
  if(params.get('lock')==='1'){
    try{ localStorage.removeItem(KEY); sessionStorage.removeItem(KEY); }catch(e){}
  }
  function isUnlocked(){
    try{ return localStorage.getItem(KEY)==='master' || sessionStorage.getItem(KEY)==='master'; }catch(e){ return false; }
  }
  window.KRAuthor={
    unlocked:isUnlocked(),
    level:isUnlocked()?'master':'guest',
    ensure:function(){
      if(!this.unlocked && (location.search.indexOf('unlock=master')>=0)){
        this.unlocked=true; this.level='master';
      }
      return this.unlocked;
    },
    badgeHtml:function(){
      return this.ensure()
        ? '<span class="kr-badge ok">作者权限 · 已解锁</span>'
        : '<span class="kr-badge">访客 · <a href="?unlock=master#/">点此解锁作者</a></span>';
    }
  };
  // style
  var s=document.createElement('style');
  s.textContent='.kr-badge{display:inline-block;font-size:12px;padding:4px 10px;border-radius:8px;border:1px solid rgba(111,155,122,.35);color:#9aa89c;margin:0 0 10px}.kr-badge.ok{color:#7eb889;border-color:rgba(126,184,137,.55);background:rgba(126,184,137,.1)}.kr-badge a{color:#c4a574}';
  document.head.appendChild(s);
})();
