简体   繁体   English

通过Chrome上的JavaScript书签获取远程IP

[英]Get remote ip via javascript bookmark on Chrome

I'm trying to make a small snippet on Google Chrome (as a bookmark) so I can "alert" my remote ip adress, but it is not working as it gives me the following error : 我正在尝试在Google Chrome上制作一个小片段(作为书签),以便我可以“提醒”我的远程IP地址,但它无法正常工作,因为它会给我以下错误:

XMLHttpRequest cannot load https://l2.io/ip.js?var=myip . XMLHttpRequest无法加载https://l2.io/ip.js?var=myip No 'Access-Control-Allow-Origin' header is present on the requested resource. 请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' http://stackoverflow.com ' is therefore not allowed access. 因此,不允许来源“ http://stackoverflow.com ”访问。

var url = "https://l2.io/ip.js?var=myip"
var httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = function (data) {
  // code
}
httpRequest.open('GET', url)
httpRequest.send()
alert(httpRequest["response"])

How can I get around this problem ? 我怎样才能解决这个问题?

You can get around this problem in a few ways. 您可以通过几种方式解决此问题。 Tampermonkey , Chrome extensions or a Chrome app. Tampermonkey ,Chrome扩展程序或Chrome应用。

Here is an example using Tampermonkey. 以下是使用Tampermonkey的示例。

// ==UserScript==
// @name       Show my IP
// @version    0.1
// @include    /https?:\/\/*/
// @grant      GM_xmlhttpRequest
// @grant      GM_registerMenuCommand
// ==/UserScript==

GM_registerMenuCommand('My IP', run);

function run(){
    GM_xmlhttpRequest({
        method: "GET",
        url: "https://l2.io/ip.js?var=myip",
        onload: function(response) {
            alert(response.responseText);
        }
    });
}

This adds a menu option to the tampermonkey plugin called "My IP" that when you click it, it shows the alert box. 这会为名为“我的IP”的tampermonkey插件添加一个菜单选项,当您单击它时,它会显示警告框。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM