From 55d0057266bbd15e0cce21247a164e77d7242236 Mon Sep 17 00:00:00 2001 From: Folyd Date: Sun, 26 May 2024 17:02:53 -0700 Subject: [PATCH] Integrate omnibox search into manage page --- core | 2 +- extension/main.js | 74 +++++++++++++++++------------- extension/manage/js/index.js | 3 ++ extension/search/docs/crate-doc.js | 1 + manage/templates/index.html | 3 +- 5 files changed, 48 insertions(+), 35 deletions(-) diff --git a/core b/core index 81711bb1..5ae9f8ef 160000 --- a/core +++ b/core @@ -1 +1 @@ -Subproject commit 81711bb16bb4f3ff6d07dc81cdfdb918d3f4ac65 +Subproject commit 5ae9f8ef2dcfb3597f1deebe33ee0281bbb03e71 diff --git a/extension/main.js b/extension/main.js index 7bbc0dc2..228e1a87 100644 --- a/extension/main.js +++ b/extension/main.js @@ -37,7 +37,7 @@ function getPlatformOs() { }); } -async function start() { +async function start(el) { // All dynamic setting items. Those items will been updated // in chrome.storage.onchange listener callback. let isOfflineMode = await settings.isOfflineMode; @@ -110,7 +110,7 @@ async function start() { let rustcSearcher = new RustcSearch(); const defaultSuggestion = `Search std ${c.match("docs")}, external ${c.match("docs")} (~,@), ${c.match("crates")} (!), ${c.match("attributes")} (#), ${c.match("books")} (%), clippy ${c.match("lints")} (>), and ${c.match("error codes")}, etc in your address bar instantly!`; - const omnibox = new Omnibox({ defaultSuggestion, maxSuggestionSize: c.omniboxPageSize() }); + const omnibox = new Omnibox({ el, defaultSuggestion, maxSuggestionSize: c.omniboxPageSize() }); let formatDoc = (index, doc) => { let content = doc.href; @@ -427,6 +427,40 @@ async function start() { omnibox.addNoCacheQueries("/", "!", "@", ":"); + // Skip following code if `el` provide, which mean this function run in webpage. + if (el) return; + + // DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE: + // https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049 + chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + switch (message.action) { + // Rustc:* action is exclusive to rustc docs event + case "rustc:check": { + sendResponse({ + version: rustcSearcher.version, + }); + break; + } + case "rustc:add": { + if (message.searchIndex) { + rustcSearcher.setSearchIndex(message.searchIndex); + rustcSearcher.setVersion(message.version); + sendResponse(true); + } else { + sendResponse(false); + } + break; + } + case "open-url": { + if (message.url) { + Omnibox.navigateToUrl(message.url); + } + break; + } + } + return true; + }); + chrome.storage.onChanged.addListener(changes => { for (let [key, { oldValue, newValue }] of Object.entries(changes)) { console.log('storage key updated:', key); @@ -520,6 +554,10 @@ async function start() { } }); + await checkAutoUpdate(); +} + +async function checkAutoUpdate() { if (await settings.autoUpdate) { let version = await storage.getItem('auto-update-version'); let now = new Date(); @@ -532,37 +570,6 @@ async function start() { Omnibox.navigateToUrl(INDEX_UPDATE_URL); await storage.setItem('auto-update-version', `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}`); } - - // DO NOT USE ASYNC CALLBACK HERE, SEE THIS ISSUE: - // https://github.com/mozilla/webextension-polyfill/issues/130#issuecomment-918076049 - chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { - switch (message.action) { - // Rustc:* action is exclusive to rustc docs event - case "rustc:check": { - sendResponse({ - version: rustcSearcher.version, - }); - break; - } - case "rustc:add": { - if (message.searchIndex) { - rustcSearcher.setSearchIndex(message.searchIndex); - rustcSearcher.setVersion(message.version); - sendResponse(true); - } else { - sendResponse(false); - } - break; - } - case "open-url": { - if (message.url) { - Omnibox.navigateToUrl(message.url); - } - break; - } - } - return true; - }); } const chromeAction = chrome.action || chrome.browserAction; @@ -578,4 +585,5 @@ chrome.runtime.onInstalled.addListener(async ({ previousVersion, reason }) => { } }); + export { start }; diff --git a/extension/manage/js/index.js b/extension/manage/js/index.js index a873b956..6d947b94 100644 --- a/extension/manage/js/index.js +++ b/extension/manage/js/index.js @@ -1,5 +1,6 @@ import { STATS_PATTERNS } from "../../statistics.js"; import Statistics from "../../statistics.js"; +import { start } from "../../main.js"; const TYPE_OTHER = "other"; const CHART_COLOR = "rgba(249, 188, 45, 0.5)"; @@ -306,4 +307,6 @@ async function renderYearList() { const yearAgo = moment().startOf('day').subtract(1, 'year').valueOf(); await renderCharts(now, yearAgo); await renderYearList(); + + await start("#omnibox"); })(); diff --git a/extension/search/docs/crate-doc.js b/extension/search/docs/crate-doc.js index 5bdf8418..c2dcd44e 100644 --- a/extension/search/docs/crate-doc.js +++ b/extension/search/docs/crate-doc.js @@ -1,5 +1,6 @@ import { c } from "../../core/index.js"; import DocSearch from "./base.js"; +import CrateDocManager from "../../crate-manager.js"; // A DocSearch dedicated to a single crate based on the search-index. class SingleCrateDocSearch extends DocSearch { diff --git a/manage/templates/index.html b/manage/templates/index.html index 42336294..7b75eaf8 100644 --- a/manage/templates/index.html +++ b/manage/templates/index.html @@ -5,12 +5,13 @@ - + {% endblock head %} {% block content %} +