How to make a script that searches an area of a page for text and gives warning if found?

38 views
Skip to first unread message

Sean Smith

unread,
Aug 26, 2020, 5:44:48 PM8/26/20
to greasemonkey-users
Hi,

I'm trying to make a script so that when something on eBay needs to be sent first class, I get some kind of warning when viewing the address label page. The relevant part of the HTML is this:

<div class="buyer__info__items"><h3 class="buyer_info__header"><span>Selected</span></h3><span class="buyer_info__secondary buyer__paid-service">Royal Mail 2nd Class Letter / Large Letter</span></div>

I need to make it so that when it says "1st Class", I get a pop-up or the background of that section flashes yellow or something.

I found this on StackExchange:
var searcharea = jQuery('h2').parent('div').text(); var searchstring = "superstring"; if( searcharea.indexOf( searchstring ) != -1 ) alert("exchange alert to your own things");

If I change h2 to h3 and "superstring" to "1st Class", I figured I'd get a warning but that doesn't work. Could anyone please tell me if they know what I'm doing wrong? Thanks!

The entire script is:

// ==UserScript==
// @name     eBay order first class postage alert
// @include https://gslblui.ebay.co.uk/gslblui/new_label?*
// @version  1
// @grant    none
// ==/UserScript==
var searcharea = jQuery('h3').parent('div').text();
var searchstring = "1st Class";
if( searcharea.indexOf( searchstring ) != -1 )
    alert("Send this item first class");

Jenna

unread,
Aug 26, 2020, 7:06:36 PM8/26/20
to greasemonkey-users
Hi,

jQuery is undefined.

I think you need to import it somehow or use the jQuery from the web page.

LWChris

unread,
Aug 31, 2020, 5:19:55 AM8/31/20
to greasemon...@googlegroups.com

Hi,

libraries like jQuery aren't accessible by default. You have to @require the jQuery library (from the Google Hosted Libraries https://developers.google.com/speed/libraries#jquery, for example), and then define it.

I always use a little "template" around my code to have a very clean separation of what I am doing vs. the page's code, so they don't interfere with each other:

// ==UserScript==
// @name     Script name
// @version  1.0
// @author   LWChris
// @include 
// @require  https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js


// @grant    none

// ==/UserScript==

(function() {
  // Your constants here
 
  var $ = jQuery.noConflict();
 
  _addStyle = function() {
    let style = $('<style type="text/css">\
</style>');
    $("head").append(style);
  };
 
  _init = function() {
    _addStyle();
    // Your initializations here
  };
 
  // Your methods here
 
  _init();
})();

Since your application is quite basic, you can remove the _addStyle method and its call. You can utilize the "load" event to run the check when the page is ready loading its content. Secondly, I'd try to make the jQuery selector as specific as possible.

So your script could look like this (I haven't tested it though since I don't use eBay):

// ==UserScript==
// @name     eBay order first class postage alert


// @grant    none

// ==/UserScript==

(function() {
  const selector =
".buyer__paid-service"; // The element with the class name buyer__paid-service
  const searchString = "1st Class"; // The content to check for
  const warningMessage = "
Send this item first class"; // The message that'll be displayed
 
  var $ = jQuery.noConflict();
 
  _init = function() {
    $(document).on("load", check);
  };
 

  var check = function() {
    if (getSearchContent().includes(searchString.to
LowerCase()) {
      showWarning();
    }
  };
 
  var getSearchContent = function() {
    return
$(selector).text().toLowerCase();
  };
 
  var showWarning = function() {
    alert(warningMessage);
  };
 
  _init();
})();

Best regards
Chris

--
You received this message because you are subscribed to the Google Groups "greasemonkey-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to greasemonkey-us...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/greasemonkey-users/15397ce4-8285-4f19-a4db-c973568a921an%40googlegroups.com.

Sean Smith

unread,
Aug 31, 2020, 8:07:33 PM8/31/20
to greasemonkey-users
Hi. Thanks very much for writing this, Chris. Unfortunately, it doesn't seem to be working for me. Any idea how I can troubleshoot it? I have Firefox version 80.0 which is apparently the latest. Should I be getting a dialogue box for which I have to click OK? Thanks, Sean

LWChris

unread,
Sep 2, 2020, 6:43:21 PM9/2/20
to greasemon...@googlegroups.com

Hi Sean,

yes, you can troubleshoot with these steps:

  1. Add console.log("eBay script: <replace function name here>"); as first statement in all the functions
  2. Open a page on eBay that you think should trigger the script
  3. Open the web console (Ctrl Shift K)
    • It's a new window.
    • It will probably already contain lots and lots of messages, but don't worry.
    • Most of them have accumulated over time; for modern web pages, almost every click adds some messages, so the log grows very fast.
    • We're just going to look at the ones that'll be added from now
  1. Deselect all quick filters except for "Error"
  2. Clear the log
  3. Go back to the eBay tab and reload the page
  4. Go back to the console window
    • It'll hopefully not contain too many errors
    • Some might appear, for example if an AdBlocker removed content from the page, and the native eBay scripts try to access them
    • If you see any errors from Greasemonkey, that's your first hint. Maybe I made a typo.
  1. If you don't see any errors:
    • Enable all quick filters again.
    • Filter for "eBay script" (that's why we put that prefix there)
    • You'll now see if any function gets called at all.
    • If not, make sure your URL filter actually matches the page
    • If some are working, you hopefully find out which method is the last one that's called.
  1. You can probably find out what's going on by making more calls to console.log with more information in the output and reloading the page

Feel free to report back your findings in case you get stuck.

Best regards
Chris

Reply all
Reply to author
Forward
0 new messages