/* Copyright (c) 2009 - 2017, Evan Jehu All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EVAN JEHU BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ window.addEventListener('focus', onVisiblitychange, false); document.addEventListener("visibilitychange", onVisiblitychange, false); /** * lazy variable to address the background page */ var bg = chrome.extension.getBackgroundPage(); /** * connect to the background page on opening */ var bgMessagePort = chrome.runtime.connect({name: "qtPopup"}); /** * log name constant */ var LOG_SRC = "POPUP"; /** * empty variable used to cache the browser history once it has been loaded */ var historyCache = null; /** * the search implementation to be used when search tabs */ var search = null; /** * max number of search results to show when searching bookmarks and history. */ var MAX_NON_TAB_RESULTS = 50; /** * minimum tabs required before bookmarks get searched automatically. */ var MIN_TAB_ONLY_RESULTS = bg.autoSearchBookmarks() ? 5 : 0; /** * Simple little timer class to help with optimizations */ function Timer() { this.start = this.last = (new Date).getTime(); } Timer.prototype.log = function() { var args = Array.prototype.slice.call(arguments); var now = (new Date).getTime(); args.push("total time " + (now - this.start) + " m/s, delta " + (now - this.last) + " m/s"); log.apply(this, args); this.last = now; }; Timer.prototype.reset = function() { this.start = this.last = (new Date).getTime(); }; /** * timer to record page initialization events */ var pageTimer = new Timer(); /** * Log call that prepends the LOG_SRC before delegating to the background page to simplify debugging */ function log() { if (bg.debug) { var args = Array.prototype.slice.call(arguments); args.unshift(LOG_SRC); bg.log.apply(bg, args); } } function openInNewTab(url) { log("opening new tab", url); chrome.tabs.create({url: url, index: 1000}); return closeWindow(); } function closeWindow() { /** * unbind document events before closing the popup window, see issue * Chrome shortcuts do not work immediately after using quicktabs #95 */ log("Unbinding document event handlers."); $(document).unbind(); window.close(); return false; } function closeTabs(tabIds) { bg.recordTabsRemoved(tabIds, function() { for (var x = 0; x < tabIds.length; x++) { var tabId = tabIds[x]; chrome.tabs.remove(tabId); $("#" + tabId).fadeOut("fast").remove(); } $('.closed').remove(); }) } function scrollToFocus() { var element = $(".withfocus"); var offset = element.offset().top; var elementHeight = element.outerHeight(true) * 2; var visible_area_start = $(window).scrollTop(); var visible_area_end = visible_area_start + window.innerHeight; if (offset < visible_area_start + elementHeight) { // scrolling up $('html,body').animate({scrollTop: offset - elementHeight}, 10); return false; } else if (offset > visible_area_end - elementHeight) { // scrolling down $('html,body').animate({scrollTop: offset - window.innerHeight + elementHeight}, 10); return false; } return true; } function focus(elem) { $(".withfocus").removeClass('withfocus'); elem.addClass('withfocus'); } function entryWithFocus() { return $(".withfocus"); } function isFocusSet() { return entryWithFocus().length > 0; } function focusFirst() { return $(".item:first").addClass("withfocus"); } function focusLast() { return $(".item:last").addClass("withfocus"); } function focusPrev(skip) { skip = skip || 1; entryWithFocus().removeClass('withfocus').prevAll(".item").eq(skip - 1).addClass('withfocus'); if (!isFocusSet()) { (skip === 1 ? focusLast : focusFirst)(); } scrollToFocus(); } function focusNext(skip) { skip = skip || 1; entry = entryWithFocus().removeClass('withfocus').nextAll(".item").eq(skip - 1).addClass('withfocus'); if (!isFocusSet()) { (skip === 1 ? focusFirst : focusLast)(); } scrollToFocus(); } /** * This function takes 2 arrays of tabs and returns a new array that contains all of the valid tabs in the recordedTabsList with * and tabs in the queryTabList appended. * * @param recordedTabsList a list of currently know tabs * @param queryTabList a tab query result array * @returns {Array} an array of tabs in the same order as recordedTabsList with any invalid entries removed and any tabs found in queryTabList * appended to the end */ function compareTabArrays(recordedTabsList, queryTabList) { var queriedTabsMap = {}; var tabsToRender = []; for (var i = 0; i < queryTabList.length; i++) { if (queryTabList[i] && queryTabList[i].id) { queriedTabsMap[queryTabList[i].id] = queryTabList[i]; } } for (var x = 0; x < recordedTabsList.length; x++) { if (!recordedTabsList[x]) { continue; } var id = recordedTabsList[x].id; var tab = queriedTabsMap[id]; if (tab) { tabsToRender.push(tab); delete queriedTabsMap[id]; } else { log(" tab found that is not currently reported as open: ", recordedTabsList[x]); delete queriedTabsMap[id]; } } for (var extraTab in queriedTabsMap) { if (queriedTabsMap.hasOwnProperty(extraTab) && bg.includeTab(queriedTabsMap[extraTab])) { log(' adding missing tab', queriedTabsMap[extraTab]); tabsToRender.push(queriedTabsMap[extraTab]); } } return tabsToRender; } // Valid keyCodes based on Firefox spec for commands, which includes these // as well as keyCodes for A-Z, 0-9. var validKeyCodesMap = { 13: "Return", 33: "PageUp", 34: "PageDown", 35: "End", 36: "Home", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 32: "Space", 45: "Insert", 46: "Delete", 188: "Comma", 190: "Period" } function validateKeys(evt, key) { var matches = evt.ctrlKey == key.ctrl && evt.metaKey == key.meta && evt.altKey == key.alt && evt.shiftKey == key.shift && (validKeyCodesMap[evt.keyCode] == key.key || String.fromCharCode(evt.keyCode) == key.key); return matches; } /** * ============================================================================================================================================================= * Page initialization, rendering and event hookups * ============================================================================================================================================================= */ var popupDisplayed = false; $(document).ready(function() { // pageTimer.log("Document ready"); switch(bg.searchType()) { case 'fuze': search = new FuseSearch(); break; case 'regex': search = new RegExSearch(); break; case 'substring': search = new StringContainsSearch(); break; case 'fuzzy': default: // make sure he have something... search = new FuzzySearch(); break; } $('