forked from Juuso-H/hotkeys-for-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add direct URL, show current hotkeys, fix Ubuntu settings reset
- Loading branch information
Showing
9 changed files
with
229 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,160 @@ | ||
var tabId=0; | ||
var url; | ||
var dict = {}; | ||
|
||
function getDefaultSettings() | ||
{ | ||
var defaultSettings = {}; | ||
defaultSettings["search1"] = "https://www.google.com/search?q="; | ||
defaultSettings["search2"] = "https://en.wikipedia.org/wiki/Special:Search/"; | ||
defaultSettings["search3"] = "https://www.google.com/search?&tbm=isch&q="; | ||
defaultSettings["search4"] = "https://www.youtube.com/results?search_query="; | ||
defaultSettings["openNewTab"] = true; | ||
defaultSettings["openOnLeft"] = false; | ||
defaultSettings["openInBackground"] = false; | ||
defaultSettings["openURLDirectly"] = false; | ||
return defaultSettings; | ||
} | ||
|
||
// Get defaults if no saved values found in synced storage | ||
chrome.storage.sync.get('dict', function(storageDict) { | ||
if (storageDict.search1 != null) { | ||
dict = storageDict; | ||
} | ||
else { | ||
dict["search1"] = "https://www.google.com/search?q="; | ||
dict["search2"] = "https://en.wikipedia.org/wiki/Special:Search/"; | ||
dict["search3"] = "https://www.google.com/search?&tbm=isch&q="; | ||
dict["search4"] = "https://www.youtube.com/results?search_query="; | ||
dict["openNewTab"] = "true"; | ||
dict["openOnLeft"] = "false"; | ||
dict["openInBackground"] = "false"; | ||
chrome.storage.sync.set({'dict' : dict}); | ||
} | ||
}); | ||
|
||
// Save changed settings to synced storage | ||
chrome.storage.onChanged.addListener(function(changes, namespace) { | ||
for (key in changes) { | ||
dict[key] = changes[key]; | ||
// Check if string is a valid URL | ||
// From StackOverflow | ||
// https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url | ||
function validURL(str) | ||
{ | ||
var pattern = new RegExp('^(https?:\\/\\/)?' + // protocol | ||
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name | ||
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address | ||
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path | ||
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string | ||
'(\\#[-a-z\\d_]*)?$', 'i'); // fragment locator | ||
return !!pattern.test(str); | ||
} | ||
|
||
chrome.runtime.onInstalled.addListener(function(details) | ||
{ | ||
chrome.storage.sync.get('dict', function(storageDict) | ||
{ | ||
// Copy settings from old version to new | ||
if (storageDict.search1 != null) | ||
{ | ||
storageDict.openNewTab = (storageDict.openNewTab == "true"); | ||
storageDict.openOnLeft = (storageDict.openOnLeft == "true"); | ||
storageDict.openInBackground = (storageDict.openInBackground == "true"); | ||
storageDict.openURLDirectly = false; | ||
dict = storageDict; | ||
} | ||
// Get stored settings or defaults | ||
else if (storageDict.dict != null) | ||
{ | ||
dict = storageDict.dict; | ||
} | ||
else | ||
{ | ||
dict = getDefaultSettings(); | ||
} | ||
|
||
}); | ||
}); | ||
|
||
// Listen to keyboard hotkeys | ||
chrome.commands.onCommand.addListener(function(command) { | ||
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) { | ||
tabId = tabs[0].index; | ||
chrome.runtime.onStartup.addListener(function() | ||
{ | ||
// Get stored settings or defaults | ||
chrome.storage.sync.get('dict', function(storageDict) | ||
{ | ||
if (storageDict.dict != null) | ||
{ | ||
dict = storageDict.dict; | ||
} | ||
else | ||
{ | ||
dict = getDefaultSettings(); | ||
} | ||
}); | ||
}) | ||
|
||
// Get changes of stored settings | ||
chrome.storage.onChanged.addListener(function(changes, namespace) | ||
{ | ||
for (key in changes) | ||
{ | ||
dict[key] = changes[key]; | ||
} | ||
}); | ||
|
||
// Listen to keyboard hotkeys | ||
chrome.commands.onCommand.addListener(function(command) | ||
{ | ||
var searchURL; | ||
// Select search site according to hotkey pressed | ||
switch (command) { | ||
switch (command) | ||
{ | ||
case "search1": | ||
url = dict["search1"]; | ||
break; | ||
searchURL = dict["search1"]; | ||
break; | ||
case "search2": | ||
url = dict["search2"]; | ||
break; | ||
searchURL = dict["search2"]; | ||
break; | ||
case "search3": | ||
url = dict["search3"]; | ||
break; | ||
searchURL = dict["search3"]; | ||
break; | ||
case "search4": | ||
url = dict["search4"]; | ||
break; | ||
searchURL = dict["search4"]; | ||
break; | ||
} | ||
|
||
// Get selected string from current tab | ||
if (url != "") chrome.tabs.executeScript(null, {code:"window.getSelection().toString()"}, | ||
function(result){ | ||
if (result != ""){ | ||
var newURL = url + result; | ||
var active; | ||
// Open result in current or new tab | ||
if (dict["openInBackground"] == "true") active = false; | ||
else active = true; | ||
if (dict["openNewTab"] == "true") { | ||
if (dict["openOnLeft"] == "true") chrome.tabs.create({ index: tabId, url: newURL, active: active}); | ||
else chrome.tabs.create({ index: tabId + 1, url: newURL, active: active}); | ||
if (searchURL != "") | ||
{ | ||
chrome.tabs.executeScript(null, | ||
{ | ||
code: "window.getSelection().toString()" | ||
}, | ||
function(selectedText) | ||
{ | ||
if (selectedText != "") | ||
{ | ||
var targetURL; | ||
if (dict["openURLDirectly"] && validURL(selectedText.toString())) | ||
{ | ||
targetURL = selectedText.toString(); | ||
if (!targetURL.startsWith("http")) | ||
{ | ||
targetURL = "https://" + targetURL; | ||
} | ||
} | ||
else | ||
{ | ||
targetURL = searchURL + selectedText; | ||
} | ||
// Open result in current or new tab | ||
if (dict["openNewTab"]) | ||
{ | ||
// Get index of current tab for correct positioning of new tab | ||
chrome.tabs.query( | ||
{ | ||
currentWindow: true, | ||
active: true | ||
}, function(tabs) | ||
{ | ||
var targetTabIndex = tabs[0].index; | ||
if (!dict["openOnLeft"]) | ||
{ | ||
targetTabIndex++; | ||
} | ||
chrome.tabs.create( | ||
{ | ||
index: targetTabIndex, | ||
url: targetURL, | ||
active: !dict["openInBackground"] | ||
}); | ||
}); | ||
} | ||
else | ||
{ | ||
chrome.tabs.update( | ||
{ | ||
url: targetURL | ||
}); | ||
} | ||
} | ||
else chrome.tabs.update({url: newURL}); | ||
} | ||
}); | ||
}); | ||
}); | ||
} | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.