The relevant portion of the code is below. Basically, I look for websites that have an alert() function call whose message contains certain words, such as a toll-free phone number, and redirect them. The problem is, I am getting inconsistent behavior. I know the extension settings are working OK, because the first alert in my test code below always shows up. However, when testing it on websites which the extension should catch, it only works some of the time. Sometimes, the page loads and I never see any of my test alerts. Sometimes, I see the test alerts, but it doesn't pop up with the alert showing that it found the phrase, even though I know the phrase exists on the page. Yet other times, all the alerts show up as expected, but the redirect (window.location.href, at the bottom) does not occur. Any suggestions on what tweaks I need to make to the code?
function checkForURLs() {
var currentURL = window.location.href;
var scamBlocked = false;
for(var i = 0; i < targetURLs.length; i++)
if(currentURL.toString().indexOf(targetURLs[i]) >= 0 && currentURL.toString().indexOf('https://mysite.com/scam-blocked') < 0 && currentURL.toString().indexOf('apis.google.com') < 0)
{
window.stop();
window.location.href = 'https://mysite.com/scam-blocked?url=' + currentURL.toString();
}
if (scamBlocked == false) {
if (settings.aggressiveMode == true) {
alert('Aggressive Mode is on!');
// Get all script nodes.
//var scripts = document.querySelectorAll('script');
//alert(scripts.length);
//for (var i = 0; i < scripts.length; ++i) {
//var js = scripts[i].innerHTML;
var js = document.body.innerHTML;
alert(js);
// Look for an alert function call.
alert('Checking for phrases');
var foundFirst = js.match(/alert\(/m);
var foundSecond = js.match(/suspicious/im);
var foundThird = js.match(/detected/im);
var foundFourth = js.match(/virus/im);
var foundFifth = js.match(/security/im);
var foundTollFreeFirst = js.match(/800/m);
var foundTollFreeSecond = js.match(/888/m);
var foundTollFreeThird = js.match(/877/m);
var foundTollFreeFourth = js.match(/866/m);
var foundTollFreeFifth = js.match(/855/m);
var foundTollFreeSixth = js.match(/844/m);
alert('Done checking');
var shouldKill = (foundFirst.length > 0 && (foundSecond.length > 0 || foundThird.length > 0 || foundFourth.length > 0 || foundFifth.length > 0) && (fountTollFreeFirst.length > 0 || fountTollFreeSecond.length > 0 || fountTollFreeThird.length > 0 || fountTollFreeFourth.length > 0 || fountTollFreeFifth.length > 0 || fountTollFreeSixth.length > 0));
if (foundFirst.length > 0) alert('alert\( was found.');
if (foundSecond.length > 0) alert('suspicious was found.');
if (foundThird.length > 0) alert('detected was found.');
if (foundFourth.length > 0) alert('virus was found.');
if (foundFifth.length > 0) alert('security was found.');
if (foundTollFreeFirst.length > 0) alert('800 was found.');
if (foundTollFreeSecond.length > 0) alert('888 was found.');
if (foundTollFreeThird.length > 0) alert('877 was found.');
if (foundTollFreeFourth.length > 0) alert('866 was found.');
if (foundTollFreeFifth.length > 0) alert('855 was found.');
if (foundTollFreeSixth.length > 0) alert('844 was found.');
if (shouldKill) {
window.stop();
window.location.href = 'https://mysite.com/scam-blocked';
break;
}
//}
}
}
}