From 14fd0b29a6dc2e285aa9f3bca7eeca5647c2257f Mon Sep 17 00:00:00 2001 From: Sina Siadat Date: Sat, 26 Sep 2015 14:21:46 +0330 Subject: [PATCH] Add files. --- background.js | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++ ff.png | Bin 0 -> 2601 bytes manifest.json | 17 +++++++ 3 files changed, 142 insertions(+) create mode 100644 background.js create mode 100644 ff.png create mode 100644 manifest.json diff --git a/background.js b/background.js new file mode 100644 index 0000000..f53e79f --- /dev/null +++ b/background.js @@ -0,0 +1,125 @@ +// This event is fired each time the user updates the text in the omnibox, +// as long as the extension's keyword mode is still active. +var FF_MAX_SUGGESTIONS = 20; +var FF_MAX_MATCHLENGTH = 1000; +var FF_DEBUGGING = !false; +var FF_HISTORY = []; + +function ffGetDomain(url) { + var a = document.createElement ('a'); + ; a.href = url; + return a.hostname; +} + +function ffSearchFor(text, callback) { + chrome.tabs.query({}, function(array_of_tabs) { + var fuzzy_query = new RegExp(text.split('').join('.*'), 'i'); + var exact_query = new RegExp(text, 'i'); + var exact_query_hl = new RegExp("(" + text + ")", 'ig'); + callback( + array_of_tabs. + map(function(tab) { + + if(FF_DEBUGGING) { console.debug("tab", tab); } + + switch (true) { + case !!tab.title.match(exact_query): + tab.score = 40; + tab.score += 1 - 1.0 * exact_query.exec(tab.title).index / FF_MAX_MATCHLENGTH; + tab.title = tab.title.replace(exact_query_hl, "$1") + break; + case !!tab.url.match(exact_query): + tab.score = 40; + tab.score += 1 - 1.0 * exact_query.exec(tab.url).index / FF_MAX_MATCHLENGTH; + break; + case !!tab.title.match(fuzzy_query): + tab.title = tab.title.replace(fuzzy_query, function(m) { return "" + m + ""; }) + tab.score = 20; + tab.score += 1 - 1.0 * tab.title.match(fuzzy_query)[0].length / FF_MAX_MATCHLENGTH; + break; + case !!tab.url.match(fuzzy_query): + tab.score = 20; + tab.score += 1 - 1.0 * tab.url.match(fuzzy_query)[0].length / FF_MAX_MATCHLENGTH; + break; + default: + tab.score = 0; + } + + if(tab.pinned) { tab.score += 1; } + + return tab; + }). + filter(function(tab) { + return tab.score >= 10; + }). + sort(function(tab1, tab2) { + if(tab1.score < tab2.score) return 1; + if(tab1.score > tab2.score) return -1; + return 0; + }). + slice(0, FF_MAX_SUGGESTIONS). + map(function(tab) { + + var content = JSON.stringify({tabId: tab.id, windowId: tab.windowId}); + var desc = tab.title + " " + ffGetDomain(tab.url) + ""; + if(FF_DEBUGGING) { desc = "score:" + tab.score + " - " + desc; } + + if(tab.status !== "complete") desc = "[" + tab.status + "] " + desc; + if(tab.incognito) desc = "[incognito] " + desc; + if(tab.pinned) desc = "[pinned] " + desc; + if(tab.audible) desc = "[audible] " + desc; + + return {content: content, description: desc}; + }) + ); + }); +} + +chrome.omnibox.onInputChanged.addListener( + function(text, suggest) { ffSearchFor(text, suggest); } +); + +function ffActivateTag(tabId, windowId) { + if(tabId) { + chrome.tabs.update(tabId, {active: true}); + } + if(windowId) { + chrome.windows.update(windowId, {focused: true}); + } +} + +chrome.omnibox.onInputEntered.addListener( + function(text) { + if(FF_DEBUGGING) { + console.debug("entered:", text); + console.debug("history:", FF_HISTORY); + } + + var selected = {}; + + if(text.length === 0) { + if(FF_HISTORY.length >= 2) { + selected = FF_HISTORY[FF_HISTORY.length - 2]; + } else { + return; + } + } else { + try { + selected = JSON.parse(text); + } catch(e) { + // User probably typed something but selected the first default option, + // i.e., "Run ff command: query" + ffSearchFor(text, function(suggestions) { + if(suggestions.length === 0) { return; } + var selected = JSON.parse(suggestions[0].content); + FF_HISTORY.push(selected); + ffActivateTag(selected.tabId, selected.windowId) + }); + return; + } + } + + FF_HISTORY.push(selected); + ffActivateTag(selected.tabId, selected.windowId) + } +); diff --git a/ff.png b/ff.png new file mode 100644 index 0000000000000000000000000000000000000000..25deba751f986389642c178860e31bc8d1eace75 GIT binary patch literal 2601 zcmV+^3fA?BP)004R>004l5008;`004mK004C`008P>0026e000+ooVrmw00002 zVoOIv|Nj)LG0gw~2}DUmK~z}7wb*-bm*tfQ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wR zZ{MVO3@;?NFX#W6-Ia`~AY&7Ue!+(a*!EkFt)S?ajDL??2T?SgmU=2{N%kBIq0jT| z>%m(I>%YaZ37nfxmppFj%#>HTqnt6%vTG>C#nfd)?t7~^M)WTvieF~dZl>=f_YAv> z8TmNdSMtW2YO-14UH;m) z9E?l9Z8^hxQ1vVa*HF_=1z%H3#W{{1Bx5n#vN(Py+PZAGE2io+J1ZG^m{fW)pZ2A` zfBP}%{3?zeitnc@IeQx|?If2i^Ek7NllR1S*0IgeMtgE@JI9wq{Zy)wKFi6?YkyML zN{Z$X^4bdCnK8+`l4RW-Ys>C!)qCIhbiGQ&fBk$4+i;}9i;Q`fqJgnxbXh0U{O3^UJ{1sD3$*1^tOfO~hWx!?a zX4nBr-i)yZa+g!`920v-^1WEPFO6d*rCi^Ou#sCo;r4C}T_0^;4Cl_qWEEY?V|Y}S{BlDf}HLJcN< z_U8Zq03~!qSaf7zbY(hYa%Ew3WdJfTF*PkPG%YYQR53R?Gc-CgI4v+YIxsMI%c5!k z001R)MObuXVRU6WZEs|0W_bWIFflbPFf=VNGgL7*Ix{pnGdL|UH##sd29_Gu00000 LNkvXXu0mjfh&JvR literal 0 HcmV?d00001 diff --git a/manifest.json b/manifest.json new file mode 100644 index 0000000..3572e9d --- /dev/null +++ b/manifest.json @@ -0,0 +1,17 @@ +{ + "manifest_version": 2, + + "name": "FF (Fuzzy Finder)", + "description": "Omnibox Fuzzy Finder for Chrome. Type 'ff' in your address bar and search for your tabs.", + "version": "1.0", + "author": "Sina Siadat ", + "homepage_url": "https://github.com/siadat/chrome-ff", + "permissions": [ "tabs", "activeTab" ], + "background": { + "scripts": ["background.js"] + }, + "omnibox": { "keyword" : "ff" }, + "icons": { + "16": "ff.png" + } +}