Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple chrome.webNavigation.onHistoryStateUpdated not working

The goal of this is to have my extension wait for a change in the History, depending on what it says, do a certain action.

Here is what I have so far

popup.js

chrome.tabs.update({ url: "https://www.WEBSITE.com/messages" });

chrome.webNavigation.onHistoryStateUpdated.addListener(function(details) {
    if (details.url.indexOf("messages") >= 0) {
        chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, {
            file: 'getInboxStats.js'
        });;
    } else {//if (details.url.indexOf("match") >= 0) {
        chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, {
            file: 'startBotting.js'
        });;
    }
});

chrome.runtime.onMessage.addListener(function(message) {
    if (message.type == "emptyAmount") {
        emptyAmount = message.content;
        if (!(percentageOfMessages > 0)) {
            percentageOfMessages = 50;
        }
        amountToSend = Math.floor(emptyAmount * (percentageOfMessages / 100));
        alert(amountToSend);
        chrome.tabs.update({ url: "https://www.WEBSITE.com/match" });
    }

});

getInboxStats.js

var currentAmount = document.getElementsByClassName('count')[1].innerHTML;
var maxAmount = document.getElementsByClassName('total')[0].innerHTML;
var emptyAmount = maxAmount - currentAmount;

chrome.runtime.sendMessage({ content: emptyAmount, type: "emptyAmount" });

startBotting.js

alert("TEST");

The issue I have is that the getInboxStats.js starts, but it's like the onHistoryStateUpdated only seems to work once because the file startBotting.js never displays an alert that says 'TEST'

like image 541
Dgameman1 Avatar asked Nov 25 '25 22:11

Dgameman1


1 Answers

You misunderstand the purpose of onHistoryStateUpdated.

It captures the instances of history state manipulation without navigation via History API as opposed to regular navigation. When you call update({url: "..."}) it's a regular navigation.

If you're really concerned about browser history updates, you should be using chrome.history.onVisited.

If you want to use webNavigation API to capture regular navigations, you should use onCommitted event.

You should also look into chrome.tabs API.

like image 70
Xan Avatar answered Nov 28 '25 13:11

Xan