通过点击在Chrome中的Greasemonkey / userscript一个页面上的一个按钮 [英] Clicking a button on a page using a Greasemonkey/userscript in Chrome

查看:240
本文介绍了通过点击在Chrome中的Greasemonkey / userscript一个页面上的一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要去,因为我碰到的是最终没有平移了几个解决方案,以作为绝对冗长这里成为可能。请记住,我不知道的JavaScript 。我知道基本的HTML和CSS。我没有任何实际编程的背景,但我试图通过研究这样的基本任务来学习的点点滴滴。请跟我说话我是个白痴。任何行话我在这个岗位到处乱扔,而我研究这个具体问题的经验教训。我在写这userscript作为一个个人项目,并与一些朋友分享。

I'm going to be as absolutely verbose here as possible as I've run into a few solutions that didn't end up panning out. Please keep in mind that I don't know Javascript. I know basic HTML and CSS. I don't have any actual programming background but I'm trying to learn bit by bit by researching basic tasks like this. Please talk to me like I'm an idiot. Any lingo I throw around in this post I learned while researching this specific issue. I'm writing this userscript as a personal project and to share with some friends.

我试着写了镀铬/ Greasemonkey的一个userscript(Chrome是我的目标浏览器),将点击战地3服务器浏览器上的刷新的按钮。对于那些你不知道的,战地3使用带有一个浏览器插件VOIP,实际上推出通过服务器浏览器游戏配对一个网站。它的大部分似乎是安排在表中相当简单的HTML。

I'm trying to write a userscript for Chrome/Greasemonkey (Chrome is my target browser) that will click the Refresh button on the Battlefield 3 server browser. For those of you that don't know, Battlefield 3 uses a web site paired with a browser plugin for VOIP and actually launching the game via a server browser. The bulk of it seems to be fairly straight forward HTML arranged in tables.

的想法是,查看主页面是全服务器时,该脚本将点击的刷新的按钮,每三秒钟左右,直到页面报告服务器上的空位,然后停止刷新循环,然后点击加入服务器按钮。我已经有运行轮询服务器电流和最大的球员,然后将它们分配给自己变量的脚本的一部分。

The idea is that when viewing the main page for a server that is full, the script will click the Refresh button every three seconds or so until the page reports an open spot on the server, then stop the refresh loop and click the join server button. I've already got the part of the script running that polls the server current and maximum players then assigns them to their own variables.

在这一点上,我试图让一个单击控制台来工作,所以我实际上可以把它在我的脚本使用一些和我有零运气。

At this point I'm trying to get a click to work in the console so I can actually put it to some use in my script and am having zero luck.

这是对于我试图单击从Chrome开发工具拉到按钮DIV:

This is the div for the button that I'm trying to click pulled from the Chrome dev tools:

<div class="serverguide-header-refresh-button alternate show"> 
<div type="reset" class="common-button-medium-grey">
<p style="position: relative;">
<a href="/bf3/servers/show/c7088bdc-2806-4758-bf93-2106792b34d8/">Refresh </a>
</p>
</div>
</div>

(该链接也不是一成不变的,它是一个链接到一个特定的服务器页)

(That link is not static. It's a link to a specific server page)

要真正找到我使用 getElementsByClassName 按钮。它不具有一个唯一的ID,但类是唯一对这个特定网页上的元件,以便 getElementsByClassName(的ServerGuide头刷新按钮)[0] 是拉适当的div各一次。这是越来越脚本在这就是问题的按钮执行任何实际行动上。

To actually find the button I'm using getElementsByClassName. It doesn't have a unique ID but the class is unique to that element on this particular page so getElementsByClassName("serverguide-header-refresh-button")[0] is pulling the proper div each time. It's getting the script to perform any actual action on the button that's the problem.

document.getElementsByClassName("serverguide-header-refresh-button")[0].click();

我现在认识到这一点并没有工作,因为它不是一个常规的提交按钮。我不明白标准的具体细节,但在这里我得到它不支持。点击()方法。

function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// the guts of this userscript
function main() {
  unsafeWindow.jQuery('.serverguide-header-refresh-button')[0].click();
}

// load jQuery and execute the main function
addJQuery(main);

这简直是 unsafeWindow.jQuery('的ServerGuide头刷新按钮。)点击(); 裹在一些code加载用于的jQuery userscripts。这是一个有点我拿起别的地方,但被告知,如果是jQuery的网页上加载它只会工作。我想这是值得一试。这是其中之一的我不知道我在做什么的镜头在黑暗中,并没有奏效。我想下面的同样的事情用jQuery code的另一个片段我拿起

This is simply unsafeWindow.jQuery('.serverguide-header-refresh-button').click(); wrapped in some code to load jQuery for userscripts. It was a bit I picked up elsewhere but was told it would only work if jQuery was loaded on the page. I figured it was worth a try. This is one of those I have no idea what I'm doing shots in the dark and it didn't work. I tried the same thing below with another snippet of jQuery code I picked up:

function addJQuery(callback) {
  var script = document.createElement("script");
  script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
  script.addEventListener('load', function() {
    var script = document.createElement("script");
    script.textContent = "(" + callback.toString() + ")();";
    document.body.appendChild(script);
  }, false);
  document.body.appendChild(script);
}

// the guts of this userscript
function main() {
   var evObj = document.createEvent('Events');
      evObj.initEvent("click", true, false);
      document.getElementsByClassName('serverguide-header-refresh-button')[0].dispatchEvent(evObj);
}

    // load jQuery and execute the main function
addJQuery(main);

这两个回报的未定义的在Chrome和Firebug的控制台。

Both of these return Undefined in the Chrome and Firebug consoles.

所以,会有人这么好心帮我创建一个位code为这个脚本preSS这个页面?

So, would anyone be so kind as to help me create a bit of code for this script to press the Refresh button on this page?

推荐答案

请注意:


  1. jQuery的。点击()不会对那些没有在第一时间通过jQuery设置click事件可靠地工作。

  1. jQuery .click() does not work reliably on click events that were not set via jQuery in the first place.

您需要创建合适的事件; createEvent('活动')是不一样。

You need to create the right kind of event; createEvent('Events') is not the way.

随着吉姆·德维尔指出的那样,没有被选中的链接伪按钮。

As Jim Deville pointed out, the link pseudo-button was not being selected.

您不需要jQuery的这个(到目前为止)。

You do not need jQuery for this (so far).

请确认刷新控制静态加载试验code所示的问题。如果它在AJAXed,点击尝试可能太早火。

Make sure that the "Refresh" control is loaded statically for the test code as shown in the question. If it's AJAXed in, the click attempt may fire too soon.

把所有在一起,下面的code的的工作。但要注意,一些网站(如某些谷歌应用程序)的使用时髦,国家敏感的设计 - 这需要一系列事件的

Putting that all together, the following code should work. But beware that some sites (like certain Google apps) use funky, state-sensitive designs -- which require a sequence of events.

var refreshBtn = document.querySelector (
    "div.serverguide-header-refresh-button div[type='reset'] a"
);
var clickEvent = document.createEvent ('MouseEvents');
clickEvent.initEvent ('click', true, true);
refreshBtn.dispatchEvent (clickEvent);

这篇关于通过点击在Chrome中的Greasemonkey / userscript一个页面上的一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆