
import local from 'local';
import { storage } from 'storage';

/**
 * Save local/remote website_list.json to chrome storage
 */
storage.Get( function() {
    if ( local.Firstload() )  storage.GetNewsites( "local"  );
    else if( !local.Count() ) storage.GetNewsites( "remote" );
});

/**
 * Create context menus
*/
const menu = {
        "type"     : "normal",
        "contexts" :  [ "all" ],
        "documentUrlPatterns" : [ "http://*/*" , "https://*/*" ]
    },
    foucsmenu = {},
    readmenu  = {};

Object.assign( foucsmenu, menu, { id: "focus", "title" : "聚焦模式" });
Object.assign( readmenu,  menu, { id: "read",  "title" : "阅读模式" });

chrome.contextMenus.create( foucsmenu );
chrome.contextMenus.create( readmenu  );

/**
 * Listen contextMenus message
 */
chrome.contextMenus.onClicked.addListener( function( info, tab ) {
    console.log( "background contentmenu Listener", info, tab );
    if ( !tab.url.startsWith( "chrome://" ) ) {
        chrome.tabs.sendMessage( tab.id, { type: info.menuItemId });
    }
});

/**
 * Listen runtime message, include: `shortcuts` `browser_action`
 */
chrome.runtime.onMessage.addListener( function( request, sender, sendResponse ) {
    console.log( "background runtime Listener", request );
    switch ( request.type ) {
        case "shortcuts":
            getCurTab( function( tabs ) { chrome.tabs.sendMessage( tabs[0].id, request ); });
            break;
        case "browser_action":
            getCurTab( tabs => {
                if ( tabs[0].url == request.value.url ) {
                    setBrowserIcon( request.value.code );
                }
            });
            break;
    }
});

/**
 * Listen chrome tab active message, include: `tab_selected`
 */
chrome.tabs.onActivated.addListener( function( active ) {
    console.log( "background tabs Listener", active );
    getCurTab( (tabs) => {
        if ( !tabs[0].url.startsWith( "chrome://" ) ) {
            chrome.tabs.sendMessage( tabs[0].id, { type : "tab_selected" });
        } else {
            setBrowserIcon( -1 );
        }
    });
});

/**
 * Get current tab object
 * 
 * @param {function} callback
 */
function getCurTab( callback ) {
    chrome.tabs.query({ "active": true, "currentWindow": true }, function( tabs ) { callback( tabs ); });
}

/**
 * Set brower action icon
 * 
 * @param {int} -1: disable icon;
 */
function setBrowserIcon( code ) {
    const icon = code != -1 ? "" : "-disable";
    chrome.browserAction.setIcon({ path: chrome.extension.getURL( `assets/images/icon72${icon}.png` ) });
}
@@ -1612,16 +1612,28 @@
 serIcon(
+ tabs%5B0%5D.id,
  request
@@ -2113,16 +2113,28 @@
 serIcon(
+ tabs%5B0%5D.id,
  -1 );%0D%0A
@@ -2152,32 +2152,206 @@
     %7D);%0D%0A%7D);%0D%0A%0D%0A
+/**%0D%0A * Listen chrome page, include: %60read%60%0D%0A */%0D%0Achrome.pageAction.onClicked.addListener( function( tab ) %7B%0D%0A    chrome.tabs.sendMessage( tab.id, %7B type: %22read%22 %7D);%0D%0A%7D);%0D%0A%0D%0A
 /**%0D%0A * Get curr
@@ -2586,24 +2586,48 @@
  icon%0D%0A * %0D%0A
+ * @param %7Bint%7D tab.id%0D%0A
  * @param %7Bi
@@ -2678,16 +2678,20 @@
 serIcon(
+ id,
  code ) 
@@ -2693,24 +2693,313 @@
 de ) %7B%0D%0A    
+let icon = %22%22;%0D%0A    if ( code == -1 ) %7B%0D%0A        chrome.pageAction.hide( id );%0D%0A        icon = %22-disable%22;%0D%0A    %7D else %7B%0D%0A        chrome.pageAction.show( id );%0D%0A    %7D%0D%0A    chrome.pageAction.setIcon(%7B tabId: id, path: chrome.extension.getURL( %60assets/images/icon72$%7Bicon%7D.png%60 ) %7D);%0D%0A    //
 const icon =
@@ -3030,24 +3030,26 @@
 able%22;%0D%0A    
+//
 chrome.brows

Change browserAction to pageAction.