MCPcopy
hub / github.com/philc/vimium / focusOrLaunch

Function focusOrLaunch

background_scripts/marks.js:87–110  ·  view source on GitHub ↗
(markInfo, req)

Source from the content-addressed store, hash-verified

85// The tab we're trying to find no longer exists. We either find another tab with a matching URL and
86// use it, or we create a new tab.
87async function focusOrLaunch(markInfo, req) {
88 // If we're not going to be scrolling to a particular position in the tab, then we choose all tabs
89 // with a matching URL prefix. Otherwise, we require an exact match (because it doesn't make sense
90 // to scroll unless there's an exact URL match).
91 const markIsScrolled = markInfo.scrollX > 0 || markInfo.scrollY > 0;
92 const query = markIsScrolled ? markInfo.url : `${markInfo.url}*`;
93 const tabs = await chrome.tabs.query({ url: query });
94 if (tabs.length > 0) {
95 // There is at least one matching tab. Pick one and go to it.
96 const tab = await pickTab(tabs);
97 gotoPositionInTab(Object.assign(markInfo, { tabId: tab.id }));
98 } else {
99 // There is no existing matching tab. We'll have to create one.
100 TabOperations.openUrlInNewTab(
101 Object.assign(req, { url: getBaseUrl(markInfo.url) }),
102 (tab) => {
103 // Note. tabLoadedHandlers is defined in "main.js". The handler below will be called when
104 // the tab is loaded, its DOM is ready and it registers with the background page.
105 return tabLoadedHandlers[tab.id] = () =>
106 gotoPositionInTab(Object.assign(markInfo, { tabId: tab.id }));
107 },
108 );
109 }
110}
111
112// Given a list of tabs candidate tabs, pick one. Prefer tabs in the current window and tabs with
113// shorter (matching) URLs.

Callers 1

gotoFunction · 0.85

Calls 3

pickTabFunction · 0.85
gotoPositionInTabFunction · 0.85
getBaseUrlFunction · 0.85

Tested by

no test coverage detected