0

I'm trying to create a simple chrome extension, to change the uploaded youtube title text to upper case or lowercase according to the selection from user.

App flow: https://www.youtube.com/upload -> Upload Video -> Prompt appears to provide video details (title, description etc)

Here is where I need to change the title text. Using the console on chrome with inspect I am able to get the text with document.querySelector('[aria-label="Add a title that describes your video]').firstChild)

ContentScripts.js

chrome.runtime.sendMessage(null, text, (response) => {
    console.log("I'm from the send response function: " + response)

    console.log("Title", document.querySelector('[aria-label="Add a title that describes your video"]').firstChild)
    console.log("Description", document.querySelector('[aria-label="Tell viewers about your video"]').firstChild)

    // chrome.storage.local.set({
    //     title: document.querySelector('[aria-label="Add a title that describes your video"]').firstChild),
    // })
})

Manifest.json

"permissions": ["storage", "tabs","contextMenus", "search", "tts", "webRequest"],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["contentScript.js"],
      "run_at": "document_end"
    }
  ]

The following error message appears on contentScripts.js: Error handling response: TypeError: Cannot read properties of null (reading 'firstChild'). Apparently, the call is being made before the element is set on the DOM. On content_scripts manifest, I have "run_at": "document_end", so it should run at the end. Based on the network calls, it seems to be https://studio.youtube.com/youtubei/v1/creator/get_creator_videos?* contains the title and description values. Maybe I can wait for that POST call to be made then get the value.. or maybe there is a simpler way of achieving this?

https://developer.chrome.com/docs/extensions/reference/webRequest/

CookieMonster
  • 241
  • 1
  • 9
  • 26
  • You need to wait for the element to appear. You can use MutationObserver for that. See also [How to detect page navigation on YouTube and modify its appearance seamlessly?](https://stackoverflow.com/q/34077641) – wOxxOm Sep 25 '22 at 22:26
  • The title was rewritten by detecting the addition of **DIV#label-help-tooltip.style-scope.ytcp-form-input-container** with **MutationObserver**. But it rolls back. Even if it is changed by **executeScript**, it will be rolled back. It seems to roll back every 10 seconds. – Norio Yamamoto Oct 20 '22 at 02:38

0 Answers0