A chrome extension that renames Dj Grauchi's Mixes and any other The Good Company Mixes on Youtube

Overview

DJ Grauchi Chrome Extension

The Problem

All mixes under The Good Company youtube channel are not well named. There is no way to tell whether a video is a HipHop/Afrobeats/Reggae/Pop mix. All you can do is start listening and cross your fingers and hope you'll like it.

The Solution

A chrome extension that renames all of the mixes appropriately. This way, if you are in the mood for some Amapiano, you don't have to go through 7 videos just to find the perfect one.

How the solution works

Simple, sort of. I know you must be wondering, "Who is renaming the titles?", good question. I am the one renaming the titles (as of the first solution). The way it works is, When you click on a video, I make sure it is a video under "The Good Company", then I get the video-ID(watch id) that uniquely identifies a YT video. I then use this ID to get all the comments for that video, find my comment from the list and then set my comment as the video title.

[log 1] Getting my (Steve Kibuika's) comment was easy and it worked very well. But I needed a solution that could be easily adopted by other users.

Solution: Fetch the current user's username then use that username to get the user's comment and use that as the title.
This sounds good and it made sense. But it came with a new problem. I could not access the cuurent user's username since the username exists in a popup. 
The issue was that, popups do not exist in the DOM unless they are clicked (opened). So I had to hack a solution that would add the popup to the DOM by clicking it to open and then clicking it to close.


document.querySelector("ytd-popup-container").style['display'] = "none"; // hides the popup
document.getElementById("avatar-btn")?.click() // clicks the avatar to open the pop up, this way it will be added to the DOM without being visible
document.getElementById("avatar-btn")?.click()
userName = document.querySelector("#account-name")?.innerText
document.querySelector("ytd-popup-container").style['display'] = "block";


This block of code helped me achieve this effect.

- Clicks the popup twice to show and hide and then from there I can access the username.

It worked!!

Bottlenecks

  • The main idea is to rename Grauchi's mixes so you don't have to click a video. This means, I need to design a script that will run on the main youtube page and not just when a video is clicked. I have not put much thought into this, I am hoping to get one of those eureka moments.

[update] - I learn't of Mutation Observer that would listen to DOM mutations and run a script that would find The Good Company KE's videos, fetch comments, look for the user's comment and update it to the title

The Mutation Observer keeps track of changes in the DOM. In this case, the change we want to keep track of is the list of "child nodes" (video items) that are in the DOM. Ideally, when you are scrolling down, the number of child nodes is increasing and it is important to keep track of that. This way, when new nodes (video items) are added to the DOM, we can check for The Good Company KE's videos and find the title we want to replace the title with.

This was the most fascinating thing in this project. It was a very spot-on solution that put everything together and allowed things to work in a way I would not have anticipated.

I even wrote about it in my blog - MutationObserver saved my weekend build

The atomics of Chrome extensions

Chrome extensions rely on the manifest.json file. This is essentially what tells the extension which pages to work on and which scripts to run.

{
  "manifest_version": 3,
  "name": "DJ Grauchi",
  "version": "1.0.0",
  "description": "An extension that renames The Good Company KE's mixes according to the comments you add to the videos.",
  "content_scripts": [
    {
      "matches": ["https://www.youtube.com/*"],
      "js": ["js/content.js"],
      "all_frames": true,
      "run_at": "document_idle"
    },
    {
      "matches": ["https://www.youtube.com/watch*"],
      "js": ["js/innerContent.js"],
      "all_frames": true,
      "run_at": "document_end"
    }
  ],
  "icons": {
    "16": "icon16.png",
    "32": "icon32.png",
    "48": "icon48.png",
    "128": "icon128.png"
  }
}

content_scripts is where everything lies. matches defines the urls in which the extension will run on, in this case, it only works on Youtube. js points to the script to be executed. manifest_version has to be 3 if you are working with the latest chrome versions.

js/content.js is the main script. Here, the only thing you'll need is a Google API with Youtube Data API enabled.

js/content.js runs on the Youtube homepage while js/innerContent.js runs on the inner Youtube video page (this script is however not properly loading at the moment, if you can, please create a PR 😄 )

The run_at property defines when the script is executed. Ideally, we want our scripts to be executed when the page document has loaded. However, since we are making use of Mutation Observers, it doesn't matter much when our script is executed.

You can learn more about content_scripts here

Running the project

Once you've cloned the project, get a Google API with Youtube Data API enabled and replace it on the url requests. To run it on the browser, open manage extensions on chrome, make sure Developer mode is on, click on Load Unpacked and upload the whole project directory. Open Youtube and viola!!! It should work.

To learn more about developing Chrome Extensions, read their very good docs here

Fancy optional things

I find it fun to add "cute" features to my code, simply because I want to enjoy reading the code. For this project, the cute feature was useState.

This is a "hook" in React that provides a state value (state being a snapshot of data at a particular moment) and a setter function which is used to update the state.

Here, I decided to use this concept to store the username, this way, instead of having a variable that may need to be mutated in the course of the script execution, I can just access the username from the state value and update it using the setter function.

function useState(initialState) {
  let _val = initialState;
  const state = () => _val;
  const setState = newVal => {
    _val = newVal;
  };
  return [state, setState];
}

This is how I wrote that useState function. It is not exactly how it is in React but this is a much simpler way to write it especially when you only want to keep track of a single state value. It uses the concept of closures to keep a reference to the value.

A couple of things to note:

- The returned methods are a getter(state) and a setter(setState)
- This will only work with a single state value, for multiple values, you need to make use of arrays and indexes 
- This problem could have been better solved by the Storage API in chrome (a possible PR) 

In case you need me, dm me on twitter or email me [email protected]

You might also like...

A Chrome Extension for Bionic Reading on ANY website!

A Chrome Extension for Bionic Reading on ANY website!

bionic-reading A Chrome Extension for Bionic Reading on ANY website! How it works Below is a screenshot demonstrating how the extension works by boldi

Jan 4, 2023

A javascript based whatsapp bot for downloading and sending media from youtube and facebook in different formats alongwith couple of other features.

Whatsmazan Available Features Downlaod youtube mp4 Video and send Downlaod youtube mp3 audio and send Search something from youtube Downlaod facebook

Oct 30, 2022

a VS Code Extension for Easily Localize any blade/php text in any Laravel project.

a VS Code Extension for Easily Localize any blade/php text in any Laravel project.

Laravel Easy Localize a VS Code Extension for Easily Localize any blade/php text in any Laravel project. Features Custom array key for each translatio

Oct 31, 2022

This project is used to extract media from various posting platfroms like Twitter, Reddit, Pixiv, Youtube and many other

Social-Picker-API This project is used to extract media from various posting platfroms like Twitter, Reddit, Pixiv, Youtube and many others. It's writ

Nov 29, 2022

A fast and powerful http toolkit that take a list of domains to find active domains and other information such as status-code, title, response-time , server, content-type and many other

A fast and powerful http toolkit that take a list of domains to find active domains and other information such as status-code, title, response-time , server, content-type and many other

HTTPFY curently in beta so you may see problems. Please open a Issue on GitHub and report them! A Incredible fast and Powerful HTTP toolkit Report Bug

Dec 22, 2022

A web application for a company that provides commercial and scientific space travel services. The application will allow users to book rockets and join selected space missions.

A web application for a company that provides commercial and scientific space travel services. The application will allow users to book rockets and join selected space missions.

Space Travelers A web application for a company that provides commercial and scientific space travel services. The application will allow users to boo

Apr 6, 2022

This a web application for a company that provides commercial and scientific space travel services. The application will allow users to book rockets, dragons and join selected space missions.

Space Travelers' Hub In this project, we have worked with the real live data from the SpaceX API. Our task was to build a web application for a compan

Oct 31, 2022

This project is a web application for a company that provides commercial and scientific space travel services

This project is a web application for a company that provides commercial and scientific space travel services

Space Traveler's Hub This project is a web application for a company that provides commercial and scientific space travel services.

Jun 8, 2022
Comments
  • Welcome!

    Welcome!

    If you are seeing this then it's probably because you like this project. So feel free to raise an issue, a feature request or need help understanding the code.

    hakuna matata 
    opened by kibuikaCodes 0
Owner
Steve Kibuika
Steve Kibuika
Hackathon for Social Good 2022 and use your superpowers to create a solution for the social good.

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Laura Diaz 3 Jun 27, 2022
A Very Good Documentation Site created by the Very Good Ventures Team 🦄

Very Good Docs Site Developed with ?? by Very Good Ventures ?? A Very Good Docs Site created by the Very Good Ventures Team. Generated by the Very Goo

Very Good Open Source 8 Nov 2, 2022
Eth-explorers-extension - Chrome extension to open Ethereum addresses & transaction hash from any page on popular explorers + dashboards

eth-explorers-extension(s) This repository contains two folders with two extensions that work for address and transactions respectively. 1. eth-addres

Apoorv Lathey 71 Jan 6, 2023
This package generates a unique ID/String for different browsers. Like chrome, Firefox and any other browsers which supports canvas and audio Fingerprinting.

Broprint.js The world's easiest, smallest and powerful visitor identifier for browsers. This package generates a unique ID/String for different browse

Rajesh Royal 68 Dec 25, 2022
batList- chrome extension for YouTube

batList A watchList extension for YouTube Live Link : batList What exactly it does? It's basically like a watch later or Playlist feature from YouTube

Zuber Dunge (he/him) 8 Nov 22, 2022
Chrome Extension to learn English through subtitles while watching YouTube.

Super-Subtitles Chrome Extension to learn English through Subtitles while watching YouTube How does it helps Non-native English speakers often tend to

null 6 Nov 11, 2022
🍃 Create soothing focus mixes

Groovi ?? Create soothing concentration mixes Branding/Design Board · Demo ?? Quickstart Run the website locally git clone https://github.com/harshhhd

Harsh Singh 21 Nov 29, 2022
A chrome / firefox extension to draw on any webpage with tldraw

tldrawe A chrome / firefox extension to draw on any webpage with tldraw. Development From the root folder: Run yarn to install dependencies. Run yarn

Nimesh Nayaju 54 Jan 6, 2023