This repository has been archived by the owner on Aug 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
content-keydown.js
73 lines (60 loc) · 1.81 KB
/
content-keydown.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
var searchBoxVisible = false,
$searchBox, $input;
function onSearchKeypress(e){
if (e.which === 13){ // the ENTER key
var REPO_REGEX = new RegExp("github\\.com/([^/]+/[^/]+)(/.*)?$"),
repo = window.location.href.match(REPO_REGEX)[1],
query = $(this).val();
if ($('body').hasClass('vis-private') || $('.repo-search').length > 0){
// private search (which allows direct search within repo)
window.location = 'http://github.com/' + repo + '/search?q=' + encodeURIComponent(query);
} else {
// public search
window.location = 'https://github.com/search?type=Code&q=' + encodeURIComponent(query) + '+repo%3A' + encodeURIComponent(repo);
}
}
}
function showSearchBox(){
// show the search box
if (!$searchBox){
// create the input field
$searchBox = $('<div style="background-color:#E4E8EC; border: 1px solid #999; border-radius:10px; box-shadow: 5px 8px 10px #999; padding:15px; position:fixed;"><input type="text" style="font-size:3em;"/></div>');
$searchBox.appendTo('body');
$input = $searchBox.find('input');
$input
.blur(hideSearchBox)
.keypress(onSearchKeypress);
}
$searchBox
.show()
.position({
my: 'center',
at: 'center',
of: window
});
$input.focus();
searchBoxVisible = true;
}
function hideSearchBox(){
$searchBox.hide();
searchBoxVisible = false;
}
$(window).keypress(function(e){
if (!searchBoxVisible && e.which === 6){ // CTL-SHIFT-f
showSearchBox();
}
});
$(window).keydown(function(e){
if (searchBoxVisible && e.which === 27){ // the ESC key
hideSearchBox();
}
});
chrome.extension.onMessage.addListener(function(request){
if (request.action === 'toggleSearchBox'){
if (searchBoxVisible){
hideSearchBox();
} else {
showSearchBox();
}
}
});