From 9e95543899bc27c34c9223249b10b184f1271c2f Mon Sep 17 00:00:00 2001 From: panjiangyi <78237724@qq.com> Date: Tue, 9 Jan 2024 12:47:33 +0800 Subject: [PATCH] fix: open omni failed in new tab --- src/background.js | 19 +++++++++++++------ src/content.js | 8 +++++--- src/newtab.html | 2 +- src/newtab.js | 1 + 4 files changed, 20 insertions(+), 10 deletions(-) create mode 100644 src/newtab.js diff --git a/src/background.js b/src/background.js index b99e4e0..e7905cb 100644 --- a/src/background.js +++ b/src/background.js @@ -8,10 +8,10 @@ const clearActions = () => { const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0; let muteaction = {title:"Mute tab", desc:"Mute the current tab", type:"action", action:"mute", emoji:true, emojiChar:"🔇", keycheck:true, keys:['⌥','⇧', 'M']}; let pinaction = {title:"Pin tab", desc:"Pin the current tab", type:"action", action:"pin", emoji:true, emojiChar:"📌", keycheck:true, keys:['⌥','⇧', 'P']}; - if (response.mutedInfo.muted) { + if (response?.mutedInfo?.muted) { muteaction = {title:"Unmute tab", desc:"Unmute the current tab", type:"action", action:"unmute", emoji:true, emojiChar:"🔈", keycheck:true, keys:['⌥','⇧', 'M']}; } - if (response.pinned) { + if (response?.pinned) { pinaction = {title:"Unpin tab", desc:"Unpin the current tab", type:"action", action:"unpin", emoji:true, emojiChar:"📌", keycheck:true, keys:['⌥','⇧', 'P']}; } actions = [ @@ -475,10 +475,17 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { } break; case "search": - chrome.search.query( - {text:message.query,disposition:message.disposition} - ) - break; + var query = () => + chrome.search.query({ + text: message.query, + disposition: message.disposition, + }); + if (message.__inNewTab__) { + setTimeout(query, 125); + } else { + query(); + } + break; case "restore-new-tab": restoreNewTab(); break; diff --git a/src/content.js b/src/content.js index 5b9b14b..f89e912 100644 --- a/src/content.js +++ b/src/content.js @@ -20,10 +20,11 @@ $(document).ready(() => { // Request actions from the background chrome.runtime.sendMessage({request:"get-actions"}, (response) => { actions = response.actions; + populateOmni() }); // New tab page workaround - if (window.location.href == "chrome-extension://mpanekjjajcabgnlbabmopeenljeoggm/newtab.html") { + if (window.__inNewTab__) { isOpen = true; $("#omni-extension").removeClass("omni-closing"); window.setTimeout(() => { @@ -131,7 +132,7 @@ $(document).ready(() => { // Close the omni function closeOmni() { - if (window.location.href == "chrome-extension://mpanekjjajcabgnlbabmopeenljeoggm/newtab.html") { + if (window.__inNewTab__) { chrome.runtime.sendMessage({request:"restore-new-tab"}); } else { isOpen = false; @@ -304,7 +305,8 @@ $(document).ready(() => { window.open($(".omni-item-active").attr("data-url"), "_self"); } } else { - chrome.runtime.sendMessage({request:action.action, disposition:(e.altKey||e.metaKey)? "NEW_TAB":"CURRENT_TAB", tab:action, query:$(".omni-extension input").val()}); + var disposition = window.__inNewTab__ ? "CURRENT_TAB" : e.altKey || e.metaKey ? "NEW_TAB" : "CURRENT_TAB"; + chrome.runtime.sendMessage({request:action.action,__inNewTab__:window.__inNewTab__, disposition:disposition, tab:action, query:$(".omni-extension input").val()}); switch (action.action) { case "bookmark": if (e.ctrlKey || e.metaKey) { diff --git a/src/newtab.html b/src/newtab.html index 3086923..75cfec9 100644 --- a/src/newtab.html +++ b/src/newtab.html @@ -11,7 +11,7 @@ - + diff --git a/src/newtab.js b/src/newtab.js new file mode 100644 index 0000000..d1c64b9 --- /dev/null +++ b/src/newtab.js @@ -0,0 +1 @@ +window.__inNewTab__ = true;