0

I want to make an extension in chrome which stops youtube video at particular time and add a custom button below the video. I can pause the video but stuck at adding a button at the bottom of the video. Can anyone help me with that. It's been now 5 hours I am trying :( but cannot append a button at the bottom of the video.

this is my content.js file

chrome.runtime.onMessage.addListener (
  function(request, sender, sendResponse) {
    if (request.message === "toggle_video_state") {
        var video = document.getElementsByTagName("video")[0];
      if (video) {
        if (video.paused) {
            video.play();
              sendResponse({paused: false, tabId: request.tabId});
              
        } else {
            video.pause();
            sendResponse({paused: true, tabId: request.tabId});
        }
      } else {
        sendResponse({error: 'No video object found'});
      }
    }
    if (request.message === "toggle_video_state") {
      var video = document.getElementsByTagName("video")[0];
      video.pause();
      sendResponse({paused: true, tabId: request.tabId});
    }
  }
);
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if (request.requested == "createDiv"){
            //Code to create the div
            sendResponse({confirmation: "Successfully created div"});
        }   

});

and this is my popup.js file

var matchUrl = '*://www.youtube.com/watch?v*';
var queryInfo = {url: matchUrl};
chrome.tabs.query(queryInfo, function(tabs) {
    var cl = $('#content-list');
    tabs.forEach(function(tab) {
        var imageClass = tab.audible ? 'pauseImage' : 'playImage';
        var x = `
            <div class="item" id=${tab.id}>
                <div class="tabimage" id="playpause${tab.id}">
                    <img class="${imageClass}" id="playImage${tab.id}" border="0">
                </div>
                <div class="tabInfo" id="jump${tab.id}">
                    <div class="close" id="close${tab.id}" title="close tab"></div>
                    <div class="title" id="title${tab.id}">${tab.title}</div>
                    <div class="url" id="url${tab.id}">${tab.url}</div>
                </div>
            </div>
        `
        cl.append($(x));
        setTimeout(function() {
        chrome.tabs.sendMessage(tab.id, {message: 'toggle_video_state', tabId: tab.id}, function(response) {
                console.log('response: ' + response);
                if (response.error) {
                    console.log('No video found in tab');
                } else {
                    if (response.paused) {
                        $('#playpause' + response.tabId + ' img').attr('class', 'playImage');
                    } else {
                        $('#playpause' + response.tabId + ' img').attr('class', 'pauseImage');
                    }
                }
            });
    }, 10000);
        $('#playpause'+tab.id).on('click', {tabId: tab.id}, function(event) {
            console.log('Clicked tab with event state ', event.data.tabId);
            chrome.tabs.sendMessage(event.data.tabId, {message: 'toggle_video_state', tabId: event.data.tabId}, function(response) {
                console.log('response: ' + response);
                if (response.error) {
                    console.log('No video found in tab');
                } else {
                    if (response.paused) {
                        $('#playpause' + response.tabId + ' img').attr('class', 'playImage');
                    } else {
                        $('#playpause' + response.tabId + ' img').attr('class', 'pauseImage');
                    }
                }
            });
        });

        $('#jump'+tab.id).on('click', {tabId: tab.id, windowId: tab.windowId}, function(event) {
            console.log('Clicked tab with event state ', event.data.tabId);
            chrome.windows.update(event.data.windowId, {focused: true});
            chrome.tabs.update(event.data.tabId, {active: true});
        });

        $('#close'+tab.id).on('click', {tabId: tab.id}, function(event) {
            console.log('Clicked tab with event state ', event.data.tabId);
            chrome.tabs.remove(event.data.tabId);
        });
    });
    if (tabs.length == 0) {
        var x = `
            <div style="text-align: center">No active youtube tabs</div>
        `
        $('body').attr('style', 'margin: 0; padding: 0');
        $('html').attr('style', 'margin: 0; padding: 0');
        cl.append($(x));
    }
});

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  var re = /www\.youtube\.com/;
  if (re.test(tab.url) && changeInfo.title) {
    document.getElementById("title" + tabId).textContent = changeInfo.title;
    document.getElementById("url" + tabId).textContent = tab.url;
  }
});
  • It's unclear where is the code that you need help with. It's also unclear why adding a button is a problem. – wOxxOm Oct 19 '20 at 12:02
  • that's what I am trying to do, searched every where don't know how to do it. where to put the button and make it visible on the youtube, not on my plugin page. – RAJA ONCE AGAIN Oct 19 '20 at 12:11
  • [How to access the webpage DOM rather than the extension page DOM?](https://stackoverflow.com/a/4532567) – wOxxOm Oct 19 '20 at 12:14
  • I have tried that approach not working. Can you please write some sample? Or refer any tutorial? – RAJA ONCE AGAIN Oct 19 '20 at 13:21
  • It's the correct and the only approach. Show the code that you've tried. – wOxxOm Oct 19 '20 at 13:30

0 Answers0