开发者

Javascript/Greasemonkey: search for something then set result as a value

开发者 https://www.devze.com 2022-12-24 10:20 出处:网络
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 answe

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.

U开发者_开发百科sing 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.


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号