0

I am trying to create a Chrome extension that changes the "What's on your mind?" message in the Twitter status update box, but I am stuck.

Here is my manifest:

{
"name":"Change Status Update",
"description":"Changes the What's On Your Mind Message",
"version":"1",
"manifest_version":2,
"content_scripts": [
    {
        "matches": ["https://twitter.com/*"],
        "run_at": "document_end",
        "js": ["myscript.js"],
        "all_frames": true
    }
  ],
"permissions": [
    "activeTab"
]
}

And here is my content script (myscript.js) file is:

document.getElementsByClassName("public-DraftEditorPlaceholder-inner").item(0).innerText = "Are you sure you want to post that?"

As you can see, it is a pretty simple extension.

Now, my problem is the content script works perfectly to change the Twitter status update message when I run it directly within the developer tools console in Chrome, but not when I run it within a content script in the Chrome extension.

In this case, I continuously get the error "Uncaught TypeError: Cannot set property 'textContent' of undefined."

Does anyone have any insight into this? I can't find any similar info from other stack overflow questions. Thank you!

  • The element doesn't exist when your script runs. You'll have to wait until Twitter has finished building the page. – Guy Incognito May 16 '20 at 17:03
  • That's why I said "run_at": "document_end." Should I say something else? – stovestove2 May 16 '20 at 17:11
  • That waits until the HTML page is loaded, but Twitter then generates the rest of the page dynamically with JavaScript. Unfortunately there's no simple hook for when that's done. – Guy Incognito May 16 '20 at 17:16
  • Hmm, I see. Is there a workaround (i.e., waiting a few seconds after the page loads, or something like that?) – stovestove2 May 16 '20 at 17:53

0 Answers0