4

I'm trying to figure out how to insert a predefined string on right clicking a input field (editable), usually i would use the document get element by id. However, since i have no way to determine what element a user clicks, on any website how do i go about doing this?

so far i have my manifest file

"permissions": [
    "contextMenus",
    "background",
    "https://ajax.googleapis.com/"
],

"background": {
    "scripts": ["scrippy.js"] 
}
}

(i have the rest of the required info as well.)

And my script file

// Create context menu and set to only on inputs
var type = ["editable"];
var scrippyMenu = chrome.contextMenus.create({"title": "Scrippy", 
"contexts": type});


// menu context
var menuContext = chrome.contextMenus.create({"title": "blabla", 
"parentId": scrippyMenu, "contexts": type});

var menuChild1 = chrome.contextMenus.create(
{"title": "time", "parentId": menuContext, "contexts": type, "onclick": 
genericOnClick});

function genericOnClick(info, tab) {

document.getElementById("Theunknownelement").textContent = 'This is added text';

}
octo-carrot
  • 240
  • 1
  • 11

1 Answers1

5

You have one fundamental misunderstanding: your background script can't modify the webpage, because document refers to the background page itself.

Take a look at the Architecture overview. After you do, you should know that you need a Content Script.

Once we've established that, your content script can independently track where the click happened - and that would contain a reference to the element. Then, when your background requests a modification, you know the last such element.

There is a very good question discussing your problem in great detail too.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • Hi, thanks for links and info. I looked at your example from that other answer however, I still cant seem to get it to work. i've updated my question could you take alook? i'm not a developer im just going by when I find on google and learning on the way so any help would be great! – octo-carrot May 13 '16 at 15:59
  • 1
    Please look at your question. Then to my answer. Does my answer still answer your question? No. Please don't do significant question rewrites after answers are given - ask a follow-up question separately. – Xan May 13 '16 at 16:21
  • Ok, i get your point, so should i create a new question? i dont use this site that often. – octo-carrot May 13 '16 at 17:15
  • Yes, create a new question, include a link to this one. I'll roll your question back (or you can do it yourself) and take a look at the new one. – Xan May 13 '16 at 17:16