0

我正在运行以下脚本来为我的平板电脑提供天气预报。问题是每次有人不小心触摸它时,它都会打开源页面。任何想法我怎么能阻止外部页面的链接?我使用沙盒在 iframe 上取得了成功,但无法使其在此工作,因为我不确定这种语言是什么:

<a class="weatherwidget-io" href="https://forecast7.com/en/51d51n0d13/london/ "data-label_1="LONDON" data-label_2="WEATHER" data-font="Roboto" data-icons="Climacons Animated" data-theme="pure"  pointer-events: none>LONDON WEATHER</a>
<script>
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src='https://weatherwidget.io/js/widget.min.js';fjs.parentNode.insertBefore(js,fjs);}}(document,'script','weatherwidget-io-js');href="javascript:void(0)"
</script>

<script>
 function reloadIFrame() {
     document.getElementById('weatherwidget-io-0').src = document.getElementById('weatherwidget-io-0').src;
 }
    
 window.setInterval(reloadIFrame, 95000);
 </script>

<script type="text/javascript">
   function DisableFrameLinks(){
   var iFrame = document.getElementById('weatherwidget-io');
   var links;
  if ( iFrame.contentWindow)
   {
     links = iFrame.contentWindow.document.links;
     for(var i in links)
     {
        links[i].href="#";
     }
   }
 }
  </script>
4

1 回答 1

-1

中缺少它周围的属性pointer-events: none<a>style=""

从以下位置更新它:

<a class="weatherwidget-io" ... pointer-events: none>LONDON WEATHER</a>

到:

<a class="weatherwidget-io" ... style="pointer-events: none;">LONDON WEATHER</a>

此外,您的整个代码可以简化为以下内容:

  • 如果该功能不存在,所有<script>!function(d,s,id)...</script>功能都会添加<script id="weatherwidget-io-js" src="https://weatherwidget.io/js/widget.min.js"></script>到页面中 - 您可以首先添加脚本并删除该功能。

  • DisableFrameLinks()一旦 CSS 样式pointer-events: none;正确应用<a>到.

最低要求的代码:

<a class="weatherwidget-io" href="https://forecast7.com/en/51d51n0d13/london/" data-label_1="LONDON" data-label_2="WEATHER" data-font="Roboto" data-icons="Climacons Animated" data-theme="pure" style="pointer-events: none;">LONDON WEATHER</a>

<script id="weatherwidget-io-js" src="https://weatherwidget.io/js/widget.min.js"></script>

<script>
    function reloadIFrame() {
        document.getElementById('weatherwidget-io-0').src = document.getElementById('weatherwidget-io-0').src;
    }

    window.setInterval(reloadIFrame, 95000);
</script>
于 2021-07-10T16:59:47.883 回答