0

In my code, I want to check if on the page you are on, if certain requirements are met with the url, do something

It is saying it cannot get the toLowerCase of undefined

this is my code, thanks

Also, I do have tab permissions in the manifest This file runs in the background, I want it to execute for every page you open

var names = ['hunter5deer', 'pielovingcat', '199MS', 'harrdy']
$(function() {
    for (i=0;i<names.length;i++) {
        var currentPage;
        chrome.tabs.getSelected(null,function(tab) {
            currentPage = tab.url;
        });
        var page;
        if (currentPage.toLowerCase().indexOf("item?id=") > -1) {
            page = 'seller';
            name(page, names[i]);

        }
        else if (currentPage.toLowerCase().indexOf("showpost.aspx?postid=") > -1) {
            page = 'forum';
            name(page, names[i]);
        }
        else if (currentPage.toLowerCase().indexOf("user.aspx?") > -1) {
            page = 'profile'
            name(page, names[i]);
        }
    }
});
Sam
  • 85
  • 10

1 Answers1

1

chrome.tabs.getSelected executes asynchronously so the if statement was running before currentPage was set. Move the if statement inside the callback.

var names = ['hunter5deer', 'pielovingcat', '199MS', 'harrdy']
$(function() {
    for (i=0;i<names.length;i++) {
        chrome.tabs.getSelected(null,function(tab) {
            var currentPage = tab.url;
            var page;
            if (currentPage.toLowerCase().indexOf("item?id=") > -1) {
                page = 'seller';
                name(page, names[i]);

            }
            else if (currentPage.toLowerCase().indexOf("showpost.aspx?postid=") > -1) {
                page = 'forum';
                name(page, names[i]);
            }
            else if (currentPage.toLowerCase().indexOf("user.aspx?") > -1) {
                page = 'profile'
                name(page, names[i]);
            }
        });
    }
});
abraham
  • 46,583
  • 10
  • 100
  • 152