
if(typeof browser === 'undefined') {
  window.browser = window.chrome;
}

let INTERVAL_ID;
// the same members as storage.local's
let SETTINGS = {
  update_check : true,
  update_check_interval : 14000,
  mute_by_key : false,
  block_by_key : false,
};
const STORE_NAME = 'pptw_settings';

const pptw = {

  adjustScrollPos : () => {
    //#page-container > div.ProfileCanopy.ProfileCanopy--withNav.ProfileCanopy--large.js-variableHeightTopBar > div > div.ProfileCanopy-header.u-bgUserColor
    console.log('do');
    const header = document.querySelector('#page-container > div.ProfileCanopy.ProfileCanopy--withNav.ProfileCanopy--large.js-variableHeightTopBar > div > div.ProfileCanopy-header.u-bgUserColor');
    header.style.height = '280px';
    const selectedItem = document.getElementsByClassName('selected-stream-item');
    //selectedItem[0].scrollIntoView();
  },
  
  clickUpdateButton : () => {
    const wk_elm = document.getElementsByClassName('new-tweets-bar');
    if(wk_elm && wk_elm.length > 0) {
      wk_elm[0].click();
    }
  },

  handleKeydown : (evt) => {
    if(evt.key === 'k') {
      console.log(`key ${evt.key}`);
      pptw.adjustScrollPos();
    }
    else if(SETTINGS.block_by_key === false && evt.key === 'b') {
      console.log(`block ${SETTINGS.block_by_key}`);
      evt.preventDefault();
    }
    else if(SETTINGS.mute_by_key === false && evt.key === 'u') {
      console.log(`mute ${SETTINGS.mute_by_key}`);
      evt.preventDefault();
    }
    else if(SETTINGS.update_check === false && evt.key === 'a') {
      INTERVAL_ID = setInterval(pptw.clickUpdateButton, SETTINGS.update_check_interval);
      SETTINGS.update_check = true;
      pptw.updatePageAction();
    }
    else if(SETTINGS.update_check && evt.key === 'q') {
      clearInterval(INTERVAL_ID);
      SETTINGS.update_check = false;
      pptw.updatePageAction();
    }
  },

  preventKeydown : (evt) => {
    if(evt.target.isContentEditable
      || evt.target.nodeName.toUpperCase() === 'INPUT'
      || evt.target.nodeName.toUpperCase() === 'TEXTAREA') {
      return;
    }
    console.log('preventkeydown');
    evt.preventDefault();
  },

  updatePageAction : () => {
    browser.runtime.sendMessage({ task:'setIcon', icon: SETTINGS.update_check, });
  },

};

const start = () => {
  browser.storage.local.get(STORE_NAME, (store_obj) => {
    const result = store_obj[STORE_NAME];
    if(result) {
      SETTINGS = result;
    }
    console.log(`${JSON.stringify(SETTINGS, null, 2)}`);
    pptw.updatePageAction();
    document.addEventListener('keydown', pptw.handleKeydown);
    if(SETTINGS.update_check) {
      INTERVAL_ID = setInterval(pptw.clickUpdateButton, SETTINGS.update_check_interval);
    }
  });
};

start();

// vim:expandtab ff=dos fenc=utf-8 sw=2

@@ -1078,16 +1078,347 @@
   %7D,%0D%0A%0D%0A
+  getTweetList : () =%3E %7B%0D%0A    const root = document.getElementById('stream-items-id');%0D%0A    if(!root) %7B%0D%0A      return;%0D%0A    %7D%0D%0A    const tweetContainerList = root.getElementsByClassName('js-stream-item');%0D%0A    if(tweetContainerList && tweetContainerList.length %3E 0) %7B%0D%0A      console.log(tweetContainerList.length);%0D%0A    %7D%0D%0A  %7D,%0D%0A%0D%0A
   handle
@@ -2247,32 +2247,137 @@
 tion();%0D%0A    %7D%0D%0A
+    else if(evt.key === 'l') %7B%0D%0A      console.log(%60key $%7Bevt.key%7D%60);%0D%0A      pptw.getTweetList();%0D%0A    %7D%0D%0A
   %7D,%0D%0A%0D%0A  preven

handle tweet list on screen. beginning