SteemIt: Javascript Function to Get Original Post from Comment's PermLink - 如何从评论的 PermLink 得到原文的链接?

in #cn7 years ago (edited)


Image Credit: Pixabay.com

@nationalpark suggests me add 'original post URL' in my tool: SteemIt Tool to Recover Deleted Comments/Posts , and here is the way that a permlink for a comment is organized.

For example,

re-tvb-re-justyy-re-tvb-45qr3w-20171011t144205534z

If we split the permlink string by '-', the last group is obviously the timestamp which we can discard. The rest are in the form of 're-author' where the last group is the original author.

We can split the string and handle the logics straightforward, but the best way is to use the regular expression to match this pattern. The following is the Javascript script function that does the job.

var restore = function(url) {
    var pat = /(re-\w+-)*((\w+\-)*)/g;
    var my = pat.exec(url);
    if (my[1] && my[2]) {       
        var author = my[1].split('-')[1];
        var link = my[2].slice(0, -1);
        return 'https://steemit.com/@' + author + '/' + link;
    }
    return null;
}

For example,

console.log("re-tvb-re-justyy-re-tvb-45qr3w-20171011t144205534z");

The original steemit post URL is given:
"https://steemit.com/@tvb/45qr3w"


Image Credit: Pixabay.com


昨天, @nationalpark 建议在我那工具: Steemit 查看被删除的评论 里加上原文的链接,并且告诉我如何从评论的 permlink 得到原文的链接,比如:

re-tvb-re-justyy-re-tvb-45qr3w-20171011t144205534z

如果把字符串以 连接符 - 分组,那么最后一个显示是时间 (timestamp), 评论会以 re-作者- 这种方式连接,而最后一组re-tvb 中表示 tvb 是作者, 然后 justyy 回复了,然后 tvb 又回复了。

我们可以写一个函数用 split 分组然后再依次处理,但是有点麻烦,最简单的方法就是按正则表达式,\w 匹配字母, () 取得分组, * 表示匹配0个或多个, + 匹配1个或多个.

Javascript 来写就是:

var restore = function(url) {
    var pat = /(re-\w+-)*((\w+\-)*)/g;
    var my = pat.exec(url);
    if (my[1] && my[2]) {       
        var author = my[1].split('-')[1];
        var link = my[2].slice(0, -1);
        return 'https://steemit.com/@' + author + '/' + link;
    }
    return null;
}

比如:

console.log("re-tvb-re-justyy-re-tvb-45qr3w-20171011t144205534z");

输出STEEMIT原文链接:
"https://steemit.com/@tvb/45qr3w"

@justyyhttps://justyy.com 的博主,在 @tumutanzi 大哥 的介绍下加入 STEEMIT,写些帖子挣些小钱养家糊口。


@justyy 也是CN 区的点赞机器人,对优质内容点赞,只要代理给 @justyy 每天收利息(年化率14.6%)并能获得一次至少2倍(VP 200%+)的点赞,大鱼 @htliao 都加入了这个计划(530 SP)表示支持。

  1. cn区最低保障系统 上线了!
  2. cn区低保计划(鼓励新人)真的适合你么?

如何从评论的 PermLink 得到原文的链接(Javascript)?
Get Original Post from Comment’s PermLink
Steemit 在线工具和API接口
SteemIt Tools and APIs

Sort:  

我又变成了道具哈哈

姐,赶紧去睡觉,女人养颜美容是很重要的

@eileenbeach has voted on behalf of @minnowpond. If you would like to recieve upvotes from minnowponds team on all your posts, simply FOLLOW @minnowpond.

    To receive an upvote send 0.25 SBD to @minnowpond with your posts url as the memo
    To receive an reSteem send 0.75 SBD to @minnowpond with your posts url as the memo
    To receive an upvote and a reSteem send 1.00SBD to @minnowpond with your posts url as the memo

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.030
BTC 65185.94
ETH 2630.94
USDT 1.00
SBD 2.83