Jquery .slice chars from array?

136
March 13, 2018, at 5:34 PM
var obj = $(".yt-simple-endpoint.inline-block.style-scope.ytd-thumbnail")
        console.log("slicing obj " + obj[0].slice(32, 43));

obj is a array that contains youtube links. It includes around 32 youtube links if you are at youtube.com main page. So what I am trying to do here is, choose obj[0]"First youtube url in that array and slice only the part that comes after watch?v=.

However when I try this I get this error message: "contentscript.js:137 Uncaught TypeError: obj[0].slice is not a function at contentscript.js:137"

Answer 1

As you mentioned in the comments, you want to extract the video url from youtube links.

To get querystring parameters from a url, you can use the solution provided in this answer.

Then, to get the v part of the url, do this:

var urls = [ 
  'https://www.youtube.com/watch?v=dQw4w9WgXcQ', 
  'https://www.youtube.com/watch?v=4fndeDfaWCg', 
  'https://www.youtube.com/watch?v=yPYZpwSpKmA' 
]; 
 
var firstLink = getParameterByName('v', urls[0]); 
 
document.write(firstLink); 
 
 
 
function getParameterByName(name, url) { 
  if (!url) { 
    url = window.location.href 
  } 
  name = name.replace(/[\[\]]/g, "\\$&"); 
  var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), 
    results = regex.exec(url); 
  if (!results) { 
    return null 
  } 
  if (!results[2]) { 
    return '' 
  } 
  return decodeURIComponent(results[2].replace(/\+/g, " ")) 
}

Answer 2

If you want to extract some characters from the first entry in the array, you could indeed use urls[0] to get the first entry from the array. You may want to verify its existance.

To extract some characters you can use substr. i.e. (watch id from YT link)

  var url = "https://www.youtube.com/watch?v=Oh3zMzmwsCA"; 
   
  var urls = [url]; 
  first_url = urls[0]; 
  var myRegexp = /watch\?v=(.{11})/g; 
  var match = myRegexp.exec(first_url); 
  console.log(match[1]); // abc

READ ALSO
Sending changes to a sql Database without refreshing the page ASP.net core 2.0

Sending changes to a sql Database without refreshing the page ASP.net core 2.0

I have a bunch of draggable divs and i need to save their positions in a sql database when they are dropped, but i dont want it to refresh the pageIm thinking of adding some ajax call when the div is dropped but i have no idea what that ajax call would look like

110
Using math.random for A/B testing

Using math.random for A/B testing

Currently I have setup a piece of code to use for A/B testing based on 50/50But now I want to rebuild it to a 50/25/25 setup

121
FinalCountdown.js stop event handler not working

FinalCountdown.js stop event handler not working

i am using the adminLTE template and within that I have a FinalCountdownjs timer as follows:

129
.addClass() jQuery how to specify the CSS

.addClass() jQuery how to specify the CSS

I have a div and I want to put a display none when I click on itI am using bootstrap4 and It has a css style called "d-none" that put display none to an element

132