-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Skater as an Overlay (no more popup) #4
Changes from 1 commit
ca77718
e6ab88e
a525b4e
233d28c
b004608
6dccf6a
4f995ff
3fcdfe4
f14f36d
83144e3
f15d78c
c072ee1
57e52f5
fd693b5
f9dd148
abaf038
40e1311
dde4036
501cc48
ffdcefb
982517c
d490177
d300f62
3f348bf
8c08a50
34f6c6d
4347017
436dc59
ab1be7a
bcd6fef
bfd7d08
4ba4e20
2811541
1ee543d
b64cf4b
19d850b
a806fbb
799ea1c
cab186f
6515d86
104d019
8c6a5af
722c4c1
4affb72
1a24642
3423792
d65ca11
ca16391
e549a01
0ce0afa
c31a29b
2bc350e
84a19a9
bcd89dc
4df158b
1a6fe05
8e583d4
47092d4
abe79b1
0ec5c5e
590aee7
9a481df
9c26639
252a1e2
7c337ba
a2570fd
1e6d070
c229444
73fe112
1992a76
9e2078b
036c9f3
a67e01a
4705a3b
531c4f9
8dd0d4d
b2caa10
b8acd5e
6a063ce
aac71ce
c9f80a1
2047547
f4dc41c
792e37e
95223b0
548422f
89cd3bf
538e88c
c75438c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,41 @@ document.addEventListener('keydown', event => { | |
if (event.ctrlKey && event.key === 'l') { | ||
const overlayDiv = createOverlayDiv(); | ||
const searchDiv = createSearchDiv(); | ||
const resultsDiv = createSearchResultsList(); | ||
|
||
overlayDiv.appendChild(resultsDiv); | ||
overlayDiv.appendChild(searchDiv); | ||
document.body.appendChild(overlayDiv); | ||
|
||
getSearchInputElement().focus(); | ||
|
||
// now the magic | ||
const searchInput = getSearchInputElement(); | ||
searchInput.addEventListener('keyup', async function(event) { | ||
const query = searchInput.value; | ||
const searchResults = searchBookmarks(query); | ||
// refine results | ||
if (typeof searchResults != 'undefined') { | ||
const refinedResults = refineResults(searchResults, query); | ||
updateSearchText(refinedResults); | ||
updateLinkEventListeners(); | ||
} | ||
if (event.key === "Enter") { | ||
// go to first event in the list | ||
const top_result = refinedResults[0]; | ||
window.open(top_result.url) | ||
} | ||
}); | ||
} | ||
}); | ||
|
||
function getSearchInputElement() { | ||
return document.getElementById("searchInput"); | ||
} | ||
} | ||
|
||
function getSearchResultsElement() { | ||
return document.getElementById("searchResults"); | ||
} | ||
|
||
function createOverlayDiv () { | ||
const overlayDiv = document.createElement('div'); | ||
|
@@ -29,4 +53,76 @@ function createSearchDiv() { | |
searchDiv.class = ""; | ||
searchDiv.autocomplete = "off"; | ||
return searchDiv | ||
} | ||
} | ||
|
||
function createSearchResultsList() { | ||
const ul = document.createElement('ul'); | ||
ul.id = "searchResults"; | ||
return ul | ||
} | ||
|
||
function refineResults(searchResults, query) { | ||
return searchResults.filter(result => { | ||
// cuts bookmark Title down to a substring for closer matching | ||
const queryLen = query.length; | ||
const queryLower = query.toLowerCase(); | ||
const bookmarkTitle = result.title.substring(0, queryLen).toLowerCase(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. result.title.toLowerCase().startsWith(queryLen.toLowerCase()) |
||
return bookmarkTitle.includes(queryLower) & result.hasOwnProperty('url'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. result.url |
||
}); | ||
} | ||
|
||
|
||
function updateLinkEventListeners() { | ||
const links = document.querySelectorAll(".link"); | ||
if (links.length) { | ||
links.forEach(link => { | ||
link.addEventListener('click', event => { | ||
const ctrlPressed = (event.ctrlKey || event.metaKey); | ||
const url = event.target.href; | ||
chrome.tabs.create({'url': url, active: !ctrlPressed}); | ||
}, false); | ||
}); | ||
} | ||
} | ||
|
||
function updateSearchText(results) { | ||
const resultsDiv = getSearchResultsElement(); | ||
// wipes the unordered list | ||
resultsDiv.innerHTML = ''; | ||
results.forEach(result => { | ||
resultsDiv.appendChild( | ||
createListItem(result) | ||
); | ||
}); | ||
} | ||
|
||
function createListItem(result) { | ||
const listElement = document.createElement('li'); | ||
const listURL = document.createElement('a'); | ||
listURL.class = "link"; | ||
|
||
if (result.title.length > 27) { | ||
listURL.innerHTML = result.title.substring(0, 27) + '...'; | ||
} | ||
else { | ||
listURL.innerHTML = result.title; | ||
} | ||
listURL.href = result.url; | ||
listElement.appendChild(listURL); | ||
return listElement | ||
} | ||
|
||
function focusInput() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. focusSearchInputElement |
||
getSearchInputElement().focus(); | ||
} | ||
|
||
function searchBookmarks(query) { | ||
if (query.length > 3) { | ||
chrome.runtime.sendMessage({queryBody: query}, response => { | ||
debugger | ||
console.log(response) | ||
return response | ||
|
||
}); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this if block should be in the func and return the index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also the only diff between these two functions is the +1 vs -1