我不能在我的https://example.com/?l=http://example2.com工作服务器上使用url查询CloudFlare。
所以,我的问题是,如何将这个JS脚本转换为使用https://example.com/#http://example2.com
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi,
function(m,key,value) {
vars[key] = value;
});
return vars;
}
var l = getUrlVars()["l"];
根据jp-jee的答复,这是我的脚本:
<script>
var hash = window.location.hash;
function change(){
var txt = hash.replace("#", "");
document.getElementById("url").value = txt;
}
</script>
<input onclick="change()" name="url" type="text" id="url" placeholder="enter url here" value="enter url here" />
<button id="submit" onclick="submitURL()">Submit</button>
谢谢:)
发布于 2022-08-14 10:31:54
使用window.location.hash
并从结果中删除#
字符。
对于URL https://example.com/#http://example2.com
,window.location.hash
将转到"#http://example2.com"
发布于 2022-08-14 11:17:19
它后面的#
和文本称为“片段”或“散列”。URL
类将其放入属性.hash
中。
但是,有一个不同的问题:该文本不是在常规HTTP中发送到服务器的--它只保存在浏览器中。这意味着您无法在Cloudflare code或任何其他服务器端代码中看到此值。如果要使用#
进行导航,则必须在客户端浏览器中实现导航。
发布于 2022-08-20 10:07:34
由于您使用的是Cloudflare工作者,所以它的工作方式类似于nodeJS或任何其他服务器端应用程序。这意味着您将无法访问window.*
或document.*
对象。
如果您试图替换占位符,您应该使用的是.placeholder
而不是.value
,您可以在文档的头或正文中添加此脚本和您的工作人员
<script>
var hash = window.location.href;
var txt = hash.replace("#", "");
document.getElementById("url").placeholder= txt;
</script>
这样,一旦页面加载,浏览器就会执行脚本&占位符将被修改。
或者,您也可以使用Cloudflare工作者通过将HTML模板更改为
<input onclick="change()" name="url" type="text" id="url" cloudflare_placeholder="enter url here" value="enter url here" />
现在,从您的工作人员,您可以将文本替换为
intercepted_html = intercepted_html.replace('cloudflare_placeholder=\"enter url here\"', 'placeholder=\"${host}/${pathname}\"')
有关如何使用Cloudflare工作者修改响应的示例。云焰响应修正
https://stackoverflow.com/questions/73354135
复制