halemorning 发表于 2023-4-30 16:10:51

判断网页某段代码里的颜色,并执行相应代码里的onclick应该怎么实现

<con>选择要分解的装备:<br><a href="#" onclick="p.cmd('helptools 73421346');return false;"><img src="/img/arm/db/278.gif" border="0" style="margin-right:5px;"><b><font color="008800">[召唤白龙披风]</font></b></a> <a href="#" onclick="p.cmd('foo fenjie 73421346');return false;">[分解]</a><br>         </con>

例如上面的代码,font color ="008800" 判断等于008800后,执行对应下面的onclick,p.cmd('foo fenjie 73421346');return false;
用js应该怎么实现,求大佬指导

liuhongrun2022 发表于 2023-4-30 16:16:02

var fontElement = document.querySelector('font'); // 获取颜色为 #008800 的字体元素
if (fontElement) {    // 判断元素是否存在
    var onClickCommand = fontElement.parentElement.querySelector('a').getAttribute('onclick');// 获取点击事件的命令
    if (onClickCommand) {
      eval(onClickCommand.replace("return false;","")); // 执行点击事件
    }
}

isdkz 发表于 2023-4-30 16:24:52

要实现这个功能,你可以使用JavaScript来检查颜色并执行对应的 onclick 事件。以下是一个简单的示例:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>颜色判断示例</title>
    <script>
      function checkColorAndExecute(element) {
            // 获取颜色
            const color = element.querySelector('font').getAttribute('color');
            // 判断颜色是否为'008800'
            if (color === '008800') {
                // 执行对应的onclick事件
                element.querySelector('a').onclick();
            }
      }

      function init() {
            const conElement = document.querySelector('con');
            checkColorAndExecute(conElement);
      }
    </script>
</head>
<body onload="init()">
    <con>选择要分解的装备:<br><a href="#" onclick="p.cmd('helptools 73421346');return false;"><img src="/img/arm/db/278.gif" border="0" style="margin-right:5px;"><b><font color="008800">[召唤白龙披风]</font></b></a> <a href="#" onclick="p.cmd('foo fenjie 73421346');return false;">[分解]</a><br></con>
</body>
</html>


这个示例中, checkColorAndExecute 函数会检查给定元素内 <font> 标签的颜色属性。如果颜色等于 008800 ,则执行对应的 onclick 事件。

在这个例子中,当页面加载时, init 函数会被调用,然后会检查 <con> 元素内的颜色并执行相应的 onclick 事件。

注意:这个示例代码只适用于具体提供的HTML结构。根据实际情况,你可能需要对代码进行调整。

halemorning 发表于 2023-4-30 16:57:22

我用的糖果游戏浏览器,用的是里面自带的js运行代码,显示对象不支持querySelector方法或属性

halemorning 发表于 2023-4-30 16:58:16

isdkz 发表于 2023-4-30 16:24
要实现这个功能,你可以使用JavaScript来检查颜色并执行对应的 onclick 事件。以下是一个简单的示例:


...

糖果游戏浏览器没有开发者工具 不能在网页代码里面修改东西 大佬

isdkz 发表于 2023-4-30 17:01:09

halemorning 发表于 2023-4-30 16:58
糖果游戏浏览器没有开发者工具 不能在网页代码里面修改东西 大佬

你的浏览器能装油猴插件吧?

以下是一个适用于你需求的油猴脚本示例:

// ==UserScript==
// @name         检查颜色并执行onclick
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description检查颜色并执行onclick
// @author       You
// @match      *://*/*
// @grant      none
// ==/UserScript==

(function() {
    'use strict';

    function checkColorAndExecute() {
      const conElement = document.querySelector('con');

      if (conElement) {
            const fontElement = conElement.querySelector('font');
            const color = fontElement && fontElement.getAttribute('color');

            if (color === '008800') {
                const targetLink = conElement.querySelector('a:not()');

                if (targetLink) {
                  targetLink.click();
                }
            }
      }
    }

    // 在页面加载完成后执行 checkColorAndExecute 函数
    window.addEventListener('load', checkColorAndExecute);
})();


这个油猴脚本会在页面加载完成后执行 checkColorAndExecute 函数,检查 <con> 元素内 <font> 标签的颜色属性。如果颜色等于 008800 ,则执行对应的 onclick 事件。

请注意,你需要根据实际情况修改 @match 字段,使脚本仅在需要的网站上运行。在这个示例中,它匹配了所有网站( *://*/* ),所以请确保将其更改为适合你需求的URL模式。

halemorning 发表于 2023-4-30 17:07:35

isdkz 发表于 2023-4-30 17:01
你的浏览器能装油猴插件吧?

以下是一个适用于你需求的油猴脚本示例:


大佬这个我该怎么用,没接触过油猴脚本

halemorning 发表于 2023-4-30 17:09:35

isdkz 发表于 2023-4-30 17:01
你的浏览器能装油猴插件吧?

以下是一个适用于你需求的油猴脚本示例:


糖果游戏浏览器,可以添加外部辅助,但是只能输入外部辅助网址

isdkz 发表于 2023-4-30 17:10:51

halemorning 发表于 2023-4-30 17:07
大佬这个我该怎么用,没接触过油猴脚本

油猴(Tampermonkey)是一款用户脚本管理器扩展,可以让你在浏览器上安装并运行脚本来自定义网页的行为。

1. 首先,你需要安装Tampermonkey扩展。请根据你的浏览器选择合适的链接(如果你的浏览器有对应的插件商店,则在你的浏览器的插件商店安装,没有的话你可以试试打开 Edge 那个商店的链接安装):
   · Chrome: https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo
   · Firefox: https://addons.mozilla.org/en-US/firefox/addon/tampermonkey/
   · Microsoft Edge: https://microsoftedge.microsoft.com/addons/detail/tampermonkey/iikmkjmpaadaobahmlepeloendndfphd
   · Safari: https://apps.apple.com/us/app/tampermonkey/id1482490089

   打开链接后,点击“添加”或“安装”按钮,根据提示完成扩展安装。

2. 安装扩展后,浏览器工具栏上会出现一个Tampermonkey图标。点击图标,然后点击“创建新脚本”。

3. 在新标签页中,删除Tampermonkey为你生成的默认脚本内容,然后将我提供的脚本代码复制粘贴到编辑器中。

4. 修改 @match 字段,使脚本仅在需要的网站上运行。例如,如果你想让脚本仅在 example.com 域名下的所有页面运行,可以将 @match 字段更改为:
      @match      *://*.example.com/*
   

5. 点击保存按钮(通常是一个磁盘图标)以保存脚本。

现在脚本已保存并安装在Tampermonkey中。当你访问 @match 字段指定的URL时,油猴脚本会自动运行。在本例中,它会检查 <con> 元素内 <font> 标签的颜色属性。如果颜色等于 008800 ,则执行对应的 onclick 事件。

如果你需要禁用或管理脚本,可以点击浏览器工具栏上的Tampermonkey图标,然后选择“已安装的用户脚本”进行操作。

halemorning 发表于 2023-4-30 17:36:22

isdkz 发表于 2023-4-30 17:10
油猴(Tampermonkey)是一款用户脚本管理器扩展,可以让你在浏览器上安装并运行脚本来自定义网页的行为。 ...

我刚刚试了试 糖果游戏浏览器 没有拓展 但是可以添加外部插件,插件还得是exe格式

halemorning 发表于 2023-4-30 18:06:18

isdkz 发表于 2023-4-30 17:10
油猴(Tampermonkey)是一款用户脚本管理器扩展,可以让你在浏览器上安装并运行脚本来自定义网页的行为。 ...

大佬,我认命了,换了个百分浏览器,这个可以用油猴了!

isdkz 发表于 2023-4-30 18:29:37

halemorning 发表于 2023-4-30 18:06
大佬,我认命了,换了个百分浏览器,这个可以用油猴了!

好吧,既不能装浏览器插件,也不能用油猴脚本,确实也没什么方法了,关键是开发者工具都搞不出来

halemorning 发表于 2023-4-30 18:39:06

isdkz 发表于 2023-4-30 18:29
好吧,既不能装浏览器插件,也不能用油猴脚本,确实也没什么方法了,关键是开发者工具都搞不出来

换了个浏览器,安装上了油猴插件,正在研究,您给出的代码很有用,但是还需要加个重复执行和延迟2秒

isdkz 发表于 2023-4-30 18:42:29

halemorning 发表于 2023-4-30 18:39
换了个浏览器,安装上了油猴插件,正在研究,您给出的代码很有用,但是还需要加个重复执行和延迟2秒

你可以参考一下这个:

为了实现每2秒重复执行脚本,你可以使用 setInterval 函数。以下是修改后的油猴脚本:

// ==UserScript==
// @name         检查颜色并执行onclick
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description检查颜色并执行onclick
// @author       You
// @match      *://*/*
// @grant      none
// ==/UserScript==

(function() {
    'use strict';

    function checkColorAndExecute() {
      const conElement = document.querySelector('con');

      if (conElement) {
            const fontElement = conElement.querySelector('font');
            const color = fontElement && fontElement.getAttribute('color');

            if (color === '008800') {
                const targetLink = conElement.querySelector('a:not()');

                if (targetLink) {
                  targetLink.click();
                }
            }
      }
    }

    // 设置每2秒执行一次 checkColorAndExecute 函数
    setInterval(checkColorAndExecute, 2000);
})();


这个版本的脚本将每隔2秒(2000毫秒)执行一次 checkColorAndExecute 函数。你只需将这段代码复制并粘贴到Tampermonkey中替换原有脚本即可。

请注意, setInterval 会持续执行,直到页面关闭或者浏览器停止运行。在某些情况下,你可能需要考虑使用 setTimeout 代替 setInterval ,以便在执行一定次数后停止执行脚本。

halemorning 发表于 2023-5-1 10:26:24

isdkz 发表于 2023-4-30 18:42
你可以参考一下这个:

为了实现每2秒重复执行脚本,你可以使用 setInterval 函数。以下是修改后的油猴 ...

(function() {
    'use strict';

    function checkColorAndExecute() {


            const conElement = document.querySelector('con');

            if (conElement) {
                const fontElement = conElement.querySelector('font');
                const color = fontElement && fontElement.getAttribute('color');

                if (color === '008800') {
                  const targetLink = conElement.querySelector('a:not()');

                  if (targetLink) {
                        targetLink.click();
                  }
                }
            }
                var iframes = document.getElementsByTagName('frame');
      if (iframes.length > 0) {
            var iframe = iframes; // 获取第一个iframe
            var frameWindow = iframe.contentWindow; // 获取iframe内的window对象
      }
            frameWindow.useCut(11);

      const ok =document.getElementById('popup_ok');
      if (ok){
            ok.click();
      }
      }
      // 设置每2秒执行一次 checkColorAndExecute 函数
      setInterval(checkColorAndExecute, 6000);
    })();

大佬还得麻烦您帮忙修改一下,这个是我后来又添加了一点点东西,但是上面判断颜色那里似乎出了些小问题,

<con>选择要分解的装备:<br><a href="#" onclick="p.cmd('helptools 73425726');return false;"><img src="/img/arm/armor/ring/rare/lv038/HuoHouZhiJie.gif" border="0" style="margin-right:5px;"><b><font color="0070DD">[火喉巨戒]</font></b></a> <a href="#" onclick="p.cmd('foo fenjie 73425726');return false;">[分解]</a><br><a href="#" onclick="p.cmd('helptools 73425728');return false;"><img src="/img/arm/armor/ring/uncommon/lv042/ZhiLiYinShiZhiHuan.gif" border="0" style="margin-right:5px;"><b><font color="008800">[智力印石指环]</font></b></a> <a href="#" onclick="p.cmd('foo fenjie 73425728');return false;">[分解]</a><br><a href="#" onclick="p.cmd('helptools 73425781');return false;"><img src="/img/arm/armor/hands/plate/uncommon/lv035/GuLangJianDingShouTao.gif" border="0" style="margin-right:5px;"><b><font color="008800">[孤狼坚定手套]</font></b></a> <a href="#" onclick="p.cmd('foo fenjie 73425781');return false;">[分解]</a><br></con>

这里如果判断第一个颜色不是008800就不会判断下面的了,导致下面的没有被判定到,从而一直卡在那里了
页: [1]
查看完整版本: 判断网页某段代码里的颜色,并执行相应代码里的onclick应该怎么实现