Z
Z
ze72018-05-01 10:17:23
JavaScript
ze7, 2018-05-01 10:17:23

How to determine what kind of object it is from a VKontakte link?

How to determine what kind of object it is from a VKontakte link: a photo, a post on the wall, a comment on a post, etc.?
Links are taken from the browser. Opened a photo or product or post on the wall. You copy the link and you need to get an object from it. For example, "photo1_456315566":

const obj = { ownerId:1, objectId:456315566, type:'photo'}

https://vk.com/durov?z=photo1_456315566%2Falbum1_00%2Frev

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Sokolov, 2018-05-01
@ze7

Gather more link options and you'll have a guess! ; )
From the documentation for messages.send():

photo — фотография;
video — видеозапись;
audio — аудиозапись;
doc — документ;
wall — запись на стене;
market — товар.

Upd. you can do it with a regex:
function parseVkLink(link) {
  var re = new RegExp('(photo|video|audio|doc|wall|market)(\\d+)_(\\d+)');
  var matches = link.match(re);
  if(matches) {
    return {
      type: matches[1],
      ownerId: matches[2],
      objectId: matches[3],
    };
  } else {
    return false;
  }
}

var test = 'https://vk.com/durov?z=photo1_456315566%2Falbum1_00%2Frev';
JSON.stringify( parseVkLink(test) ) // {"type":"photo","ownerId":"1","objectId":"456315566"}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question