Skip to content

Commit

Permalink
add direct URL, show current hotkeys, fix Ubuntu settings reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Juuso-H committed May 19, 2020
1 parent d5b58fa commit 29c2b4c
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 114 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ Alt+W = Wikipedia
Alt+A = Google Images
Alt+S = YouTube

The target search websites and keyboard shortcuts can be changed from the extension settings menu.
Go to Chrome extension shortcuts page to change hotkeys.
194 changes: 143 additions & 51 deletions background.js
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 modified hotkeys-for-search.zip
Binary file not shown.
5 changes: 2 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
},
"icons": {"16": "icon.png", "128": "icon2.png"},
"name": "Hotkeys for Search",
"permissions": ["commands", "tabs", "storage",
"http://*/", "https://*/"],
"version": "1.2.0",
"permissions": ["commands", "activeTab", "storage"],
"version": "1.3.0",
"commands": {
"search1": {
"suggested_key": {
Expand Down
10 changes: 6 additions & 4 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
<h1>Target Search Websites</h1>
<div role="main" style='width:100%'>
<form>
Website 1 (Default: Alt+Q)&emsp;<input type="text" id="search1" style='width:100%'><br>
Website 2 (Default: Alt+W)&emsp;<input type="text" id="search2" style='width:100%'><br>
Website 3 (Default: Alt+A)&emsp;<input type="text" id="search3" style='width:100%'><br>
Website 4 (Default: Alt+S)&emsp;<input type="text" id="search4" style='width:100%'>
Website 1: <label id="shortcut1"></label><input type="text" id="search1" style='width:100%'><br>
Website 2: <label id="shortcut2"></label><input type="text" id="search2" style='width:100%'><br>
Website 3: <label id="shortcut3"></label><input type="text" id="search3" style='width:100%'><br>
Website 4: <label id="shortcut4"></label><input type="text" id="search4" style='width:100%'>
<br><br>
Go to <a href="#" id="shortcutsLink">Chrome extension shortcuts</a> page to change hotkeys.
<br><br>
<div style="display: inline-block"><input type="checkbox" id="openNewTab"><label for="openNewTab"> Search in a new tab</label><br>
<input type="checkbox" id="openOnLeft"><label id="openOnLeftLabel" for="openOnLeft"> New tab on left side</label><br>
<input type="checkbox" id="openInBackground"><label id="openInBackgroundLabel" for="openInBackground"> New tab in background</label><br>
<br>
<input type="checkbox" id="openURLDirectly"><label id="openURLDirectlyLabel" for="openURLDirectly"> Open directly if selected text is a URL</label><br>
</div>
<div style="display: inline-block; float:right; margin-right:10px"><button type="button" id="Defaults">Reset to defaults</button></div>
</form>
Expand Down
Loading

0 comments on commit 29c2b4c

Please sign in to comment.