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

292 天前
 busuzhike

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

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

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

782 次点击
所在节点    问与答
6 条回复
Pipecraft
292 天前
你把你的代码或脚本地址发出来看看吧
busuzhike
292 天前
// ==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
292 天前
这个网站是通过 ajax 获取数据,前端渲染的。
所以要等到数据加载完毕,列表显示出来后,再执行 openPost 和 document.querySelectorAll('a.post-link-selector');
可以用 MutationObserver 或 setTimeout 判断数据是否加载完毕。
busuzhike
292 天前
这个加了,试过后依然不行。

// ==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
292 天前
上面这段代码只判断页面是否加载完毕,并没有判断数据是否加载完毕。

下面是简单实现。

```
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
292 天前
@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()函数打开链接,该函数可以绕过同源策略的限制。同时,你还需要确保你的代码可以正确地找到需要操作的网页元素,可以使用浏览器开发者工具检查元素的选择器是否正确。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/958594

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX