Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convertToValidEmbedCode #84

Open
5Mi opened this issue May 21, 2018 · 0 comments
Open

convertToValidEmbedCode #84

5Mi opened this issue May 21, 2018 · 0 comments

Comments

@5Mi
Copy link
Owner

5Mi commented May 21, 2018

嵌入第三方外链 (验证并处理成 iframe)

项目要实现发布的文章加外链的功能,从机核网上扒了段代码,帮大忙了

混淆过的代码真是太难读了

function () {
    "use strict";
    //e 规则
    var e = {
      bilibili: {
        source: /^(https?:)?\/\/www\.bilibili\.com\/video\/av(\d+)(\/?.*)?/,
        embedSrc: /^(https?:)?\/\/www\.bilibili\.com\/html\/player\.html/,
        isVideo: !0
      },
      bilibiliEmbed: {
        embedSrc: /^(https?:)?\/\/static\.hdslb\.com\/miniloader\.swf/,
        useFlash: !0,
        isVideo: !0
      },
      bilibiliBalckboard: {
        embedSrc: /^(https?:)?\/\/www\.bilibili\.com\/blackboard\/player\.html/,
        isVideo: !0
      },
      bilibiliAfterPlay: {
        embedSrc: /^(https?:)?\/\/player\.bilibili\.com\/player\.html.+/,
        isVideo: !0
      },
      steam: {
        source: /^(https?:)?\/\/store\.steampowered\.com\/app\/(\d+)/,
        embedSrc: /^(https?:)?\/\/store\.steampowered\.com\/widget\/.+/
      },
      "163music": {
        source: /^(https?:)?\/\/music\.163\.com\/#\/(playlist|song)\?id=(\d+)/,
        embedSrc: /^(https?:)?\/\/music\.163\.com\/outchain\/player\?.+/
      },
      youku: {
        source: /^(https?:)?\/\/v\.youku\.com\/v_show\/id_(.+)\.html/,
        embedSrc: /^(https?:)?\/\/player\.youku\.com\/embed\/.+/,
        isVideo: !0
      },
      miaopai: {
        source: /^(https?:)?\/\/www.miaopai.com\/show\/(.+)\.htm/,
        embedSrc: /^(https?:)?\/\/v\.miaopai\.com\/iframe\?.+/,
        onlyHttps: !0,
        isVideo: !0
      },
      qqvideo: {
        source: /^(https?:)?\/\/v\.qq\.com\/x\/page\/(.+)\.html/,
        embedSrc: /^(https?:)?\/\/v\.qq\.com\/iframe\/player\.html\?.+/,
        isVideo: !0
      },
      taptap: {
        source: /^(https?:)?\/\/www\.taptap\.com\/app\/(\d+)/,
        embedSrc: /^(https?:)?\/\/www\.taptap\.com\/widget\/(\d+)/
      },
      tudou: {
        source: /^(https?:)?\/\/www\.tudou\.com/,
        deprecated: !0,
        isVideo: !0
      },
      acfun: {
        source: /^(https?:)?\/\/www\.acfun\.cn/,
        deprecated: !0,
        isVideo: !0
      },
      twitchRecord: {
        source: /^(https?:)?\/\/www\.twitch\.tv\/videos\/(\d+)(\?t=.+)?/,
        embedSrc: /^(https?:)?\/\/player\.twitch\.tv\/\?.*video=v\d+/,
        isVideo: !0
      },
      twitch: {
        source: /^(https?:)?\/\/www\.twitch\.tv\/([^\/]+)$/,
        embedSrc: /^(https?:)?\/\/player\.twitch\.tv\/\?.*channel=.+/,
        isVideo: !0
      },
      taptapPlayer: {
        source: /^(https?:)?\/\/www\.taptap\.com\/video\/(\d+)\/embed.*/,
        embedSrc: /^(https?:)?\/\/www\.taptap\.com\/video\/(\d+)\/embed/,
        isVideo: !0
      }
    }
    // 规则 含name
    var t = Object.keys(e).map(function (t) {
      return e[t].name = t,
        e[t]
    })
    var i = function (e) {
      return e.trim()
    }
    //  i为 sourceUrl or embedSrc
    var n = function (e, i, n) {
      t.forEach(function (t) {
        var o = t[i];
        if (o) {
          var s = o.exec(e);
          if (s) {
            n(t, s)
            return false
          }
        }
        return true
      })
    }
    //是否以 http开头  
    var o = function (e) {
      return /^(https?:)?\/\//i.test(e)
    }
    //非http开头时  是哪种element  
    var s = function (e) {
      if ("undefined" == typeof document)
        return null;
      var t = document.createElement("div");
      if (t.innerHTML = e,
        1 === t.childNodes.length) {
        var i = t.childNodes[0];
        return /OBJECT|IFRAME|EMBED/.test(i.tagName) ? i : null
      }
      return null
    }
    //取element的src  
    var r = function (e) {
      var t = s(e);
      if (t) {
        var i = t.getAttribute("src");
        if (i)
          return i
      }
      return null
    }
    //embed时 获取规则类型
    var a = function (e) {
      var t = r(e)
      var i = null

      n(t, "embedSrc", function (e) {
        i = e.name
      })
      return t ? i : null
    }
    // 根据规则类型 和 匹配到的正则分组  返回统一iframe 
    var l = function (e, t) {
      var i = t[0]
        , n = void 0;
      switch (e) {
        case "bilibili":
          n = '<iframe src="https://www.bilibili.com/html/player.html?aid=$$aid$$&wmode=transparent&as_wide=1&autoplay=true&page=$$page$$" frameborder="0" width="646" height="190"></iframe>';
          var o = i.match(/p=(\d+)/) || i.match(/page=(\d+)/)
            , s = o ? o[1] : 1;
          return n.replace("$$aid$$", t[2]).replace("$$page$$", s);
        case "steam":
          return n = '<iframe src="//store.steampowered.com/widget/$$id$$/" frameborder="0" width="646" height="190"></iframe>',
            n.replace("$$id$$", t[2]);
        case "163music":
          n = '<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=$$height1$$ src="//music.163.com/outchain/player?type=$$type$$&id=$$id$$&auto=0&height=$$height2$$"></iframe>';
          var r = "playlist" === t[2] ? 0 : 2
            , a = "playlist" === t[2] ? 430 : 66;
          return n.replace("$$type$$", r).replace("$$id$$", t[3]).replace("$$height1$$", a + 20).replace("$$height2$$", a);
        case "youku":
          return n = '<iframe height="400" width="680" src="//player.youku.com/embed/$$id$$" frameborder="0" allowfullscreen=""></iframe>',
            n.replace("$$id$$", t[2]);
        case "qqvideo":
          return n = '<iframe frameborder="0" width="640" height="498" src="//v.qq.com/iframe/player.html?vid=$$id$$&tiny=0&auto=0" allowfullscreen></iframe>',
            n.replace("$$id$$", t[2]);
        case "taptap":
          return n = '<iframe src="https://www.taptap.com/widget/$$id$$" frameborder="0" width="100%" height="90px"></iframe>',
            n.replace("$$id$$", t[2]);
        case "twitchRecord":
          return n = '<iframe src="https://player.twitch.tv/?autoplay=false$$t$$&video=v$$id$$" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="100%"></iframe>',
            n.replace("$$id$$", t[2]).replace("$$t$$", t[3] || "");
        case "twitch":
          return n = '<iframe src="https://player.twitch.tv/?channel=$$channel$$" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="100%"></iframe>',
            n.replace("$$channel$$", t[2]);
        case "taptapPlayer":
          return n = '<iframe src="$$src$$" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="100%"></iframe>',
            n.replace("$$src$$", t[0]);
        case "miaopai":
          return n = '<iframe src="https://v.miaopai.com/iframe?scid=$$scid$$" frameborder="0" allowfullscreen="true" scrolling="no" height="378" width="100%"></iframe>',
            n.replace("$$scid$$", t[2]);
        default:
          return null
      }
    }

    // if以http开头return结果iframe
    // else 返回哪种element
    var c = function (e) {
      // 以http开头
      if (o(e)) {
        var t = null;
        n(e, "source", function (e, i) {
          t = l(e.name, i)
        })
        //返回 处理后的iframe
        return t
      }
      //embed
      return s(e) ? e : null
    }
    // deprecated  return 带name规则
    var u = function (e) {
      var t = null;
      o(e) && n(e, "source", function (e) {
        e.deprecated && (t = e)
      })
      return t
    }
    //  
    var d = function (t, n) {
      //i trim一下
      var o = i(t)
      var s = c(o, n);
      if (s) {
        var r = a(s);
        //r 为规则类型
        //s 为element元素
        if (r) {
          var l = e[r]
            , d = l.useFlash
            , h = l.isVideo
            , f = s.replace("'allowfullscreen'", "allowfullscreen").replace(/http:\/\//, "https://");
          return h && (f.indexOf("allowfullscreen") < 0 && (f = f.replace(/^<([^ ]+) /, "<$1 allowfullscreen ")),
            "bilibiliBalckboard" !== r && "bilibili" !== r || f.indexOf("as_wide=") < 0 && (f = f.replace(/src="([^"]+)"/, 'src="$1&as_wide=1"'))),
            {
              useFlash: d,
              isVideo: h,
              code: f
            }
        }
        return null
      }
      var p = u(o);
      return n && p ? {
        isVideo: !0,
        useFlash: !1,
        code: '<a class="embedJump" href="' + o + '" target="_blank">' + p.name + " \u63d0\u4f9b\u7684\u63a7\u4ef6\uff0c\u70b9\u51fb\u67e5\u770b</a>"
      } : null
    };
    window.EmbedMana = {
      //暴露出
      convertToValidEmbedCode: d,
      tip: "\u76ee\u524d bilibili\u3001steam\u3001\u7f51\u6613\u4e91\u97f3\u4e50\u3001\u4f18\u9177\u3001\u817e\u8baf\u89c6\u9891\u3001Twitch\u3001TapTap \u652f\u6301\u539f\u7f51\u5740\u89e3\u6790\uff1b\u79d2\u62cd\u4ec5\u652f\u6301 iframe \u5d4c\u5165\u3002"
    }
  }
//返回形如
{
useFlash: Boolean,
isVideo: Boolean,
code: '<iframe>'
}
@5Mi 5Mi added the 项目中 label May 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant