1

In my Electron app I have a webview. If I click a link (e.g. on Google) and then move back, the link turns not purple, like in normal browsers.

Can I activate this behaviour in Electron too? Or do it programmatically, if I would store the browsing history by myself?

I guess it is somehow connected that the history support in Electron is not yet the best?

Would this maybe possible with Muon fork of Electron?

flori
  • 14,339
  • 4
  • 56
  • 63
  • Definitely let me know if that answer doesn't do it for you, or if you need more help with it :) It should be completely reliable, but I don't know your use case I can't verify that. – Clonkex Jul 05 '17 at 03:18
  • @Clonkex I my case it will be a real history, in extreme cases for with a year of daily browsing. So I am worried about the scaling … – flori Jul 05 '17 at 13:42
  • I hadn't considered scalability; I've edited my answer with extra information (and made a slight performance improvement by moving `window.location.href` outside the loops). – Clonkex Jul 06 '17 at 00:22

1 Answers1

2

As you can see from the comments at about line 16 of this file, the Electron guys/girls have created their own navigation solution. It would seem this doesn't handle a:visited properly.

Instead, we can easily create our own solution. Add the following function to all of your renderer scripts and ensure you put simuHistory(); at the start of every page:

function simuHistory() {
    var localstorageSimuHistory = localStorage.getItem('simuHistory');
    var simuHistory = localstorageSimuHistory ? JSON.parse(localstorageSimuHistory) : [];
    var found = false;
    var windowHref = window.location.href;
    for(let i=0;i<simuHistory.length;i++) {
        if(simuHistory[i] == windowHref) {
            found = true;
            break;
        }
    }
    if(found === false) {
        simuHistory[simuHistory.length] = windowHref;
        localStorage.setItem('simuHistory', JSON.stringify(simuHistory));
    }
    var elements = document.getElementsByTagName('a');
    for(let i=0;i<elements.length;i++) {
        for(let h=0;h<simuHistory.length;h++) {
            if(elements[i].href == simuHistory[h]) {
                elements[i].className += ' visited';
            }
        }
    }
}

And add the following CSS to your stylesheet:

.visited {
    color: purple;
}

Alternatively, if you don't want to include the CSS (or want it all self-contained), you could replace this line:

elements[i].className += ' visited';

...with this line:

elements[i].style.color = 'purple';

Scalability

The TL;DR is that unless you have more than around 25,000 fairly long unique URLs in your app, you don't need to worry about scalability.

The visited URLs are stored in localStorage. This is limited in size, and therefore limited in the number of URLs we can store. I believe in Electron this is limited to around 5MB. I tested performance by adding 25,000 additional URLs to the simuHistory array using the following code:

for(var i = 0; i < 25000; i++) {
    simuHistory[simuHistory.length] = 'https://stackoverflow.com/questions/417142/what-is-the-maximum-length-of-a-url-in-different-browsers' + Math.floor((Math.random() * 1000000) + 1);
}

The URL was chosen because it was fairly long. The random number on the end actually affects nothing at all; I just included it to add some extra length. Then I timed running simuHistory() on my crappy slow laptop (with no SSD):

25,000 URLs == ~210ms
 1,000 URLs == ~13ms

In my opinion that's plenty fast for nearly any Electron app, but it depends on your use case as to how many URLs you might end up with.

Clonkex
  • 3,373
  • 7
  • 38
  • 55
  • Thanks! At least this makes clear that out of the box, it is not possible. I would love to use the real :visited state (a `check-visited-state` event would be wonderful), but I suspect it will not be possible. – flori Jul 05 '17 at 12:26
  • @flori I did spend a fair amount of time trying to find a way to get `:visited` to work normally but I couldn't do it :( It might be worth filing it as a bug for electron. Also, when you say "a `check-visited-state` event would be wonderful", are you hinting I might add something to my code? ;) I'd be totally happy to add some kind of event but I don't know what an event like that would do. – Clonkex Jul 05 '17 at 22:36
  • Thanks for your efforts! Also regarding the Scaling. No, the `check-visited-state` event idea was not about your code. – flori Jul 15 '17 at 18:41