-1
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"

Xardas105
  • 85
  • 13
  • Can you add more code? What are the contents of `url1`, 2 and 3? – Phiter Mar 13 '18 at 12:39
  • Let's say that they are just random youtube links. https://www.youtube.com/watch=dfgdr what I basically want to do is cut only the watch=* from everyarray – Xardas105 Mar 13 '18 at 12:52
  • @Xardas105 then state that in your question. I'll update my answer accordingly – ikdekker Mar 13 '18 at 12:56
  • Your error is because you are trying to `slice` a `string`. `slice` is an array function. – phuzi Mar 13 '18 at 13:24

2 Answers2

1

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

The exec function returns all the matches found based on the regex. For each so called "group", determined by '(' & ')' as seen around the .{11} another array entry will be created when matched.

In addition to those matches, the first element will always be the full match of the regex.

The \? will prevent the regex from seeing the '?' as a regex operator. Meaning it will match the literal '?' character.

ikdekker
  • 425
  • 3
  • 11
  • It worked with your code, thanks. Could you explain the code tho? Why exactly is this in array? "match[1]" Also why \? in the link? Is it to disgunish ? from another command? – Xardas105 Mar 13 '18 at 15:06
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, " "))
}
Phiter
  • 14,570
  • 14
  • 50
  • 84