From 5e8a9d2666c1cbc119b1bfde51ce3d4fdc2fec66 Mon Sep 17 00:00:00 2001 From: Alyssa X Date: Sun, 16 Jan 2022 00:21:18 +0000 Subject: [PATCH] Open direct links, search, removing some errors --- src/background.js | 22 +++++++++++++------- src/content.js | 53 +++++++++++++++++++++++++++++++++++++++++------ src/manifest.json | 4 ++-- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/src/background.js b/src/background.js index b9c3c8d..92f11c5 100644 --- a/src/background.js +++ b/src/background.js @@ -154,7 +154,9 @@ chrome.runtime.onInstalled.addListener((object) => { for (let j = 0; j < t; j++) { currentTab = currentWindow.tabs[j]; - injectIntoTab(currentTab); + if (!currentTab.url.includes("chrome://") && !currentTab.url.includes("chrome-extension://") && !currentTab.url.includes("chrome.google.com")) { + injectIntoTab(currentTab); + } } } } @@ -183,6 +185,11 @@ const resetOmni = () => { clearActions(); getTabs(); getBookmarks(); + var search = [ + {title:"Search", desc:"Search for a query", type:"action", action:"search", emoji:true, emojiChar:"🔍", keycheck:false}, + {title:"Search", desc:"Go to website", type:"action", action:"goto", emoji:true, emojiChar:"🔍", keycheck:false} + ]; + actions = search.concat(actions); } // Check if tabs have changed and actions need to be fetched again @@ -330,9 +337,7 @@ const removeBookmark = (bookmark) => { chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { switch (message.request) { case "get-actions": - clearActions(); - getTabs(); - getBookmarks(); + resetOmni(); sendResponse({actions: actions}); break; case "switch-tab": @@ -446,10 +451,13 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { closeTab(message.action); } break; + case "search": + chrome.search.query( + {text:message.query} + ) + break; } }); // Get actions -clearActions(); -getTabs(); -getBookmarks(); +resetOmni(); diff --git a/src/content.js b/src/content.js index e0200da..c3687d8 100644 --- a/src/content.js +++ b/src/content.js @@ -32,9 +32,12 @@ $(document).ready(function(){ img = ""+action.emojiChar+"" } if (index != 0) { - $("#omni-extension #omni-list").append("
"+img+"
"+action.title+"
"+action.desc+"
"+keys+"
Select
"); + $("#omni-extension #omni-list").append("
"+img+"
"+action.title+"
"+action.desc+"
"+keys+"
Select
"); } else { - $("#omni-extension #omni-list").append("
"+img+"
"+action.title+"
"+action.desc+"
"+keys+"
Select
"); + $("#omni-extension #omni-list").append("
"+img+"
"+action.title+"
"+action.desc+"
"+keys+"
Select
"); + } + if (action.type == "action" && action.action == "search") { + $(".omni-item[data-index='"+index+"']").hide(); } }) $(".omni-extension #omni-results").html(actions.length+" results"); @@ -58,9 +61,9 @@ $(document).ready(function(){ img = ""+action.emojiChar+"" } if (index != 0) { - $("#omni-extension #omni-list").append("
"+img+"
"+action.title+"
"+action.url+"
"+keys+"
Select
"); + $("#omni-extension #omni-list").append("
"+img+"
"+action.title+"
"+action.url+"
"+keys+"
Select
"); } else { - $("#omni-extension #omni-list").append("
"+img+"
"+action.title+"
"+action.url+"
"+keys+"
Select
"); + $("#omni-extension #omni-list").append("
"+img+"
"+action.title+"
"+action.url+"
"+keys+"
Select
"); } }) $(".omni-extension #omni-results").html(actions.length+" results"); @@ -115,6 +118,25 @@ $(document).ready(function(){ } } + // Add protocol + function addhttp(url) { + if (!/^(?:f|ht)tps?\:\/\//.test(url)) { + url = "http://" + url; + } + return url; + } + + // Check if valid 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); + } + // Search for an action in the omni function search(e) { if (e.keyCode == 37 || e.keyCode == 38 || e.keyCode == 39 || e.keyCode == 40 || e.keyCode == 13 || e.keyCode == 37) { @@ -174,6 +196,18 @@ $(document).ready(function(){ } } else { $(this).toggle($(this).find(".omni-item-name").text().toLowerCase().indexOf(value) > -1 || $(this).find(".omni-item-desc").text().toLowerCase().indexOf(value) > -1); + if (value == "") { + $(".omni-item[data-index='"+actions.findIndex(x => x.action == "search")+"']").hide(); + $(".omni-item[data-index='"+actions.findIndex(x => x.action == "goto")+"']").hide(); + } else if (!validURL(value)) { + $(".omni-item[data-index='"+actions.findIndex(x => x.action == "search")+"']").show(); + $(".omni-item[data-index='"+actions.findIndex(x => x.action == "goto")+"']").hide(); + $(".omni-item[data-index='"+actions.findIndex(x => x.action == "search")+"'] .omni-item-name").html('\"'+value+'\"'); + } else { + $(".omni-item[data-index='"+actions.findIndex(x => x.action == "search")+"']").hide(); + $(".omni-item[data-index='"+actions.findIndex(x => x.action == "goto")+"']").show(); + $(".omni-item[data-index='"+actions.findIndex(x => x.action == "goto")+"'] .omni-item-name").html(value); + } } }); } @@ -184,7 +218,7 @@ $(document).ready(function(){ // Handle actions from the omni function handleAction(e) { - var action = actions.find(x => x.title == $(".omni-item-active .omni-item-name").text()); + var action = actions[$(".omni-item-active").attr("data-index")]; closeOmni(); if ($(".omni-extension input").val().toLowerCase().startsWith("/remove")) { chrome.runtime.sendMessage({request:"remove", type:action.type, action:action}); @@ -195,7 +229,7 @@ $(document).ready(function(){ window.open($(".omni-item-active").attr("data-url")); } } else { - chrome.runtime.sendMessage({request:action.action, tab:action}); + chrome.runtime.sendMessage({request:action.action, tab:action, query:$(".omni-extension input").val()}); switch (action.action) { case "bookmark": if (e.ctrlKey || e.metaKey) { @@ -234,6 +268,13 @@ $(document).ready(function(){ window.open(action.url, "_self"); } break; + case "goto": + if (e.ctrlKey || e.metaKey) { + window.open(addhttp($(".omni-extension input").val())); + } else { + window.open(addhttp($(".omni-extension input").val()), "_self"); + } + break; } } diff --git a/src/manifest.json b/src/manifest.json index 817c7f0..f2e4660 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -2,7 +2,7 @@ "name": "Omni", "description": "Supercharge Chrome with commands, shortcuts, and more", "offline_enabled": true, - "version": "1.3.4", + "version": "1.3.5", "manifest_version": 3, "action": { "icons": { @@ -45,7 +45,7 @@ } ], "permissions": [ - "tabs", "activeTab", "bookmarks", "browsingData", "history", "scripting" + "tabs", "activeTab", "bookmarks", "browsingData", "history", "scripting", "search" ], "host_permissions": [ "https://www.googleapis.com/*", "https://gstatic.com/*", "*://*/*"