如何使用bookmarklet在新选项卡中启动函数?

时间:2022-10-07 09:04:13

This is my first time taking a shot at JavaScript, so excuse me if it seems a bit rough.

这是我第一次尝试JavaScript,如果感觉有点粗糙,请见谅。

This website http://www.keepvid.com/ provides a bookmark with JavaScript that allows you to download video off of YouTube and other media sites by clicking on it when on the media page. However, I found that instead of opening the download page in a new tab, it opens it in the current one, interrupting the media (probably to encourage you to buy their pro version).

这个网站http://www.keepvid.com/提供了一个带有JavaScript的书签,你可以在媒体页面上点击它,从YouTube和其他媒体网站上下载视频。然而,我发现它不是在一个新的标签页中打开下载页面,而是在当前的标签页中打开下载页面,中断媒体(可能是为了鼓励你购买他们的专业版)。

Seeing it as a chance to try out JavaScript (as I have only used bookmarklets before), I have been searching for answers, but the ones I found seem to only have worked with older versions of the website, as the JavaScript seems updated.

我认为这是一次尝试JavaScript的机会(因为我以前只使用过bookmarklet),所以我一直在寻找答案,但我发现的这些似乎只适用于网站的旧版本,因为JavaScript似乎已经更新了。

I have been trying to make a bookmarklet that opens a new page, and then loads the download page for the media.

我一直在尝试制作一个bookmarklet,打开一个新页面,然后加载媒体的下载页面。

The first code I encountered as a solution was: javascript:void(window.open('http://keepvid.com/?url='+escape(window.location)));

我遇到的第一个解决方案代码是:javascript:void(window.open('http://keepvid.com/?url='+escape(window.location)));

But this didn't work, which was when I noticed that the bookmarklet's final URL added &bm=y to the end of the URL, so I edited it to:
javascript:void(window.open('http://keepvid.com/?url='+escape(window.location)+'&bm=y'));

但这并没有起作用,当我注意到bookmarklet的最终URL添加了&bm=y到URL的末尾时,我将其编辑为:javascript:void(window.open, 'http://keepvid.com/?url='+escape(window.location)+'&bm=y');

I then looked at the bookmarklet's original code::
javascript: (a%20=%20(b%20=%20document).createElement('script')).src%20=%20'http://keepvid.com/js/bm.js',%20b.body.appendChild(a);void(0);

然后,我查看了bookmarklet的原始代码::javascript:(a%20=%20(b%20=%20文档).createElement('script')).src%20=%20'http://keepvid.com/js/bm.js',%20b.body.appendChild(a);

and realized that it was calling on an external script (I believe) and that it would have to stay intact.

并意识到它正在调用一个外部脚本(我相信),并且它必须保持完整。

And from their I have been trying various methods at getting this to work, and so far, this is what I have: javascript:void(window.open(window.location));void((a%20=%20(b%20=%20document).createElement('script')).src%20=%20'http://keepvid.com/js/bm.js',%20b.body.appendChild(a));

从他们那里,我尝试了各种方法来让它工作,到目前为止,我有:javascript:void(window.open(window.location);void(a%20=%20(b%20=%20).createElement('script')).src%20=% 20b. http:/ keepvid.com/bm.com/20a)。

This script opens a new tab, but the new tab will only be a copy of the YouTube video, and the original tab will load the download page, which still interrupts the media, rendering my work useless, if slightly more convenient.

这个脚本打开一个新的选项卡,但是新的选项卡将仅仅是YouTube视频的一个拷贝,而原始的选项卡将加载下载页面,它仍然会中断媒体,使我的工作变得毫无用处,如果稍微方便一点的话。

I have been trying every way I can, but it seems like I can't have a second script in the same bookmarklet without it pointing to the original tab it was called in.

我一直在尽我所能地尝试,但似乎我不可能在同一个bookmarklet中拥有第二个脚本而不指向它所调用的原始选项卡。

So, I have been wondering what it is I need to do in order to: 1. Create a new tab 2. Have the new tab load the download page (instead of the original) 3. Leave the original tab alone and use it only to capture the window.location

所以,我一直在想,为了:1,我需要做什么。创建一个新的选项卡2。让新选项卡加载下载页面(而不是原来的)3。不要使用原始选项卡,只使用它来捕获windows .location

If this is impossible to do, is their a way to have a bookmarklet in the toolbox that makes a copy of the original page and then points to a bookmarklet in the bookmarks menu that contains the redirect to the download page?

如果这是不可能做到的,那么他们的方法是在工具箱中有一个bookmarklet,使其复制原始页面,然后指向书签菜单中的bookmarklet,其中包含重定向到下载页面的内容吗?

3 个解决方案

#1


3  

Copy this text: http://keepvid.com/js/bm.js

复制这段文字:http://keepvid.com/js/bm.js

Change

改变

<form method='post' 

to

<form method='post' target='_blank'

You can host the file on your own server and append the script in the same manner as the original bookmarklet, or you can compress the code into a stand alone bookmarklet like this

您可以在自己的服务器上托管该文件,并以与原始bookmarklet相同的方式附加该脚本,也可以将代码压缩为一个独立的bookmarklet,如下所示

javascript:eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return%20r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new%20RegExp('\\b'+e(c)+'\\b','g'),k[c]);return%20p}('o%20i(a,b,c){N{7%20u=a.v(b);7%20w=u[1].v(c);x%20w[0]}O(P){x\'\'}}o%20p(){7%20q=d.Q("R");q.9="<z%20S=\'T\'%20U=\'V\'%20W=\'r://A.g/?B="+C(h.j)+"&X=y\'%20Y=\'D\'><k%20l=\'s\'%20t=\'Z\'%20m=\'"+8+"\'%20/><k%20l=\'s\'%20t=\'2\'%20m=\'"+2+"\'%20/><k%20l=\'s\'%20t=\'5\'%20m=\'"+5+"\'%20/><k%2010=\'11-12-E%20F-G-13%20F-G-14\'%20l=\'H\'%20m=\'15%2016%2017\'%20/></z>";d.e.19(q);h.1a["D"].H()}7%20d=h;6(d.j.I.1b(/J.g/i)){7%208=i(d.e.9,\';8=\',\'&\');6(8==\'\')8=i(d.e.9,\'"8":%20"\',\'"\');7%202=i(d.e.9,\'2"%201c="\',\'"\');6(2==\'\')2=i(d.e.9,\'1d"%202="\',\'"\');7%205=i(d.e.9,\'n=\',\'&\');6(5==\'\')5=i(3.f,\'n":%20"\',\'"\').K("\\\\/","/");6(5==\'\'){7%203=1e;6(!3&&1f%20L!=\'1g\'){3=E%20L()}3.1h("1i","r://1j.J.g/1k?8="+8+"&5=18",1l);3.1m=o(){6(3.1n==4){2=i(3.f,\'&2=\',\'&\');6(2==\'\')2=i(3.f,\'2=\',\'&\');5=i(3.f,\'n=\',\'&\');6(5==\'\')5=i(3.f,\'n":%20"\',\'"\').K("\\\\/","/");p()}};3.1o()}M{p()}}M{h.j.I=\'r://A.g/?B=\'+C(1p.j)}',62,88,'||title|kvajax||fmt|if|var|video_id|innerHTML|||||body|responseText|com|document||location|input|type|value|fmt_stream_map|function|finishkv|gd|http|hidden|name|a1|split|a2|return||form|keepvid|url|escape|kvdl|new|yt|uix|submit|href|youtube|replace|XMLHttpRequest|else|try|catch|err|createElement|div|method|post|target|blank|action|bm|id|vid|class|master|sprite|button|tooltip|Download|with|KeepVid||appendChild|forms|match|content|ltr|false|typeof|undefined|open|GET|www|get_video_info|true|onreadystatechange|readyState|send|window'.split('|'),0,{}))

I packed it using this: http://javascriptcompressor.com/

我使用http://javascriptcompressor.com/打包它

For treating non-youtube sites in the same manner, also change

对于非youtube网站,同样的处理方式也会改变

document.location.href='http://keepvid.com/?url='+escape(window.location);

to

window.open('http://keepvid.com/?url='+escape(window.location));

#2


1  

This question is a bit old, but my little bookmarklet to open dict.cc in a new tab fits perfectly to the question title.

这个问题有点旧了,但是我的小书签以打开一个新的选项卡中的dict.cc非常适合问题标题。

javascript:(function(){window.open("http://www.dict.cc/?s="+encodeURIComponent(prompt("Enter word to look for:")))})();

I'm not sure what the wrapper function does, but without it, the current tab will get cleared.

我不确定包装器函数的作用,但是没有它,当前选项卡将被清除。

Src: https://www.mattcutts.com/blog/javascript-bookmarklet-basics/

Src:https://www.mattcutts.com/blog/javascript-bookmarklet-basics/

#3


0  

The first code I encountered as a solution was: javascript:void(window.open('http://keepvid.com/?url='+escape(window.location)));

我遇到的第一个解决方案代码是:javascript:void(window.open('http://keepvid.com/?url='+escape(window.location)));

But this didn't work

但这没有工作

Did not work in what way? If the browser is blocking popups, you will not succeed. If it does not, that method should work.

没有以什么方式工作?如果浏览器阻塞了弹出窗口,您将不会成功。如果没有,那么这个方法应该是有效的。

Here is a possible other method - still not guaranteed to work any better

这里有一个可能的其他方法——仍然不能保证更好地工作

You need to store a script on your server. The script is loaded into the current window using

您需要在服务器上存储一个脚本。脚本被加载到当前窗口中

(function() {var scr=document.createElement('script');scr.src='http://www.yourserver.com/yourserverprocess.php?loc=escape(window.location.href;document.getElementByTagname('head')[0].appendChild(scr); })();

then on your server you can return (assuming php)

然后在服务器上返回(假设php)

header("content-type: text/javascript");
echo "window.open('http://yourserver.com/convert.php?loc=".$_GET["loc"],'_blank')";

#1


3  

Copy this text: http://keepvid.com/js/bm.js

复制这段文字:http://keepvid.com/js/bm.js

Change

改变

<form method='post' 

to

<form method='post' target='_blank'

You can host the file on your own server and append the script in the same manner as the original bookmarklet, or you can compress the code into a stand alone bookmarklet like this

您可以在自己的服务器上托管该文件,并以与原始bookmarklet相同的方式附加该脚本,也可以将代码压缩为一个独立的bookmarklet,如下所示

javascript:eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return%20r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new%20RegExp('\\b'+e(c)+'\\b','g'),k[c]);return%20p}('o%20i(a,b,c){N{7%20u=a.v(b);7%20w=u[1].v(c);x%20w[0]}O(P){x\'\'}}o%20p(){7%20q=d.Q("R");q.9="<z%20S=\'T\'%20U=\'V\'%20W=\'r://A.g/?B="+C(h.j)+"&X=y\'%20Y=\'D\'><k%20l=\'s\'%20t=\'Z\'%20m=\'"+8+"\'%20/><k%20l=\'s\'%20t=\'2\'%20m=\'"+2+"\'%20/><k%20l=\'s\'%20t=\'5\'%20m=\'"+5+"\'%20/><k%2010=\'11-12-E%20F-G-13%20F-G-14\'%20l=\'H\'%20m=\'15%2016%2017\'%20/></z>";d.e.19(q);h.1a["D"].H()}7%20d=h;6(d.j.I.1b(/J.g/i)){7%208=i(d.e.9,\';8=\',\'&\');6(8==\'\')8=i(d.e.9,\'"8":%20"\',\'"\');7%202=i(d.e.9,\'2"%201c="\',\'"\');6(2==\'\')2=i(d.e.9,\'1d"%202="\',\'"\');7%205=i(d.e.9,\'n=\',\'&\');6(5==\'\')5=i(3.f,\'n":%20"\',\'"\').K("\\\\/","/");6(5==\'\'){7%203=1e;6(!3&&1f%20L!=\'1g\'){3=E%20L()}3.1h("1i","r://1j.J.g/1k?8="+8+"&5=18",1l);3.1m=o(){6(3.1n==4){2=i(3.f,\'&2=\',\'&\');6(2==\'\')2=i(3.f,\'2=\',\'&\');5=i(3.f,\'n=\',\'&\');6(5==\'\')5=i(3.f,\'n":%20"\',\'"\').K("\\\\/","/");p()}};3.1o()}M{p()}}M{h.j.I=\'r://A.g/?B=\'+C(1p.j)}',62,88,'||title|kvajax||fmt|if|var|video_id|innerHTML|||||body|responseText|com|document||location|input|type|value|fmt_stream_map|function|finishkv|gd|http|hidden|name|a1|split|a2|return||form|keepvid|url|escape|kvdl|new|yt|uix|submit|href|youtube|replace|XMLHttpRequest|else|try|catch|err|createElement|div|method|post|target|blank|action|bm|id|vid|class|master|sprite|button|tooltip|Download|with|KeepVid||appendChild|forms|match|content|ltr|false|typeof|undefined|open|GET|www|get_video_info|true|onreadystatechange|readyState|send|window'.split('|'),0,{}))

I packed it using this: http://javascriptcompressor.com/

我使用http://javascriptcompressor.com/打包它

For treating non-youtube sites in the same manner, also change

对于非youtube网站,同样的处理方式也会改变

document.location.href='http://keepvid.com/?url='+escape(window.location);

to

window.open('http://keepvid.com/?url='+escape(window.location));

#2


1  

This question is a bit old, but my little bookmarklet to open dict.cc in a new tab fits perfectly to the question title.

这个问题有点旧了,但是我的小书签以打开一个新的选项卡中的dict.cc非常适合问题标题。

javascript:(function(){window.open("http://www.dict.cc/?s="+encodeURIComponent(prompt("Enter word to look for:")))})();

I'm not sure what the wrapper function does, but without it, the current tab will get cleared.

我不确定包装器函数的作用,但是没有它,当前选项卡将被清除。

Src: https://www.mattcutts.com/blog/javascript-bookmarklet-basics/

Src:https://www.mattcutts.com/blog/javascript-bookmarklet-basics/

#3


0  

The first code I encountered as a solution was: javascript:void(window.open('http://keepvid.com/?url='+escape(window.location)));

我遇到的第一个解决方案代码是:javascript:void(window.open('http://keepvid.com/?url='+escape(window.location)));

But this didn't work

但这没有工作

Did not work in what way? If the browser is blocking popups, you will not succeed. If it does not, that method should work.

没有以什么方式工作?如果浏览器阻塞了弹出窗口,您将不会成功。如果没有,那么这个方法应该是有效的。

Here is a possible other method - still not guaranteed to work any better

这里有一个可能的其他方法——仍然不能保证更好地工作

You need to store a script on your server. The script is loaded into the current window using

您需要在服务器上存储一个脚本。脚本被加载到当前窗口中

(function() {var scr=document.createElement('script');scr.src='http://www.yourserver.com/yourserverprocess.php?loc=escape(window.location.href;document.getElementByTagname('head')[0].appendChild(scr); })();

then on your server you can return (assuming php)

然后在服务器上返回(假设php)

header("content-type: text/javascript");
echo "window.open('http://yourserver.com/convert.php?loc=".$_GET["loc"],'_blank')";