上一主题 下一主题
ScriptCat,新一代的脚本管理器脚本站,与全世界分享你的用户脚本油猴脚本开发指南教程目录
返回列表 发新帖

【求助】如何跨域获取localStorage的值

[复制链接]
  • TA的每日心情
    开心
    2022-6-2 15:24
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    13

    主题

    56

    回帖

    79

    积分

    初级工程师

    积分
    79

    油中2周年

    发表于 2021-7-29 15:06:37 | 显示全部楼层 | 阅读模式
    本帖最后由 ozon 于 2021-7-29 15:07 编辑

    https://www.baidu.com/
    我想利用localStorage在百度页面的得到ip查询结果,但是发现跨域不好用// ==UserScript==
    // @name         测试
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  try to take over the world!
    // @author       You
    // @match        https://www.baidu.com/*
    // @match        https://ip.cn/

    // @grant        none
    // ==/UserScript==

    (function() {
      'use strict';
      //button
      var butt1 = document.createElement("input");
      butt1.setAttribute("id", "butt1")
      butt1.setAttribute("type", "button");
      butt1.setAttribute("value", "存储ip");
      butt1.style.width = "200px";

      butt1.onclick = function() {
          window.open("https://ip.cn/")
        setTimeout(() => {
          document.querySelector('#butt1').value = localStorage.getItem("ip");
        }, 3000)
      }

      var y = document.querySelector('#lg')
      y.appendChild(butt1)


      if (location.hostname == "ip.cn") {
        setTimeout(() => {
          var ip = document.querySelector('#tab0_ip').innerText
          localStorage.setItem("ip", ip);
        }, 2000)
      }

    })();

    测试.zip

    607 Bytes, 下载次数: 1

  • TA的每日心情
    开心
    2024-3-13 10:14
  • 签到天数: 211 天

    [LV.7]常住居民III

    284

    主题

    3810

    回帖

    3739

    积分

    管理员

    积分
    3739

    管理员荣誉开发者油中2周年生态建设者喜迎中秋油中3周年挑战者 lv2

    发表于 2021-7-29 15:38:12 | 显示全部楼层
    localStorage是不能跨域的。。。

    如果是油猴脚本的话 可以用 GM_get/setValue 这些来获取数据
    上不慕古,下不肖俗。为疏为懒,不敢为狂。为拙为愚,不敢为恶。/ 微信公众号:一之哥哥
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2022-6-2 15:24
  • 签到天数: 3 天

    [LV.2]偶尔看看I

    13

    主题

    56

    回帖

    79

    积分

    初级工程师

    积分
    79

    油中2周年

    发表于 2021-7-29 16:06:02 | 显示全部楼层
    王一之 发表于 2021-7-29 15:38
    localStorage是不能跨域的。。。

    如果是油猴脚本的话 可以用 GM_get/setValue 这些来获取数据 ...

    完美,谢谢大佬
    回复

    使用道具 举报

    发表回复

    本版积分规则

    快速回复 返回顶部 返回列表