2

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)

cmaher
  • 5,100
  • 1
  • 22
  • 34
Madivad
  • 2,999
  • 7
  • 33
  • 60
  • I can't think of a built-in way to accomplish this, but you could probably create a program (using any general-purpose language) which is scheduled to run every 24 hours on your computer which updates the URL of the bookmark in your bookmarks folder with the new date. You will need to look up where Chrome stores the bookmarks folder in your file system. – 4castle Jul 16 '17 at 02:45
  • actually, that's not a bad idea! If there are no other avenues to achieve this goal, I might actually just look at that (possibly create an extension?) – Madivad Jul 16 '17 at 03:25
  • So should the first date be static, and the second one always changing? Or should they both always be the same? Or should the first date always be the the date of the last time the bookmark was clicked? – Howzieky Jul 16 '17 at 21:59
  • That's not important. It can lock in anything, just so long as it uses today's date (and I'll have a couple others as well for a week range etc) when required. – Madivad Jul 17 '17 at 04:09
  • The only idea that's working in my head ATM is to use an `html` custom built home page which has all the links built into it (assuming I refresh it before it's needed). – Madivad Jul 17 '17 at 04:11

2 Answers2

2

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!

Howzieky
  • 829
  • 7
  • 18
  • Yep, that's it. That's exactly what I was looking for. It's annoys me when someone finds the answer on stack, because believe me, I've been working on this since last night and obviously just not using the right search terms. Thanks, this is great! – Madivad Jul 17 '17 at 07:30
  • just to add, it always amazes me what people can fit into bookmarklets – Madivad Jul 17 '17 at 07:31
  • No problem! You can fit any javascript into a bookmark if you know how to format it! – Howzieky Jul 17 '17 at 22:59
1

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);})();
PRMoureu
  • 12,817
  • 6
  • 38
  • 48
  • I did some playing with this today but I need it to be more current (ie pull out today's date). I almost had it done, well I actually *did* get it working, but didn't finish making all the desired links. And the Howzieky came alone and posted a complete solution. Thanks for your reply (I didn't know about local storage). – Madivad Jul 17 '17 at 07:29