-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
88 lines (85 loc) · 2.16 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
Author: Ulysse Prygiel
*/
var domains= {
jira: "https://jira.hpsvcs.com",
fisheye: "https://fisheye.hpsvcs.com"
}
, paths = {
jira: "/browse/$id",
fisheye: "/cru/$id",
searchJiraOnFisjeye: "/qsearch?q=$id&t=5&project="
}
, defaultKeys = {
jira: "CS-$NUM",
fisheye: "CR-$NUM"
}
, keyRegexps = {
jira: /^[a-zA-Z]{2,4}/,
fisheye: /^CR/
}
, userMessages = {
keyNoValid: "The key isn't valid, keep typing or do a search",
keyValid: "Looks like a valid key, select a site or keep typing"
}
;
function getUrl(site,text,opts){
var baseUrl = domains[site]
, opts = opts||{}
, path = paths[opts.path||site]
, defaultKey = defaultKeys[opts.defaultKey||site]
, keyRegexp = keyRegexps[site]
, url
;
if( ! keyRegexp.test(text) ) {
text = defaultKey.replace('$NUM',text);
}
url = baseUrl + path;
url = url.replace('$id', text);
return url;
}
chrome.omnibox.setDefaultSuggestion({
description: userMessages.keyNoValid
});
chrome.omnibox.onInputChanged.addListener(
function(text, suggest) {
var validRe = /^\d+$|^[a-zA-Z]{2,4}-\d+$/
, isValidKey = validRe.test(text)
, defaultSuggestion
, suggestions = []
;
defaultSuggestion = userMessages[isValidKey ? 'keyValid' : 'keyNoValid'];
console.log(defaultSuggestion)
chrome.omnibox.setDefaultSuggestion({
description: defaultSuggestion
});
suggestions.push({
content: getUrl('fisheye',text,
{path:'searchJiraOnFisjeye',
defaultKey:'jira'}
),
description: "Search a Review"
});
if (isValidKey) {
suggestions.unshift({content: getUrl('fisheye',text), description: "Review CRU"});
suggestions.unshift({content: getUrl('jira',text), description: "JIRA"});
}
suggest(suggestions);
});
// This event is fired with the user accepts the input in the omnibox.
chrome.omnibox.onInputEntered.addListener(
// user accepts
function(text) {
var currentIndex;
chrome.tabs.query({active:true},function(tabs){
// got the active tab
currentIndex = tabs[0].index
// create new tab next to the current one
chrome.tabs.create({
url: text,
index: currentIndex + 1,
active: true
}, function(){}
);
});
});