Max
Max

Reputation: 159

Firefox keyword search using javascript

I want to use a travel planner making full use of Firefox' Keyword Searches.

The keyword search basically just inputs a string somewhere in a predefined URL, e.g. google.com/search?q=%s with cats and dogs will go to https://www.google.com/search?hl=en&q=cats%20and%20dogs.

Q1: However, the URL needs the (current) date, so this needs to be entered variably as well.

Because we need a starting location and final location we need to enter two search string, which is possible (explained here).

Q2: I would also like to be able to e.g. enter 'h' for my home address.

The URL format of the travel planner is as follows:

https://www.ns.nl/reisplanner/#/?vertrek=STARTLOCATION%20Centraal&vertrektype=treinstation&aankomst=ENDLOCATION&aankomsttype=treinstation&type=vertrek&tijd=2023-03-03T15:59&firstMileModality=PUBLIC_TRANSPORT&lastMileModality=PUBLIC_TRANSPORT&disabledTransportModalities=

I currently have:

javascript:var%C2%A0s='%s'; 
url='https://www.ns.nl/reisplanner/#/?vertrek=%s&vertrektype=treinstation&aankomst=%s&aankomsttype=treinstation&type=vertrek&tijd=currentDateInRightFormat&firstMileModality=PUBLIC_TRANSPORT&lastMileModality=PUBLIC_TRANSPORT&disabledTransportModalities='; 
const h='My Home Street 1 My Home Town'
const w='My Work Street 1 Work City'
const currentDateInRightFormat = new Date(new Date().toString().split('GMT')[0]+' UTC').toISOString().split('.')[0].slice(0,16));
t=''; 
qc=0; 
chunks=url.split('%s'); 

for(i=0; i<s.length; i++){
    if(s.charAt(i)=='%22')qc=qc^1; 
    t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i)); 
}
args=t.split(/\s/); 

nurl=''; 

for(i=0; i<chunks.length; i++){
    nurl+=chunks[i]; 
    if(args[i]!=undefined)%C2%A0{
        args[i]=args[i].replace(/\^/g,' '); 
        nurl+=args[i]; 
    }
}
location.replace(nurl,'< BR>');


So I would like to be able to:

Upvotes: 0

Views: 62

Answers (1)

Professor Abronsius
Professor Abronsius

Reputation: 33813

Given that I do not believe you are able to read the address bar content using Javascript the closest I came to doing what I think you are trying (?) to do was as follows:

javascript:let home='Forfar';let work='Dundee';
let args={
    'aankomstvertrektype':'treinstation',
    'aankomsttype':'treinstation',
    'type':'vertrek',
    'tijd':(new Date(new Date().toString().split('GMT')).toISOString().substring(0,16)),
    'firstMileModality':'PUBLIC_TRANSPORT',
    'lastMileModality':'PUBLIC_TRANSPORT',
    'disabledTransportModalities':''
};
args.vertrek=prompt('Enter start location.\nUse "h" for "Home"',home);
args.aankomst=prompt('End End location.\nUse "w" for work.',work);
if(args.vertrek=='h')args.vertrek=home;
if(args.aankomst=='w')args.aankomst=work;
const endpoint='https://www.ns.nl/reisplanner/#/';
let url=new URL(endpoint);
url.search=new URLSearchParams(args).toString();
location.href=url;

When added to the Firefox bookmarklet doodah thingy it is then invoked by typing the keyword in the address bar and then supplying content when prompted - h and w can be used and will be substituted for the defined values of home and work. The default values are already set as home and work for start/end

Not speaking dutch or knowing locations to use I am not 100% sure that this is functioning as hoped

Upvotes: 1

Related Questions