Shoutbox

Greasemonkey message box / redirect - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: Greasemonkey message box / redirect (/showthread.php?tid=93554)

Greasemonkey message box / redirect by Jimbo on 01-16-2010 at 11:14 PM

Is there a way to look for a word on a page using Greasemonkey, and if that word appears anywhere on the page, either display a message box, that the user has to click OK, or just redirect to another page instantly?


RE: Greasemonkey message box / redirect by andrey on 01-17-2010 at 12:21 AM

how about

JS code:
if (/someword/m.test(document.body.innerText)) alert("OMFG")
but that would include any other inline stuff in the source (js in <script> tags, css etc)
RE: Greasemonkey message box / redirect by Jimbo on 01-17-2010 at 12:52 AM

Well, I've managed to come up with this, although it spams the alert for every instance of the word. How could I make it only alert once?

js code:
    // ==UserScript==
    // @name          Alert
    // @namespace    --
    // @description   Alert
    // @include   *
     
    // ==/UserScript==
     
    (function() {
     
    const WORD = {
      "test": {"color": "red"},
    };
     
    function highlightText() {
     
      var allTextNodes = document.evaluate('//text()', document, null,
                                           XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
                                           null);
     
      for (var i = 0; i < allTextNodes.snapshotLength; i++) {
        var ele = allTextNodes.snapshotItem(i);
        for (var key in WORD) {
       
          if (ele.nodeValue.toLowerCase().indexOf(key) != -1) {
            var span = document.createElement("span");
            ele.parentNode.replaceChild(span, ele);
            span.appendChild(ele);
       
            for (var css in WORD[key]) {
              span.style[css] = WORD[key][css];
              alert("test");
         
            }
          }
        }
      }
    }
     
      highlightText();
    })();
Also, what if the word wasnt actually hardcoded into the page source, but retrieved from a session, or something, how could I detect the word then?
RE: Greasemonkey message box / redirect by stoshrocket on 01-17-2010 at 11:44 AM

To prevent alert spamming you could just build in a variable in place of the alert that is changed to 1 when a word is found, then after the search has been made, check the variable, if = 1, alert...

js code:
    // ==UserScript==
    // @name          Alert
    // @namespace     http://sammyservers.com
    // @description   Alert
    // @include   *
     
    // ==/UserScript==
     
    (function() {
     
    const WORD = {
      "test": {"color": "red"},
    };
   
    //build in the global variable...
    var j = 0;
     
    function highlightText() {
     
      var allTextNodes = document.evaluate('//text()', document, null,
                                           XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
                                           null);
     
      for (var i = 0; i < allTextNodes.snapshotLength; i++) {
        var ele = allTextNodes.snapshotItem(i);
        for (var key in WORD) {
       
          if (ele.nodeValue.toLowerCase().indexOf(key) != -1) {
            var span = document.createElement("span");
            ele.parentNode.replaceChild(span, ele);
            span.appendChild(ele);
       
            for (var css in WORD[key]) {
              span.style[css] = WORD[key][css];
              //changes the global variable to 1
              j = 1;         
            }
          }
        }
      }
    }
      highlightText();
      if(j = 1){
        alert("test");
      }
    })();

I'm, not too strong on the javascript front so the code might not be solid, but in theory it should work!

As for searching the word outside of the source I have no clue...
RE: Greasemonkey message box / redirect by Jimbo on 01-17-2010 at 01:38 PM

Oh yeah, don't know why I didnt think of doing that. Thanks a lot.

IF anyone has any suggestions on how to search for the word outside of the page source, I would be highly grateful.


RE: Greasemonkey message box / redirect by stoshrocket on 01-17-2010 at 03:52 PM

quote:
Originally posted by Jimbo
Oh yeah, don't know why I didnt think of doing that. Thanks a lot.

IF anyone has any suggestions on how to search for the word outside of the page source, I would be highly grateful.
I don't actually get what you're trying to do tbh, I understand about looking for a word in a page source, but when would you find, or even look, for a word outside of that? Example of where you're trying to find the word?
RE: Greasemonkey message box / redirect by Jimbo on 01-17-2010 at 04:46 PM

Well, this is for a friend of mine, and its for a game called Pokemon battle arena. He wants it to alert him when the word "shiny" is found in the page, but since this isnt hardcoded, and the URL has a random set of arguments for each pokemon encounter, I was wondering if there would be a way to scan the page body, even if the word wasnt in the source.


RE: Greasemonkey message box / redirect by stoshrocket on 01-17-2010 at 05:23 PM

quote:
Originally posted by Jimbo
Well, this is for a friend of mine, and its for a game called Pokemon battle arena. He wants it to alert him when the word "shiny" is found in the page, but since this isnt hardcoded, and the URL has a random set of arguments for each pokemon encounter, I was wondering if there would be a way to scan the page body, even if the word wasnt in the source.
Ah, I've just taken a look at the game. I see what you mean about it not actually being in the source, but I can see where the text is meant to appear in the source, it's just been hidden. Hmm... I'm not too sure at the moment, if I think of a method I'll let you know.
RE: RE: Greasemonkey message box / redirect by Jimbo on 01-17-2010 at 05:40 PM

quote:
Originally posted by stoshrocket
quote:
Originally posted by Jimbo
Well, this is for a friend of mine, and its for a game called Pokemon battle arena. He wants it to alert him when the word "shiny" is found in the page, but since this isnt hardcoded, and the URL has a random set of arguments for each pokemon encounter, I was wondering if there would be a way to scan the page body, even if the word wasnt in the source.
Ah, I've just taken a look at the game. I see what you mean about it not actually being in the source, but I can see where the text is meant to appear in the source, it's just been hidden. Hmm... I'm not too sure at the moment, if I think of a method I'll let you know.
Okay then, thank you very much.
RE: Greasemonkey message box / redirect by andrey on 01-17-2010 at 06:42 PM

just to make sure i'm getting this right, this is where the "shiny whatever" will appear?
[Image: attachment.php?pid=985063]


RE: Greasemonkey message box / redirect by Jimbo on 01-17-2010 at 07:02 PM

Yes, it will say Shiny in the section with the level and pokemon, and IIRC, it even says, "Wild Shiny Appeared".


RE: Greasemonkey message box / redirect by kezz on 01-17-2010 at 10:01 PM

Slightly offtopic, but Jimbo: Your site has been very well designed and implemented. Did you code it all yourself? It's very clean, efficient and problem-free. If I didn't get ridiculous pings to UK servers I'd play yours.


RE: Greasemonkey message box / redirect by Jimbo on 01-17-2010 at 10:08 PM

You think so? really? A friend of mine did the basic design, with the one simple theme, then I added all the content and different themes myself. I personally don't like it much, but hey, it gets the job done for what it has to do.


RE: Greasemonkey message box / redirect by andrey on 01-17-2010 at 11:32 PM

Well, I've looked a bit into how the page handles those shiny things and came up with a script that should show an alert when a shiny pokemon is found. But I can't figure out how to get it working in this hackish greasemonkey piece of shit, I can't get it to replace the state_change_navi function with my modified one. But you get the idea. (this script works as a UserJS in Opera btw)

Of course I can't say this will work 100% because I haven't come across a shiny pokemon, but if you take a look at the state_Change_navi() function in navi_php.js you'll see what I was aiming at in there.


RE: Greasemonkey message box / redirect by Jimbo on 01-18-2010 at 12:10 AM

Thanks andrey, I'll get my friend to download Opera and try that out later.

EDIT: He just told me it didnt work. He passed over 2 shiny pokemon, and there was no alert.


RE: Greasemonkey message box / redirect by andrey on 01-18-2010 at 12:29 AM

didn't work? =/
well, did he set up a folder to put the userjs in (tools>preferences>advanced>content>javascript options) and removed the '.txt'? :p

i guess it was a bit of a stab in the dark, i might give it another try later on


RE: Greasemonkey message box / redirect by Jimbo on 01-18-2010 at 12:49 AM

Yeah, I made sure he did install it correctly before reporting it didn't work :P