Reputation: 2435
So I'm trying to write a Google Chrome extension, and so far my code looks like this:
callback_function = function () {
chrome.tabs.create({selected: true, url: "http://www.google.com"});
};
document.addEventListener(chrome.history.onVisited, callback_function());
If I'm reading the documentation correctly, chrome.history.onVisited
is an event that fires whenever someone visits a website, and chrome.tabs.create
creates a new tab with the specified URL. And if I understand correctly, an "event listener" waits for events to be fired, and runs the callback function whenever the event is fired.
So by my logic, this code should create a new Google tab every time I visit a website. But for some reason, it only creates the Google tab when I first reload the extension. Why is that?
Upvotes: 1
Views: 5242
Reputation: 77581
Ouch. No, that's not how Chrome API events work. They are not DOM events.
Read the documentation here: https://developer.chrome.com/extensions/events
In short, you need to take the event object (chrome.history.onVisited
) and call its method, addListener
:
chrome.history.onVisited.addListener(callback_function);
Also, note: you want to pass a reference to the function itself (callback_function
), and not the result of its execution (callback_function()
)
Upvotes: 4