V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
busuzhike
V2EX  ›  问与答

求一个油猴脚本,可以打开聚宽社区的帖子

  •  
  •   busuzhike · 281 天前 · 762 次点击
    这是一个创建于 281 天前的主题,其中的信息可能已经有所发展或是发生改变。

    聚宽社区: https://www.joinquant.com/view/community/list?listType=1

    求一个油猴脚本可以打开其中的一个用户帖子。

    自己做了一个,但是不能实现功能。我的代码放到网页的控制台手工执行是可以的。脚本自动执行就不能打开帖子。求高手帮忙。

    6 条回复    2023-07-21 16:38:39 +08:00
    Pipecraft
        1
    Pipecraft  
       281 天前
    你把你的代码或脚本地址发出来看看吧
    busuzhike
        2
    busuzhike  
    OP
       281 天前
    // ==UserScript==
    // @name Open Post on JoinQuant
    // @namespace http://tampermonkey.net/
    // @version 0.1
    // @description Open any post on JoinQuant
    // @author Your name
    // @match https://www.joinquant.com/view/community/list?listType=1
    // @grant none
    // ==/UserScript==

    (function() {
    'use strict';

    // Get all the post links
    let posts = document.querySelectorAll('a.post-link-selector'); // Replace 'a.post-link-selector' with the actual CSS selector for the post links

    // Create a function to open a post
    function openPost(index) {
    // Make sure the index is within the range of available posts
    if (index >= 0 && index < posts.length) {
    // Open the post in a new tab
    window.open(posts[index].href, '_blank');
    }
    }

    // Open the first post
    openPost(0);
    })();
    Pipecraft
        3
    Pipecraft  
       280 天前
    这个网站是通过 ajax 获取数据,前端渲染的。
    所以要等到数据加载完毕,列表显示出来后,再执行 openPost 和 document.querySelectorAll('a.post-link-selector');
    可以用 MutationObserver 或 setTimeout 判断数据是否加载完毕。
    busuzhike
        4
    busuzhike  
    OP
       280 天前
    这个加了,试过后依然不行。

    // ==UserScript==
    // @name joinquant comm open item
    // @namespace https://greasyfork.org/users/256625
    // @match https://uqer.datayes.com/v3/*
    // @match https://www.joinquant.com/view/community/list?listType=1
    // @version 1
    // @description yeah
    // @author DipshitDickinson
    // @grant GM_openInTab
    // @run-at document-end
    // ==/UserScript==
    'use strict';

    //setTimeout(autoClick,500);
    //setInterval(autoClick,2000);

    function matchURL(x) {
    return window.location.href.indexOf(x) != -1;
    }

    (function() {
    function pageOnLoad() {
    // HTML 文档已完成加载
    //打开一个社区帖子
    const item = "body > section > main > div > div.jq-m-community-list__content > div.jq-m-community-list__left > div.jq-m-community-list__list > div.jq-c-list.jq-c-list_community > div:nth-child(10) > div > div.jq-c-list_community__title > div.jq-c-list_community__text"
    console.log("----------------------------starting")

    async function waitForItem() {
    while (!document.querySelector(item)) {
    await new Promise(resolve => setTimeout(resolve, 3000)); // 等待 1 秒
    }
    // 当目标按钮出现时,执行相应的操作
    document.querySelector(item).click();
    console.log("----------------------------clicked")
    }

    waitForItem();
    }

    function ready(fn) {
    if (document.readyState !== 'loading') {
    fn();
    } else {
    document.addEventListener('DOMContentLoaded', fn);
    }
    }

    ready(pageOnLoad);
    })();
    Pipecraft
        5
    Pipecraft  
       280 天前
    上面这段代码只判断页面是否加载完毕,并没有判断数据是否加载完毕。

    下面是简单实现。

    ```
    function pageOnLoad() {
    console.log("Do something")
    const item = document.querySelector(
    ".jq-c-list_community__text,.jq-list__community-title"
    )
    item.click()
    }

    function checkAndRun(fn) {
    return () => {
    // 判断是否数据加载完毕
    const item = document.querySelector(
    ".jq-c-list_community__text,.jq-list__community-title"
    )
    if (item) {
    fn()
    return true
    }
    }
    }

    const main = checkAndRun(pageOnLoad)

    const observer = new MutationObserver(async (mutationsList) => {
    if (main()) {
    observer.disconnect()
    }
    })

    observer.observe(document, {
    childList: true,
    subtree: true,
    })

    main()

    ```
    busuzhike
        6
    busuzhike  
    OP
       280 天前
    @Pipecraft 测试了,代码没有问题。

    console 输出:
    ..........................Do something
    userscript.html?name=joinquant-v2ex-open.user.js&id=6459f5d0-bfe6-4fb7-bba4-08608487eb22:22 ..........................Done


    我问了 chatGPT:
    Q: 一段代码的功能是再网页点击元素打开一个新链接,代码放到网页 console 运行正常可以打开,放在油猴脚本中没有报错但没有打开链接,这是怎么回事?

    A:这种情况可能是由于油猴脚本运行环境的限制导致的。

    在网页上直接运行代码和在油猴脚本中运行代码是有一些差别的。油猴脚本运行在浏览器的沙盒环境中,受到了一些安全限制和浏览器的同源策略的限制。因此,如果你的代码试图打开一个不同域名的网页,可能会被浏览器拦截或者被沙盒环境限制。

    另外,如果你的代码依赖于某些网页元素,比如说某个按钮或者链接,那么在油猴脚本中运行时可能会出现找不到元素的情况,导致代码无法正常运行。

    为了解决这个问题,你可以尝试使用油猴脚本提供的 GM_openInTab()函数打开链接,该函数可以绕过同源策略的限制。同时,你还需要确保你的代码可以正确地找到需要操作的网页元素,可以使用浏览器开发者工具检查元素的选择器是否正确。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2827 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 07:04 · PVG 15:04 · LAX 00:04 · JFK 03:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.