Firefox关键字搜索使用JavaScript

huangapple go评论74阅读模式
英文:

Firefox keyword search using javascript

问题

我想使用一个旅行规划器,充分利用Firefox的关键字搜索

关键字搜索基本上只是将一个字符串输入到预定义的URL的某个位置,例如,google.com/search?q=%scats and dogs将转到https://www.google.com/search?hl=en&q=cats%20and%20dogs

Q1: 但是,URL需要(当前的)日期,因此这也需要以可变方式输入。

因为我们需要一个起始位置最终位置,我们需要输入两个搜索字符串,这是可能的(在这里有解释)。

Q2: 我还想能够输入“h”来表示我的家庭地址。

旅行规划器的URL格式如下:

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=

我目前有:

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&#39); 

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>');

所以我想要能够:

  • 在URL中输入currentDateInRightFormat
  • 使用字符串'h',然后输入我的预定义家庭地址,字符串'w'表示工作地址等。
英文:

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:

  • enter the currentDateInRightFormat in the URL
  • use the string 'h' which then inputs my predefined home address, string 'w' for work, etc.

答案1

得分: 1

鉴于我认为你无法使用Javascript读取地址栏内容,我尝试以我认为你试图做的方式进行操作如下:

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('输入起始位置。\n使用"h"表示"Home"',home);
args.aankomst=prompt('输入结束位置。\n使用"w"表示工作。',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;

当添加到Firefox的书签小工具后,可以在地址栏中键入"keyword",然后按提示输入内容 - 可以使用"h"和"w",它们将替代定义的"home"和"work"的默认值。由于我不懂荷兰语,也不了解要使用的位置,所以无法百分之百确定它是否按预期运行。

英文:

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

huangapple
  • 本文由 发表于 2023年3月4日 00:30:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定