Safari extension pop-up warning

Discussion in 'Mac Programming' started by aaronvan, Jan 5, 2017.

  1. aaronvan Suspended

    aaronvan

    Joined:
    Dec 21, 2011
    Location:
    República Cascadia
    #1
    My 89-year old father-in-law is in the early stages of dementia and uses his credit card to buy a lot of random unnecessary crap online.

    I wrote this Safari .js extension to pop-up a warning when he opens a new window, warning him about using his credit card.

    Code:
    function watchOut() {
                var msg = "DON'T USE THAT CREDIT CARD!";
                alert(msg)
            }
            watchOut();
    
    This is really annoying popping up all the time. I’d like to modify it to just call the function when he tries entering numbers in a text field (i.e. uses his credit card) or something else to that effect. I’m wondering if anyone has had this experience with elders and computers and/or has suggestions?
     
  2. aaronvan thread starter Suspended

    aaronvan

    Joined:
    Dec 21, 2011
    Location:
    República Cascadia
    #2
    This works, except it works every time a key in pressed in a text field. :) It should probably occur only once on each instance of a web page opening or reloading. Not sure how to do that. I've never written JavaScript before and I can't say I much like it.

    Code:
    function watchOut() {
        alert("DON'T USE THAT CREDIT CARD!")
        }
    document.onkeypress = function(){watchOut()};
    
     
  3. ardchoille50 macrumors 68020

    Joined:
    Feb 6, 2014
    #3
    Can JS do nested functions? Something like window.onload and then document.onkeypress? If not, then perhaps a "do when" statement?
     
  4. aaronvan thread starter Suspended

    aaronvan

    Joined:
    Dec 21, 2011
    Location:
    República Cascadia
    #4
    I'm not sure about JavaScript and nested functions, but this code works. It calls the functions and sets the event to "null" so it doesn't occur again (i.s. once on each page.)

    Code:
    function watchOut() {
        alert("DON'T USE THAT CREDIT CARD!")
        }
    
    document.onkeypress = function() {
      document.onkeypress = null;
        watchOut();
    };
     
  5. ardchoille50 macrumors 68020

    Joined:
    Feb 6, 2014
    #5
    So, it acts a reliable reminder yet avoids being an annoyance? That sounds perfect for your needs.
     

Share This Page