Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aaronvan

Suspended
Original poster
Dec 21, 2011
1,350
9,353
República Cascadia
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?
 

aaronvan

Suspended
Original poster
Dec 21, 2011
1,350
9,353
República Cascadia
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()};
 

ardchoille50

macrumors 68020
Feb 6, 2014
2,142
1,230
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()};
Can JS do nested functions? Something like window.onload and then document.onkeypress? If not, then perhaps a "do when" statement?
 

aaronvan

Suspended
Original poster
Dec 21, 2011
1,350
9,353
República Cascadia
Can JS do nested functions? Something like window.onload and then document.onkeypress? If not, then perhaps a "do when" statement?

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();
};
 
  • Like
Reactions: ardchoille50

ardchoille50

macrumors 68020
Feb 6, 2014
2,142
1,230
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();
};
So, it acts a reliable reminder yet avoids being an annoyance? That sounds perfect for your needs.
 
  • Like
Reactions: aaronvan
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.