home

Google Result - Letter Focus

press 'M' to go to 1st question "M"ain question

adjust script accordinly to allow 'M' to go through multiple question

 Add numFocus() to each code or #. in answer 'IF'
question pre.copy-code.drop-question or whatever is focused && answer is showing. 😀

  numFocus() to each question

Notes

watch tutorial on filter() and trim()

This project is saved in my ChromeExtensions folder

type

chrome://extensions/

1.

i'm trying make an extension to add to chrome that will apply my letter focus scripts, above are the html

want to target, the divs with a role="heading" are the main search results on googles, the a tage with the
data-agch="HJ3bqe" are the heading sub results, i would love to target both so that if i press the first letter in one
of the results the focus goes to the result, as you can see the first div with role "heading" and the span inside
containing the text Stack Overflow... is the first result in the google search result , if i press the "S" I would love
the focus to go there then if i press "k", it will then go to Knowing Sharing Platform, if press "r" the focus will go
to Rest New..., basically everytime i press a letter on the keyboard the focus will go to the first element with the
text that has that first letter, then if the same letter is pressed again it will go to the next element containing that
letter as its first character, but, if shift and that letter is pressed it will go to the previous element and cycle
through the page, for example if the page first opens and shift + "s" is pressed it shouldn't go to Stack Overflow... it
should go to the bottom of the page to the last element with that letter, and continue decending through the elements up
if shift + that letter is pressed, how can i write a script for this and make it an extension for chrome



Chat Gpt Said:

To implement this Chrome extension, follow these steps:

1. Create the Manifest File (manifest.json)

This defines the extension's permissions and scripts.

{
  "manifest_version": 3,
  "name": "Google Search Letter Focus",
  "version": "1.0",
  "description": "Navigate Google search results using the first letter keys.",
  "permissions": ["scripting", "activeTab"],
  "host_permissions": ["https://www.google.com/*"],
  "background": {
      "service_worker": "background.js"
  },
  "content_scripts": [
      {
          "matches": ["https://www.google.com/*"],
          "js": ["content.js"]
      }
  ],
  "action": {
    "default_popup": "popup.html"
  }
}

2. Create the Content Script (content.js)

This script will handle key presses and focus the relevant search results.

html, body {
height: 100%;
margin: 0;
}

.container {
height: 100vh; /* Ensures it fills the viewport */
display: flex; /* Optional: for centering content */
align-items: center;
justify-content: center;
}        

2. JavaScript Approach (Handles Dynamic Changes)

If the element’s height needs to dynamically adjust when the window resizes:

let searchResults = [];
let currentIndexes = {};

document.addEventListener("keydown", (event) => {
if (event.metaKey || event.ctrlKey || event.altKey) return;

let isShift = event.shiftKey;
let key = event.key.toLowerCase();
if (key.length !== 1 || !/[a-z]/.test(key)) return;

// Get search result elements if not already fetched
if (searchResults.length === 0) {
searchResults = [...document.querySelectorAll('div[role="heading"] span, a[data-agch="HJ3bqe"]')];
}

let matchingResults = searchResults.filter(el => 
el.innerText.trim().toLowerCase().startsWith(key)
);

if (matchingResults.length === 0) return;

// Initialize tracking index if not set
if (!(key in currentIndexes)) {
currentIndexes[key] = isShift ? matchingResults.length - 1 : 0;
} else {
currentIndexes[key] += isShift ? -1 : 1;
}

// Cycle through matches
if (currentIndexes[key] >= matchingResults.length) currentIndexes[key] = 0;
if (currentIndexes[key] < 0) currentIndexes[key] = matchingResults.length - 1;

let target = matchingResults[currentIndexes[key]];
target.scrollIntoView({ behavior: "smooth", block: "center" });
target.focus();
});

3. Add a Background Script (background.js)

This ensures the content script runs when needed.

chrome.runtime.onInstalled.addListener(() => {
console.log("Google Search Letter Focus Extension Installed");
});

4. Create a Popup (Optional)

Create popup.html for an options menu.

<!DOCTYPE html>
<html>
<head>
<title>Google Search Letter Focus</title>
</head>
<body>
<h2>Letter Navigation Active</h2>
<p>Use letter keys to navigate search results.</p>
</body>
</html>
                            

5. Load the Extension into Chrome

Open

chrome://extensions/

in Chrome.

Enable Developer Mode (top-right toggle). Click Load Unpacked and select your extension folder. This script will let you cycle through search results using letters. Let me know if you need refinements! 🚀

Add question 2 from Question 2.txt in this directory
HERE X
to the left, have question side alternate down the page