0

I am new to Chrome extensions and trying to create a simple code to insert some text into a html input field or text area value from a dropdown menu or right clicking.

But receiving the following error.

Error in event handler: TypeError: Cannot set property 'value' of null

However this works ok if i use the console , what am I missing here?

msg_cnt_cnt is the ID of the HTML of the box and the only box on the page

Here is my code :script.js

function past_to_box() {

var text =  'Hello world';
document.getElementById("msg_cnt_cnt").value = text;

}


chrome.contextMenus.create({
  title: "Dispatched", 
  contexts:["all"], 
  onclick: past_to_box
});

manifest

{
  "name": "Getting Started Example",
  "version": "1.0",
  "description": "Build an Extension!",
  "permissions": ["activeTab", "declarativeContent", "storage","tabs","contextMenus"],
  "background": { 
      "scripts": ["script.js"]
    },
  "manifest_version": 2
}
user34416
  • 21
  • 4
  • The background script runs in a special, separate, and hidden background page [with its own console](https://stackoverflow.com/a/10258029). To access a web page you need a content script. See the [architecture intro](https://developer.chrome.com/extensions/overview#arch). Also, it's usually much better to use document.execCommand('insertText', false, 'your text') - you can combine it with chrome.tabs.executeScript({code: 'your js code'}) – wOxxOm Mar 14 '19 at 09:22
  • Possible duplicate of [How to access the webpage DOM rather than the extension page DOM?](https://stackoverflow.com/questions/4532236/how-to-access-the-webpage-dom-rather-than-the-extension-page-dom) – wOxxOm Mar 14 '19 at 09:25
  • Yes looking at the content_script to see how this works.. – user34416 Mar 14 '19 at 09:37
  • I'm getting this as well when I login to my own app via Facebook OAuth. The console error points me to http://fburl.com/debugjs but my antivirus blocks this URL due to suspected phishing. – coder.in.me May 16 '19 at 14:01

0 Answers0