Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
Context menu not working unless I have the extensions devtools open

Im trying to make a chrome extension that lets you add a highlighted piece of text into the DOM of the extension using the context menu. This piece of code works fine but only when I have the devtools from inspecting the extension open. Otherwise the context menu command does nothing. Any help would be appreciated thanks.

Javascript Code:

if (typeof (document) !== "undefined"){
    let wordList = []
    const ulEl = document.getElementById("ul-el")
    const CONTEXT_MENU_ID = "MY_CONTEXT_MENU"
    const localStorageStuff = JSON.parse(localStorage.getItem("text"))

    if (localStorageStuff){
        wordList = localStorageStuff
        console.log(wordList)
        render(wordList)
    }

    function render(words) {
        let renderWords = ""
        for (let i=0; i<words.length; i++) {
            renderWords += `
            <li>
                <a target='blank_' href='http://www.google.com/search?q=${words[i]}'>
                ${words[i]}
                </a>
            </li>
        `
        }
        ulEl.innerHTML = renderWords
    }

    function getword(info) {
        if (info.menuItemId !== CONTEXT_MENU_ID) {
            return;
        }
        console.log("Word " + info.selectionText + " was clicked.")
        wordList.push(info.selectionText)
        console.log(wordList)
        localStorage.setItem("text", JSON.stringify(wordList))
        render(wordList)
    }
    chrome.contextMenus.removeAll(function() {chrome.contextMenus.create({
    title: "Add log: %s", 
    contexts:["selection"], 
    id: CONTEXT_MENU_ID
    })})
    chrome.contextMenus.onClicked.addListener(getword)
}

manifest.json

{
    "name": "Memos",
    "version": "1.0",
    "manifest_version": 3,
    "action": {
        "default_popup": "index.html"
    },
    "permissions": [
      "contextMenus",
      "tabs",
      "storage"
    ],
    "background": { 
      "service_worker": "index.js"
    }
}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!