书签打开多个URL无效 [英] Bookmarklet to open multiple urls not working
问题描述
s = document.selection?document.selection.createRange().text:window.getSelection?window.getSelection().toString():document.getSelection?document.getSelection():'';如果(s ==''){s = prompt(``您%20did%20not%20not%20select%20any%20text%20to%20search%20for.%20Enter%20the%20text%20to%20search%20for%20for'20,','');}如果{window.open("https://www.google.com/" + s);setTimeout(function(){window.open('https://youtube.com/'+s,"_blank");},3000);setTimeout(function(){window.open('https://url3.com/'+s,"_blank");},9000);setTimeout(function(){window.open('https://url4.com/'+s,"_blank");},12000);};
新文本Document.txt用新的windows.txt显示.'''
因此,检索所选文本的一般方法是 window.getSelection().toString();
您似乎自己已经做到了.但是,似乎您的第三行在提示和开括号之后紧跟了一个错误类型,一个额外的单引号;修复后,代码开始工作.
无论如何,有几个更多调整可以使代码更好:
- 将文本提示为纯文本,不转义空格等.
- 最好不要使用全局变量,例如
s
;因此,最好至少具有var s
. - 位格式(
=
后的空格,到处都是单引号,而不是双引号来组成统一的代码)
修改版本:
var s = document.selection?document.selection.createRange().text:window.getSelection?window.getSelection().toString():document.getSelection?document.getSelection():'';if(s ==''){s =提示(您没有选择要搜索的任何文本.请输入要搜索的文本.",'');}如果{window.open('https://www.google.com/'+s);setTimeout(function(){window.open('https://youtube.com/'+s,'_blank');},3000);setTimeout(function(){window.open('https://url3.com/'+s,'_blank');},9000);setTimeout(function(){window.open('https://url4.com/'+s,'_blank');},12000);};
而且,顺便说一句,如果要将其作为小标签(如标签中的标签),则您的代码应位于其中,并另存为小书签(浏览器用JS代码添加书签):
javascript:(function(){/*上面的代码*/})();
ive been working on the below for a while. It should work i think... but wont. i know i will have to disable chromes adblock on whatever page i run it on but i cant see where ive gone wrong. Its intended use is to highlight text then run bookmarklet which will open 4 urls using that text at the end of the url. any help is greatly appreciated. '''
s=document.selection?document.selection.createRange().text:window.getSelection?window.getSelection().toString():document.getSelection?document.getSelection():'';
if(s==''){
s=prompt(''You%20did%20not%20select%20any%20text%20to%20search%20for.%20Enter%20the%20text%20to%20search%20for%20:','');
}
if(s)
{
window.open("https://www.google.com/"+s);
setTimeout(function(){
window.open('https://youtube.com/'+s,"_blank");
}, 3000);
setTimeout(function(){
window.open('https://url3.com/'+s,"_blank");
}, 9000);
setTimeout(function(){
window.open('https://url4.com/'+s,"_blank");
}, 12000);
};
New Text Document.txt Displaying with new windows.txt. '''
So in a general way of retrieving selected text is window.getSelection().toString();
It looks like you have almost made it yourself. However, it seems like your third line has a minor mistype, an extra single quotation mark, right after prompt and an opening bracket; after the fix the code starts working.
Anyway, several more tweaks to make the code even better:
- Prompt text as plain text, without escaping spaces etc.
- It is better not to use global variables, like
s
; therefore, it might be better to have at leastvar s
. - A bit formatting (spaces after
=
, single quotation marks everywhere instead of double quotes to make uniform code)
A modified version:
var s = document.selection?document.selection.createRange().text:window.getSelection?window.getSelection().toString():document.getSelection?document.getSelection():'';
if(s == ''){
s = prompt('You did not select any text to search. Enter the text to search for.','');
}
if(s)
{
window.open('https://www.google.com/'+s);
setTimeout(function(){
window.open('https://youtube.com/'+s,'_blank');
}, 3000);
setTimeout(function(){
window.open('https://url3.com/'+s,'_blank');
}, 9000);
setTimeout(function(){
window.open('https://url4.com/'+s,'_blank');
}, 12000);
};
And, by the way, if you want to make it as a bookmarklet, as it is in your tag, then your code should be inside this and saved as a bookmarklet (a browser bookmark with js-code):
javascript:(function () {
/*code above here*/
})();
这篇关于书签打开多个URL无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!