duanshanduo3363 2018-04-08 18:09
浏览 1044
已采纳

解码base64 URL并嵌入HTML

I want make like this http://www.jredtna.com/read/?hash=cGxheWVyIDEgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvZmgwUUQtV1dyY2MKcGxheWVyIDIgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvMk1wVWotQXVhNDg=

(You can't see iframe because website using cookies to check if you coming from another website).

So please join here http://halqat.online/video/watch.php?vid=6f673f4c5 and click red play button to see what I mean.

When decode hash= by base64:

cGxheWVyIDEgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvZmgwUUQtV1dyY2MKcGxheWVyIDIgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvMk1wVWotQXVhNDg=

will give us like this:

player 1 => https://www.youtube.com/embed/fh0QD-WWrcc

player 2 => https://www.youtube.com/embed/2MpUj-Aua48

My question is how I can do like that:

Get hash=Base64URL

Decode it and split "player 1" and "youtube url" by =>

Put data inside html like this

<div class="embeding">
  <ul>
      <li><a data-src="https://www.youtube.com/embed/fh0QD-WWrcc">player 1</a></li>
      <li><a data-src="https://www.youtube.com/embed/2MpUj-Aua48">player 2</a></li>
  </ul>
</div>

and auto add li a if find another players

  • 写回答

1条回答 默认 最新

  • duanliao3826 2018-04-08 18:47
    关注

    I use splice method to make an array and then from this array I get the youtube url's strings via split method.

    function decodeBase_64(arg){ 
      var decodedData=window.atob(arg);
      var a;
      var c=[];  
      for(var i=0;i<decodedData.split('=>').length;i++){
        if(decodedData.split('=>')[i].length>10){      
          a=decodedData.split('=>')[i];
          a=a.substr(0, 42);
          c.push(a);
        }
      }
       return c; 
    }
    document.getElementById('player_1').setAttribute('data-src',decodeBase_64('cGxheWVyIDEgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvZmgwUUQtV1dyY2MKcGxheWVyIDIgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvMk1wVWotQXVhNDg=')[0] );
    document.getElementById('player_2').setAttribute('data-src',decodeBase_64('cGxheWVyIDEgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvZmgwUUQtV1dyY2MKcGxheWVyIDIgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvMk1wVWotQXVhNDg=')[1] );
    
    console.log(decodeBase_64('cGxheWVyIDEgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvZmgwUUQtV1dyY2MKcGxheWVyIDIgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvMk1wVWotQXVhNDg='));
    console.log(decodeBase_64('cGxheWVyIDEgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvZmgwUUQtV1dyY2MKcGxheWVyIDIgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvMk1wVWotQXVhNDg=')[0]);
    console.log(decodeBase_64('cGxheWVyIDEgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvZmgwUUQtV1dyY2MKcGxheWVyIDIgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvMk1wVWotQXVhNDg=')[1]);
    <div class="embeding">
      <ul>
          <li><a data-src="" id="player_1">player 1</a></li>
          <li><a data-src="" id="player_2">player 2</a></li>
      </ul>
    </div>

    Check the console to see the c array

    For the second issue..Get the her via window.location.href and then

    var str='http://www.jredtna.com/read/?hash=cGxheWVyIDEgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvZmgwUUQtV1dyY2MKcGxheWVyIDIgPT4gaHR0cHM6Ly93d3cueW91dHViZS5jb20vZW1iZWQvMk1wVWotQXVhNDg=';
    str=str.substring(str.indexOf("hash="));
    str=str.replace('hash=','');
    console.log(str);

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调