home

ReactJS Tutorials

01

chat gpt question chatgpt.com

Make a manifest.json file

{
"manifest_version": 3,
"name": "Link and Button Focus",
"version": "1.0",
"description": "Focus on the first link or button that starts with the pressed letter and click it with Enter.",
"permissions": [
"activeTab"
],
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
    "js": ["content.js"]
    }
    ]
}

02

Create Background script

background.js it's main function is running the content.js Similiar to react's index.js running app.js

// background.js
chrome.runtime.onInstalled.addListener(() => {
    console.log("Extension Installed");
});

03

create content script

contains the functionality of focusing to letters,

// content.js
document.addEventListener('keydown', (event) => {
  // Check if a letter key is pressed
  if (event.key.length === 1 && event.key.match(/[a-z]/i)) {
    const pressedLetter = event.key.toLowerCase();
    
    // Find the first link or button that starts with the pressed letter
    const elements = document.querySelectorAll('a, button');
    for (let element of elements) {
      const text = element.textContent.trim().toLowerCase();
      if (text.startsWith(pressedLetter)) {
        element.focus();
        
        // Add event listener for Enter key
        const enterListener = (e) => {
          if (e.key === 'Enter') {
            element.click();
            document.removeEventListener('keydown', enterListener);
          }
        };
        
        document.addEventListener('keydown', enterListener);
        break;
      }
    }
  }
});

04

Navigate to

chrome://extensions/
  1. enable the Developer mode in upper right corner
  2. click the load unpack
  3. open the directory containing the:
    • manifest.json
    • background.js
    • content.js

files

05

Enable the extension, its working but make it better