【问题标题】:How to open an URL via browser and output a simple text file rather than HTML code?如何通过浏览器打开 URL 并输出一个简单的文本文件而不是 HTML 代码?
【发布时间】:2022-11-01 17:16:25
【问题描述】:

我已经构建了这个小 HTML 文件,您可以在这里看到 https://alterego.cc/mypublicip/,它返回您的公共 IP。如果您检查页面的代码,您实际上可以在其中看到 HTML,因为那当然是一个 HTML 文件

我真正想要实现的是这样的https://wtfismyip.com/text(来自其他人),如果您检查代码,您可以看到它只是一个文本文件。没有额外的标签或任何特别的东西

我怎样才能达到同样的结果?

我已经尝试了一切,但我总是在那里有一些 HTML 代码。特别是 DIV 和 innerText 但到目前为止还没有特别的运气。我相信我采用了错误的方法,并且我缺少一些东西

谢谢!

【问题讨论】:

  • 只输出你想要的。如果有 HTML 代码,那么它是您添加的内容,或者您​​正在使用模板
  • JavaScript 需要客户端上的主机才能呈现,没有浏览器会在直接访问时执行纯 JS。因此,如果您尝试使用 JS 进行此操作,则需要一些最低限度的 HTML 才能使其工作。所以除非你想在页面上有一个按钮事件来打开一个新文档并写入它,否则 JS 可能不是一个选项。然后只看PHP,只需回显并退出。而已。
  • @ChrisHaas Nitpick:服务器端 JavaScript 已经存在很多年了(主要是 Node.js 的形式)。但是,设置 PHP 页面确实比托管 Node.js 页面要容易得多。

标签: javascript php html ajax


【解决方案1】:

我创建了 2 个文件。第一页.php第二页.php

试试这个代码。

第一页.php

<!DOCTYPE html>
<html>
<head>
<title>What is my Public IP address?</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>
$.getJSON("https://api.ipify.org?format=json", function(data) {
    $("#mpi").html(data.ip);
    $("#tes").html(data.ip);
    document.getElementById('tes').value = html(data.ip);
})
    mpi = mpi.replace(/s|
|&nbsp;/g, ' ');
    mpi = mpi.replace(/<[^>]+>/gm, '');

</script>
</head>
<body>
<p id="mpi"></p><br>
<button type="button" onClick="ambil()">Give me this IP in plain text</button>
<form id="lanjut" action="secondPage.php" method="get" style="display:none">
<input id="tes" name="tes" type="text" value="">
</form>
<script>
function ambil(){
    document.getElementById('tes').value = document.getElementById('mpi').innerHTML;
    document.getElementById('lanjut').submit();
    }
</script>
</body>
</html>

第二页.php

<?php
if(isset($_GET['tes'])){
$getIP = $_GET['tes'];
}
echo $getIP;
?>

【讨论】:

  • 危险: 此代码为vulnerable to XSS 用户输入需要转义才能插入到 HTML 文档中!。
  • 谢谢你,小伙伴!现在看起来好多了。我发布了一个新答案,并做了一些更改。我快到了,我希望
【解决方案2】:

谢谢@Mahen,我在这里和那里做了一些改变,现在看起来好多了。问题现在正在正确等待 getJSON 函数实际终止。我正在使用此链接上的建议:https://splunktool.com/function-wait-with-return-until-getjson-is-finished 但到目前为止还没有运气。我所做的是将 setTimeout 添加到函数 ambil() 中,这样会有一些等待,并且大多数情况下,当 getJSON 返回一些值时,代码会向前移动

你可以在这里试试:https://alterego.cc/mypublicip/firstPage.php

第一页.php

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="mypublicipstyle.css">
<title>What is my Public IP address?</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
<script>

$.getJSON("https://api.ipify.org?format=json", function(data) {
    $("#mpi").html(data.ip);
    $("#tes").html(data.ip);
    document.getElementById('tes').value = html(data.ip);
})
    mpi = mpi.replace(/s|
|&nbsp;/g, ' ');
    mpi = mpi.replace(/<[^>]+>/gm, '');

</script>
</head>
<body>
<p id="mpi" hidden></p><br>
<button type="button" id="submit" onClick="ambil()" style="display: none;"></button>
<form id="lanjut" action="secondPage.php" method="get" style="display:none">
<input id="tes" name="tes" type="text" value="">
</form>
<script>

setTimeout(function ambil(){
    document.getElementById('tes').value = document.getElementById('mpi').innerHTML;
    document.getElementById('lanjut').submit();
    }, 5000);
</script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#submit").click();
    });
</script>
</body>
</html>

第二页.php

<?php
if(isset($_GET['tes'])){
$getIP = $_GET['tes'];
}
echo $getIP;
?>

mypublicipstyle.css

button {
    visibility: hidden;
}

【讨论】:

    猜你喜欢
    • 2014-06-29
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多