Skip to content

Commit

Permalink
heavy handed ui to cut down on abandoned new tab pages
Browse files Browse the repository at this point in the history
  • Loading branch information
altintx committed Sep 8, 2024
1 parent 42bed84 commit 0c2625a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 16 deletions.
9 changes: 9 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"scripting",
"background",
"storage",
"notifications",
"history"
],
"omnibox": {
Expand All @@ -21,6 +22,14 @@
"background": {
"service_worker": "dist/background.js"
},
"commands": {
"launch_omnibox": {
"suggested_key": {
"default": "Ctrl+Shift+P"
},
"description": "Launch omnibox with >"
}
},
"content_scripts": [
{
"matches": ["<all_urls>"],
Expand Down
55 changes: 44 additions & 11 deletions src/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
afterPageLoadHandler({
message,
sender,
tabData
tabData,
history,
});
} else if (message.event === 'beforeunload') {
beforeUnloadHandler({ message, sender, tabData, history });
beforeUnloadHandler({ sender, tabData, history });
}
});

Expand Down Expand Up @@ -63,6 +64,8 @@ chrome.runtime.onSuspend.addListener(() => {

let search = "";

const newTabs: chrome.tabs.Tab[] = [];

// omnibox event listener for input changes (provide suggestions)
chrome.omnibox.onInputChanged.addListener(async (input, suggest) => {
search = input;
Expand Down Expand Up @@ -92,29 +95,59 @@ function highlightInPage(tab: chrome.tabs.Tab, search: string) {

// omnibox event listener for when a user selects a suggestion or presses Enter
chrome.omnibox.onInputEntered.addListener((input, disposition) => {
console.info("cmdp special behavior starting");
const url = input.startsWith('http') ? input : `https://${input}`;

// Search for an open tab with the same URL
chrome.tabs.query({}, (tabs) => {
chrome.tabs.query({}, async (tabs) => {
const currentTab = tabs.find((tab) => tab.active);
const existingTab = tabs.find((tab) => tab.url && tab.url.includes(input));
console.info("is there an existing tab for the page being loaded? it is", existingTab);
const highlightCallback = (tab: chrome.tabs.Tab | undefined) => {
tab && highlightInPage(tab, search);
}
};

// Detect if the current tab is the "New Tab" page (chrome://newtab/)
console.info("what is the current tab?", currentTab);
const currentTabIsNewTab = currentTab?.url === 'chrome://newtab/';
console.info("am i sitting at a new tab page?", currentTabIsNewTab);


// Check if we found an existing tab with the same URL
if (existingTab) {
if (disposition === 'currentTab') {
// Switch to the existing tab and close the current (new tab) if necessary
console.info("switching to existing tab");
chrome.tabs.update(existingTab.id!, { active: true }, highlightCallback);
if (currentTabIsNewTab && currentTab?.id) {
console.info("closing current tab");
await chrome.tabs.remove(currentTab.id);
} else {
console.info("not closing current tab");
}
} else {
// If current tab is not a real page, load the new URL in the same tab
chrome.tabs.create({ url }, highlightCallback);
console.info("creating new tab");
}
} else {
// Tab is not open, handle based on disposition
if (disposition === 'currentTab') {
// Open the URL in the current tab
chrome.tabs.update({ url }, highlightCallback);
} else {
// Open the URL in a new window
if(currentTab) {
console.info("create new tab");
chrome.tabs.create({ url }, highlightCallback);
} else {
// Tab is not already open, handle based on disposition
chrome.tabs.update({ url }, highlightCallback);
console.info("updating tab");
}
}
});
});


chrome.commands.onCommand.addListener((command) => {
chrome.notifications.create({
type: 'basic',
iconUrl: 'assets/src/assets/icon128.png',
title: 'Press Command/Control L to focus the address bar',
message: `Start your search with a > character`,
});
});
23 changes: 20 additions & 3 deletions src/server/after-page-load-handler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { CmdPDomContentReadyEvent } from "../types/events/after-page-load";
import { HistoryEntry } from "../types/history";
import { TabStore } from "../types/tab-state";

export function afterPageLoadHandler({
message,
sender,
tabData
tabData,
history,
}: {
message: CmdPDomContentReadyEvent,
sender: chrome.runtime.MessageSender,
tabData: TabStore
tabData: TabStore,
history: HistoryEntry[],
}) {
const windowId = sender.tab?.windowId?.toString(),
tabId = sender.tab?.id?.toString(),
Expand All @@ -29,5 +32,19 @@ export function afterPageLoadHandler({
tabId: tabId,
windowId: windowId,
}
console.info("server says updated tabData", tabData);
const existingHistory = history.find(entry => entry.page.url === sender.tab?.url);
if(existingHistory) {
existingHistory.count++;
existingHistory.updatedAt = now;
delete existingHistory.closedAt;
} else {
history.push({
page: {
...tabData[tabId]?.page
},
count: 1,
createdAt: now,
updatedAt: now
});
}
}
3 changes: 1 addition & 2 deletions src/server/before-unload-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { HistoryEntry } from "../types/history";
import { TabStore } from "../types/tab-state";

// TODO: before unload is not neccessarily a close. this is wrong.
export function beforeUnloadHandler({ message, sender, tabData, history }: {
message: CmdPBeforeUnloadEvent,
export function beforeUnloadHandler({ sender, tabData, history }: {
sender: chrome.runtime.MessageSender
tabData: TabStore,
history: HistoryEntry[]
Expand Down

0 comments on commit 0c2625a

Please sign in to comment.