Javascript/Greasemonkey:搜索某些内容然后将结果设置为值
好吧,当谈到 JS 时,我有点不懂(我不是最伟大的程序员),所以请温柔点 - 特别是如果我的问题已经在某个地方被问过,而我太愚蠢而无法找到正确的答案。抛开自嘲,我们进入正题。
问题
我和一大群朋友经常使用一个网站,它没有显示我们可能想知道的所有信息 - 在本例中是一个航空公司预订网站和旅行舱位。
虽然信息隐藏在页面代码中,但它不会在任何地方显示给用户。
使用 Greasemonkey 脚本,我想释放这条信息并以合适的格式显示它。
这是我想要做的事情的伪代码。
- 在 dom 中搜索指定元素
- 定义变量
- 查找文本字符串
- 如果找到
- 将结果设置为变量
- 将内容写入页面的特定位置(指定 div 之前)
- 如果未找到
- 则不执行任何操作
我认为到目前为止我已经实现了大部分目标,除了以下关键位
: string:页面需要在页面中搜索下面一段文字 HEAD:
mileageRequest += "&CLASSES=S,S-S,S-S";
我需要提取并存储的内容在第二个等号(=)和最后一个逗号(")之间。该区域的内容可以是AZ 之间的任何字母
我都不介意将其拆分为数组,因此我可以在这个阶段将结果单独使用:
- 找到的文本写入另一个位置。
将 >到目前为止的代码
这就是我到目前为止所提出的,其中突出显示了缺失的部分,
buttons = document.getElementById('buttons');
''Search goes here
var flightClasses = document.createElement("div");
flightClasses.innerHTML = '<div id="flightClasses"> ' +
'<h2>Travel classes</h2>' +
'For the above segments, your flight classes are as follows:' +
'write result here' +
'</div>';
main.parentNode.insertBefore(flightClasses, buttons);
如果有人可以帮助我,或者为我指明完成此任务的正确方向,我将不胜感激。
Ok, I'm a bit of a n00b when it comes to JS (I'm not the greatest programmer) so please be gentle - specially if my questions been asked already somewhere and I'm too stupid to find the right answer. Self deprecation out of the way, let's get to the question.
Problem
There is a site me and a large group of friends frequently use which doesn't display all the information we may like to know - in this case an airline bookings site and the class of travel.
While the information is buried in the code of the page, it isn't displayed anywhere to the user.
Using a Greasemonkey script, I'd like to liberate this piece of information and display it in a suitable format.
Here's the psuedocode of what I'm looking to do.
- Search dom for specified element
- define variables
- Find a string of text
- If found
- Set result to a variable
- Write contents to page at a specific location (before a specified div)
- If not found
- Do nothing
I think I've achieved most of it so far, except for the key bits of:
Searching for the string: The page needs to search for the following piece of text in the page HEAD:
mileageRequest += "&CLASSES=S,S-S,S-S";
The Content I need to extract and store is between the second equals (=) sign and the last comma ("). The contents of this area can be any letter between A-Z.
I'm not fussed about splitting it up into an array so I could use the elements individually at this stage.
- Writing the result to a location: Taking that found piece of text and writing it to another location.
Code so far
This is what I've come up so far, with bits missing highlighted.
buttons = document.getElementById('buttons');
''Search goes here
var flightClasses = document.createElement("div");
flightClasses.innerHTML = '<div id="flightClasses"> ' +
'<h2>Travel classes</h2>' +
'For the above segments, your flight classes are as follows:' +
'write result here' +
'</div>';
main.parentNode.insertBefore(flightClasses, buttons);
If anyone could help me, or point me in the right direction to finish this off I'd appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的意思是“位于第二个等号 (=) 和最后一个引号 ( “)”?
我假设这个:
在脚本标签中?
如果是这样,那么页面上似乎会有一个名为
mileageRequest
的 JS 变量,您可以使用unsafeWindow.mileageRequest
从 Greasemonkey 访问该变量,并假设您可以访问您的数据想要类似的东西:或者你可以尝试:
Do you mean "is between the second equals (=) sign and the last quote (")"?
And I assume that this:
is in a script tag?
If so then it looks like there will be a JS variable on the page called
mileageRequest
which you can access from Greasemonkey withunsafeWindow.mileageRequest
and assuming that you can access the data you want with something like:or you can try: