Skip to content
Permalink
Browse files

Added features from Firefox addon:

- Implemented how to handle non-url setting
- Implemented search engine setting
  • Loading branch information
EuanRiggans committed Nov 18, 2019
1 parent 34c7bcf commit 525c360a68677c10fdbe46558c39f70c55450eda
Showing with 250 additions and 88 deletions.
  1. +2 −1 .gitignore
  2. +15 −4 css/style-dark.css
  3. +60 −9 js/bulkopen.js
  4. +65 −11 js/openingtabs.js
  5. +21 −1 js/settings.js
  6. +87 −62 settings.html
@@ -11,4 +11,5 @@
*.njsproj
*.sln
*.sw*
*.txt
*.txt
/web-ext-artifacts/
@@ -6,7 +6,14 @@ a {
font-weight: bold;
}

body, #list, #savedLists, #defaultList, #customTheme, #currentlyOpenedSetting {
body,
#list,
#savedLists,
#defaultList,
#customTheme,
#currentlyOpenedSetting,
#nonURLHandlerSetting,
#selectedSearchEngineSetting {
color: #fff;
background-color: #404040;
}
@@ -36,12 +43,16 @@ button {
background-color: #404040;
}

input[type=text]:disabled, input[type=number]:disabled {
input[type=text]:disabled,
input[type=number]:disabled {
color: #fff;
background-color: #4d4d4d;
}

input[type=text], input[type=text]:focus, input[type=number], input[type=number]:focus {
input[type=text],
input[type=text]:focus,
input[type=number],
input[type=number]:focus {
color: #fff;
background-color: #404040;
}
@@ -64,4 +75,4 @@ input[type=text], input[type=text]:focus, input[type=number], input[type=number]

#openHelp {
color: #fff;
}
}
@@ -95,7 +95,9 @@ function getCurrentTabs() {
if (currentWindowSetting === false) {
currentWindowSetting = undefined;
}
chrome.tabs.query({currentWindow: currentWindowSetting}, tabs => {
chrome.tabs.query({
currentWindow: currentWindowSetting
}, tabs => {
const tabsArray = [];
for (let tab of tabs) {
tabsArray.push(tab.url);
@@ -191,18 +193,30 @@ function openList(list) {
* @param tabCreationDelay The delay between opening a new url
*/
function linksIterator(i, strings, tabCreationDelay) {
let ignoreURL = false;
strings[i] = strings[i].trim();
if (strings[i] === '') {
return;
}
let url = strings[i];
if (!isProbablyUrl(url)) {
url = 'http://www.google.com/search?q=' + encodeURI(url);
if (!isProbablyUrl(url) && getSetting('non_url_handler') === "searchForString") {
url = encodeSearchQuery(url);
} else if (!isProbablyUrl(url) && getSetting('non_url_handler') === "ignoreString") {
ignoreURL = true;
} else if (!isProbablyUrl(url) && getSetting('non_url_handler') === "attemptToExtractURL") {
const extractedString = extractURLFromString(url);
if (isProbablyUrl(extractedString)) {
url = extractedString;
} else {
ignoreURL = true;
}
}
if (!ignoreURL) {
chrome.tabs.create({
active: false,
'url': url
});
}
chrome.tabs.create({
active: false,
'url': url
});
i++;
if (i < strings.length) {
setTimeout(linksIterator, tabCreationDelay, i, strings, tabCreationDelay);
@@ -379,13 +393,48 @@ function getSetting(setting) {
return userSettings.custom_theme;
case "currently_opened_tabs_display":
return userSettings.currently_opened_tabs_display;
case "non_url_handler":
return userSettings.non_url_handler;
case "search_engine":
return userSettings.search_engine;
default:
break;
}
}
}
}

/**
* Builds search engine query url from a string
* @param {*} string
*/
function encodeSearchQuery(string) {
const setting = getSetting("search_engine");
if (setting === "googleEngine") {
string = 'http://www.google.com/search?q=' + encodeURI(string);
} else if (setting === "duckduckgoEngine") {
string = 'https://duckduckgo.com/?q=' + encodeURI(string);
} else if (setting === "bingEngine") {
string = 'https://www.bing.com/search?q=' + encodeURI(string);
}
return string;
}

/**
* Attempts to extract a url from a string
* @param {*} string
*/
function extractURLFromString(string) {
const urlRegex = /(https?:\/\/[^ ]*)/;
let url;
if (string.match(urlRegex)) {
url = string.match(urlRegex)[1];
} else {
url = "noextractionsuccess";
}
return url;
}

/**
* Gets the current version of the extension from the manifest
* @returns {string} The current version
@@ -452,7 +501,9 @@ function createSettings() {
auto_open_lists: 0,
default_list_open: -1,
custom_theme: "defaultBoostrap",
currently_opened_tabs_display: "currentWindow"
currently_opened_tabs_display: "currentWindow",
non_url_handler: "searchForString",
search_engine: "googleEngine"
};
localStorage.setItem("settings", JSON.stringify(newSettings));
}
@@ -478,4 +529,4 @@ function convertOldURLLists() {
localStorage.setItem(listID, newListStorageArray);
}
}
}
}
@@ -15,13 +15,24 @@ function linksIterator(i, strings, tabCreationDelay) {
strings[i] = strings[i].trim();
if (!(strings[i] === '') && !(strings[i] === "linksToOpen")) {
let url = strings[i];
if (!isProbablyUrl(url)) {
url = 'http://www.google.com/search?q=' + encodeURI(url);
if (!isProbablyUrl(url) && getSetting('non_url_handler') === "searchForString") {
url = encodeSearchQuery(url);
} else if (!isProbablyUrl(url) && getSetting('non_url_handler') === "ignoreString") {
ignoreURL = true;
} else if (!isProbablyUrl(url) && getSetting('non_url_handler') === "attemptToExtractURL") {
const extractedString = extractURLFromString(url);
if (isProbablyUrl(extractedString)) {
url = extractedString;
} else {
ignoreURL = true;
}
}
if (!ignoreURL) {
chrome.tabs.create({
active: false,
'url': url
});
}
chrome.tabs.create({
active: false,
'url': url
});
i++;
if (i - 1 < strings.length) {
if (strings[i] == null || strings[i].trim() === '') {
@@ -68,23 +79,66 @@ function isProbablyUrl(string) {
return false;
}

/**
* Gets a specified setting for the user
* @param setting The setting to fetch
* @returns {*} The setting value
*/
function getSetting(setting) {
const settingSelected = setting.toLowerCase();
for (let i = 0; i < localStorage.length; i++) {
const tempArray = loadList(localStorage.key(i));
const tempStorage = loadList(localStorage.key(i));
if (localStorage.key(i) === "settings") {
const userSettings = JSON.parse(tempArray);
console.dir(userSettings);
const userSettings = JSON.parse(tempStorage);
switch (settingSelected) {
case "tab_creation_delay":
return userSettings.tab_creation_delay;
break;
case "auto_open_lists":
return userSettings.auto_open_lists;
break;
case "default_list_open":
return userSettings.default_list_open;
case "custom_theme":
return userSettings.custom_theme;
case "currently_opened_tabs_display":
return userSettings.currently_opened_tabs_display;
case "non_url_handler":
return userSettings.non_url_handler;
case "search_engine":
return userSettings.search_engine;
default:
break;
}
}
}
}

/**
* Builds search engine query url from a string
* @param {*} string
*/
function encodeSearchQuery(string) {
const setting = getSetting("search_engine");
if (setting === "googleEngine") {
string = 'http://www.google.com/search?q=' + encodeURI(string);
} else if (setting === "duckduckgoEngine") {
string = 'https://duckduckgo.com/?q=' + encodeURI(string);
} else if (setting === "bingEngine") {
string = 'https://www.bing.com/search?q=' + encodeURI(string);
}
return string;
}

/**
* Attempts to extract a url from a string
* @param {*} string
*/
function extractURLFromString(string) {
const urlRegex = /(https?:\/\/[^ ]*)/;
let url;
if (string.match(urlRegex)) {
url = string.match(urlRegex)[1];
} else {
url = "noextractionsuccess";
}
return url;
}
@@ -5,6 +5,8 @@ $(document).ready(function () {
let selectedListID = -1;
let selectedTheme = "defaultBootstrap";
let currentlyOpenedTabsSetting = "allOpenedTabs";
let nonURLHandlerSetting = "searchForString";
let searchEngineSetting = "googleEngine";
$tabCreationDelaySelector.val(0);
let settingsObjPresent = false;
for (let i = 0; i < localStorage.length; i++) {
@@ -63,6 +65,8 @@ $(document).ready(function () {
if (parsedList.object_description === "user_settings") {
currentlyOpenedTabsSetting = parsedList.currently_opened_tabs_display;
selectedTheme = parsedList.custom_theme;
nonURLHandlerSetting = parsedList.non_url_handler;
searchEngineSetting = parsedList.search_engine;
}
} catch (e) {
console.log(e);
@@ -71,6 +75,8 @@ $(document).ready(function () {
$("#defaultList option[id=" + selectedListID + "]").prop('selected', true);
$("#customTheme option[id=" + selectedTheme + "]").prop('selected', true);
$("#currentlyOpenedSetting option[id=" + currentlyOpenedTabsSetting + "]").prop('selected', true);
$("#nonURLHandlerSetting option[id=" + nonURLHandlerSetting + "]").prop('selected', true);
$("#selectedSearchEngineSetting option[id=" + searchEngineSetting + "]").prop('selected', true);
document.getElementById("closeModal").addEventListener('click', (e) => {
window.close();
});
@@ -110,6 +116,8 @@ function initSettingsSave() {
const defaultList = getSelectedListID();
const theme = getSelectedTheme();
const currentlyOpenedTabsSetting = getCurrentlyOpenedTabsSetting();
const nonURLHandlerSetting = getNonURLHandlerSetting();
const searchEngineSetting = getSearchEngineSetting();
if ($nightModeSelector.is(":checked")) {
nightMode = 1;
}
@@ -127,14 +135,18 @@ function initSettingsSave() {
auto_open_lists: 0,
default_list_open: -1,
custom_theme: "defaultBoostrap",
currently_opened_tabs_display: "currentWindow"
currently_opened_tabs_display: "currentWindow",
non_url_handler: "searchForString",
search_engine: "googleEngine"
};
userSettings.tab_creation_delay = tabCreationDelay;
userSettings.night_mode = nightMode;
userSettings.auto_open_lists = autoOpenLists;
userSettings.default_list_open = parseInt(defaultList);
userSettings.custom_theme = theme;
userSettings.currently_opened_tabs_display = currentlyOpenedTabsSetting;
userSettings.non_url_handler = nonURLHandlerSetting;
userSettings.search_engine = searchEngineSetting;
saveSettings(userSettings);
}

@@ -153,3 +165,11 @@ function getSelectedTheme() {
function getCurrentlyOpenedTabsSetting() {
return $('select[id="currentlyOpenedSetting"] option:selected').attr('id');
}

function getNonURLHandlerSetting() {
return $('select[id="nonURLHandlerSetting"] option:selected').attr('id');
}

function getSearchEngineSetting() {
return $('select[id="selectedSearchEngineSetting"] option:selected').attr('id');
}

0 comments on commit 525c360

Please sign in to comment.
You can’t perform that action at this time.