Madivad
Madivad

Reputation: 3337

programmatic bookmarks in chrome

Is it possible to set bookmarks that can compile their own variables?

For example, I want to bookmark a site that has links like:

https://example.com/review?from=2017/07/16&to=2017/07/16

At the moment when I click on it: it currently and always will populate with that date.

I want it to always populate with today's date.

Therefore I would like a bookmark that can retrieve the latest system date using javascript or any method that works.

As an aside and totally not important, ideally I would like a 'little' intelligence and to use a bit of smarts in that if the current time is before noon, the date populated would be yesterday. noon is arbitrary, 8am or any other morning hour could be used.

My only idea at the moment is to create an html page with the links in them, open the page and use js to modify the links onload or onclick, but I'd much prefer these pages on the bookmarks bar. (I only have 3 I wish to set up)

Upvotes: 3

Views: 3202

Answers (2)

Howzieky
Howzieky

Reputation: 829

If you want to open the link https://example.com/review?from=2017/07/16&to=<TODAYS DATE>, this takes two steps: creating the Javascript, then putting it into a bookmark, and both steps are easy.

The code to generate the date is as follows:

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!

var yyyy = today.getFullYear();
if(dd<10){
    dd='0'+dd;
} 
if(mm<10){
    mm='0'+mm;
}
var date = yyyy+"/"+mm+"/"+dd;

Code found here

Now we just simplify this and put it into a bookmark. Simplifying this code to one line looks like this:

var today = new Date();var dd = today.getDate();var mm = today.getMonth()+1;var date="https://example.com/review?from=2017/07/16&to="+today.getFullYear()+'/'+((mm<10)?'0'+mm:mm)+'/'+((dd<10)?'0'+dd:dd);

Now we remove all of the var keywords and format it for a bookmark link. We also change var date=... to window.location=..., which will cause the page to open the link that it generates:

javascript:link=today=new Date();dd=today.getDate();mm=today.getMonth()+1;window.location="https://example.com/review?from=2017/07/16&to="+today.getFullYear()+'/'+((mm<10)?'0'+mm:mm)+'/'+((dd<10)?'0'+dd:dd);

Put that last code block into a new bookmark as the link, and that'll work!

Upvotes: 3

PRMoureu
PRMoureu

Reputation: 13347

You could use localStorage to achieve that. Depending on your browser options, it could be saved from a session to another, even after shutdown.

You can try this demo bookmarklets (paste the code in the bookmark's URL field):

javascript:(function() { localStorage.setItem('lastdate', '2017/07/16'); })();

and to retrieve the data :

javascript:(function() { alert(localStorage.getItem('lastdate')); })();

Then you could use this kind of engine to compose another URL when you click on your bookmark :

javascript:(function() {var dat = localStorage.getItem('lastdate'); window.open('https://example.com/review?from='+dat);})();

Upvotes: 1

Related Questions