busuzhike 最近的时间轴更新
busuzhike

busuzhike

V2EX 第 30092 号会员,加入于 2012-11-29 18:00:31 +08:00
busuzhike 最近回复了
68 天前
回复了 EyebrowsWhite 创建的主题 推广 [抽奖] 评论送两台 OneKey Mini
大气磅礴
@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()函数打开链接,该函数可以绕过同源策略的限制。同时,你还需要确保你的代码可以正确地找到需要操作的网页元素,可以使用浏览器开发者工具检查元素的选择器是否正确。
这个加了,试过后依然不行。

// ==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);
})();
// ==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);
})();
不计成本,哪个季节哪个城市舒服就过去住最好的酒店
198 天前
回复了 huajieyu 创建的主题 程序员 求稳定可访问 chatgpt 的魔法
@unco020511 并也不稳定,我的刚刚找不到 chat 了,只剩搜索了
嗯,感谢两位回复。我猜也没什么特别的,可能是站长想让注册才看到,没注册登录就看加密的。
201 天前
回复了 xiaonizi 创建的主题 生活 我哥问我借钱买房,我拒绝了
大家都不借钱,世界上的纷争会少一半
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2310 人在线   最高记录 6067   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 26ms · UTC 03:07 · PVG 11:07 · LAX 20:07 · JFK 23:07
Developed with CodeLauncher
♥ Do have faith in what you're doing.