Javascript/Greasemonkey:搜索某些内容然后将结果设置为值

发布于 2024-08-26 09:03:29 字数 1289 浏览 6 评论 0原文

好吧,当谈到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2024-09-02 09:03:29

我需要提取和存储的内容位于第二个等号 (=) 和最后一个逗号 (") 之间。

您的意思是“位于第二个等号 (=) 和最后一个引号 ( “)”?

我假设这个:

mileageRequest += "&CLASSES=S,S-S,S-S";

在脚本标签中?

如果是这样,那么页面上似乎会有一个名为 mileageRequest 的 JS 变量,您可以使用 unsafeWindow.mileageRequest 从 Greasemonkey 访问该变量,并假设您可以访问您的数据想要类似的东西:

// check that the mileageRequest variable exists
if(unsafeWindow.mileageRequest){
  // it exists
  var myString = unsafeWindow.mileageRequest.match(/&CLASSES=([^&=]*)/i);
  if(myString){
    // my string exists
    myString = myString[1];
  }
  else{
    // my sting does not exist
  }
}
else {
  // it does not exist
}

或者你可以尝试:

var myString = document.getElementsByTagName('head')[0].innerHTML.match(/mileageRequest\s*\+=\s*"&CLASSES=([^"]*)";/i);
if(myString){
  // my string exists
  myString = myString[1];
}
else{
  // my string does not exist
}

The Content I need to extract and store is between the second equals (=) sign and the last comma (").

Do you mean "is between the second equals (=) sign and the last quote (")"?

And I assume that this:

mileageRequest += "&CLASSES=S,S-S,S-S";

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 with unsafeWindow.mileageRequest and assuming that you can access the data you want with something like:

// check that the mileageRequest variable exists
if(unsafeWindow.mileageRequest){
  // it exists
  var myString = unsafeWindow.mileageRequest.match(/&CLASSES=([^&=]*)/i);
  if(myString){
    // my string exists
    myString = myString[1];
  }
  else{
    // my sting does not exist
  }
}
else {
  // it does not exist
}

or you can try:

var myString = document.getElementsByTagName('head')[0].innerHTML.match(/mileageRequest\s*\+=\s*"&CLASSES=([^"]*)";/i);
if(myString){
  // my string exists
  myString = myString[1];
}
else{
  // my string does not exist
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文