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

Function prepareToActivateLinkHintsMode

background_scripts/main.js:523–597  ·  view source on GitHub ↗
(
    tabId,
    originatingFrameId,
    { modeIndex, requestedByHelpDialog, isExtensionPage },
  )

Source from the content-addressed store, hash-verified

521
522 // This is sent by the content script once the user issues the link hints command.
523 async prepareToActivateLinkHintsMode(
524 tabId,
525 originatingFrameId,
526 { modeIndex, requestedByHelpDialog, isExtensionPage },
527 ) {
528 const frameIds = await getFrameIdsForTab(tabId);
529 // If link hints was triggered on a Vimium extension page (like the vimium help dialog or
530 // options page), we cannot directly retrieve the frameIds for those pages using the
531 // getFrameIdsForTab. However, as a workaround, if those pages were the pages activating hints,
532 // their frameId is equal to originatingFrameId.
533 if (isExtensionPage && !frameIds.includes(originatingFrameId)) {
534 frameIds.push(originatingFrameId);
535 }
536 const timeout = 3000;
537 let promises = frameIds.map(async (frameId) => {
538 let promise = chrome.tabs.sendMessage(
539 tabId,
540 {
541 handler: "linkHintsMessage",
542 messageType: "getHintDescriptors",
543 modeIndex,
544 requestedByHelpDialog,
545 },
546 { frameId },
547 );
548
549 promise = Utils.promiseWithTimeout(promise, timeout)
550 .catch((error) => Utils.debugLog("Swallowed getHintDescriptors error:", error));
551
552 const descriptors = await promise;
553
554 return {
555 frameId,
556 descriptors,
557 };
558 });
559
560 const responses = (await Promise.all(promises))
561 .filter((r) => r.descriptors != null);
562
563 const frameIdToDescriptors = {};
564 for (const { frameId, descriptors } of responses) {
565 frameIdToDescriptors[frameId] = descriptors;
566 }
567
568 promises = responses.map(({ frameId }) => {
569 // Don't send this frame's own link hints back to it -- they're already stored in that frame's
570 // content script. At the time that we wrote this, this resulted in a 150% speedup for link
571 // busy sites like Reddit.
572 const outgoingFrameIdToHintDescriptors = Object.assign({}, frameIdToDescriptors);
573 delete outgoingFrameIdToHintDescriptors[frameId];
574 return chrome.tabs.sendMessage(
575 tabId,
576 {
577 handler: "linkHintsMessage",
578 messageType: "activateMode",
579 frameId: frameId,
580 originatingFrameId: originatingFrameId,

Callers

nothing calls this directly

Calls 3

getFrameIdsForTabFunction · 0.85
pushMethod · 0.45
filterMethod · 0.45

Tested by

no test coverage detected