// Author: Zhe Wu
// Email: towuzhe@gmail.com
// License: MIT license

function MatchingKeyAndUrl(text, tab) {
    if ((tab.url.search(text) > -1) ||  //general rules
        (tab.title.search(text) > -1) ||  //general rules
        (text == 'm' && tab.url.search("mail.google.com") > -1) ||
        (text == 'c' && tab.title.search("Google Calendar") > -1) ||
        (text == 'i' && tab.url.search("inbox.google.com") > -1) ||
        (text == 'dr' && tab.url.search("drive.google.com") > -1) ||
        (text == 'y' && tab.url.search("youtube.com") > -1) ||
        (text == 'map' && tab.title.search("Google Map") > -1))
    {
        return true;
    }
    return false;
}

function TabExists(currentWindow, text) {
    for (var i = 0; i < currentWindow.tabs.length; i++) {
        if (MatchingKeyAndUrl(text, currentWindow.tabs[i])) {
            return {exist: true, id: currentWindow.tabs[i].id};
        }
    }
    return {exist: false, id: -1};
}

function zmdMain(text) {
    console.log('inputEntered: ' + text);
    chrome.windows.getCurrent({populate: true}, function(currentWindow) {
        var x = new TabExists(currentWindow, text);
        if (x.exist) {
            chrome.tabs.update(x.id, {active:true});
        }
        else { 
            // Tab does not exist.
            if (text == 'm') {
                chrome.tabs.update(null, {url:"https://mail.google.com"});
            } else if (text == 'c') {
                chrome.tabs.update(null, {url:"https://calendar.google.com"});
            } else if (text == 'w') {
                chrome.tabs.update(null, {url:"http://www.weibo.com"});
            } else if (text == 'f') {
                chrome.tabs.update(null, {url:"https://www.facebook.com"});
            } else if (text == 'dr') {
                chrome.tabs.update(null, {url:"https://drive.google.com"});
            } else if (text == 'y') {
                chrome.tabs.update(null, {url:"https://www.youtube.com"});
            } else if (text == 'map') {
                chrome.tabs.update(null, {url:"https://maps.google.com"});
            } else if (text == 'key') {
                chrome.tabs.update(null, {url:"https://keepersecurity.com/vault/"});
            } else {
                // General rule, just use the typed url
                chrome.tabs.update(null, {url:"http://"+text});
            }
        }
    });
}

chrome.omnibox.onInputEntered.addListener(zmdMain);

@@ -68,170 +68,119 @@
 se%0A%0A
-function MatchingKeyAndUrl(text, tab) %7B%0A    if ((tab.url.search(text) %3E -1) %7C%7C  //general rules%0A        (tab.title.search(text) %3E -1) %7C%7C  //general rules%0A        
+var KeyWord = %5B%0A'm',%0A'c',%0A'i',%0A'w',%0A'dr',%0A'y',%0A'map',%0A'key'%0A%5D%0A%0Afunction MatchingKeyAndUrl(text, tab) %7B%0A    if (
 (tex
@@ -295,32 +295,93 @@
 ndar%22) %3E -1) %7C%7C%0A
+        (text == 'w' && tab.url.search(%22weibo.com%22) %3E -1) %7C%7C%0A
         (text ==
@@ -657,24 +657,203 @@
 true;%0A    %7D%0A
+    if (KeyWord.indexOf(text) == -1) %7B%0A        // Not keyword%0A        if ((tab.url.search(text) %3E -1) %7C%7C (tab.title.search(text) %3E -1)) %7B%0A            return true;%0A        %7D%0A    %7D%0A
     return f
@@ -1836,17 +1836,17 @@
 ext == '
-f
+i
 ') %7B%0A   
@@ -1901,20 +1901,20 @@
 s://
-www.facebook
+inbox.google
 .com

remove keyword matching in general rule