书签在ios或Android浏览器上不起作用



我有这个代码,它是一个书签:

javascript:(function(e,a,g,h,f,c,b,d){if(!(f=e.jQuery)||g>f.fn.jquery||h(f)){c=a.createElement("script");c.type="text/javascript";c.src="cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.js";c.onload=c.onreadystatechange=function(){if(!b&&(!(d=this.readyState)||d=="loaded"||d=="complete")){h((f=e.jQuery).noConflict(1),b=1);f(c).remove()}};a.documentElement.childNodes[0].appendChild(c)}})(window,document,"3.4.1",function($,L){var url1 = "download-video-youtube1.p.rapidapi.com/mp3/";var url2 = window.location.href.substring(32);var url3 = url1 + url2;var settings = { "url": url3 , "method": "GET", "headers": {"x-rapidapi-host": "download-video-youtube1.p.rapidapi.com","x-rapidapi-key": "[my apikey here]" } }; $.ajax(settings).done(function(response){ window.location.href = "https://" + response.vidInfo[0].dloadUrl;});});

它在Firefox和chrome上工作得很好,但它无法与ios safari或ios快捷方式或Android上的chrome一起使用。

它检查是否有特定版本的jquery,如果没有,它会将其附加到DOM中,然后运行api请求并返回YouTube任何视频的Mp3下载链接。

这是我的第三个也是最后一个问题,因为到目前为止我还没有收到答案。 我知道这次我也不会,没关系我把它作为日记留在这里。

再见

不确定可能是什么问题,代码看起来不错(因为我已经进行了逆向工程(。如果您创建了书签,请包含完整的源代码。

  • 也许您需要在脚本 url 中包含协议?
  • 另外,请确保jQuery的版本与您的Safari/Android浏览器兼容。

注意:如果你愿意,你可以把jQuery扔进垃圾桶并使用promises。看看 Fetch API。

(function() {
let script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js";
script.onload = script.onreadystatechange = function(e) {
$.ajax({
"url": "download-video-youtube1.p.rapidapi.com/mp3/" + window.location.href.substring(32),
"method": "GET",
"headers": {
"x-rapidapi-host": "download-video-youtube1.p.rapidapi.com",
"x-rapidapi-key": "[my apikey here]"
}
}).done(function(response) {
window.location.href = "https://" + response.vidInfo[0].dloadUrl;
});
};
document.documentElement.childNodes[0].appendChild(script);
})();

使用 javascript-minifier.com 重新缩小:

javascript:!function(){let e=document.createElement("script");e.type="text/javascript",e.src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js",e.onload=e.onreadystatechange=function(e){$.ajax({url:"download-video-youtube1.p.rapidapi.com/mp3/"+window.location.href.substring(32),method:"GET",headers:{"x-rapidapi-host":"download-video-youtube1.p.rapidapi.com","x-rapidapi-key":"[my apikey here]"}}).done(function(e){window.location.href="https://"+e.vidInfo[0].dloadUrl})},document.documentElement.childNodes[0].appendChild(e)}();

使用抓取接口

它长了~17%(缩小后(,但不依赖于jQuery。它还具有YouTube视频ID提取功能,因此更加强大。

下面的脚本是一个 UserScript,可以与 Greasemonkey、Tampermonkey 或 Violentmonkey 一起使用。

// ==UserScript==
// @name         YouTube MP3
// @namespace    com.youtube.mp3
// @version      1.0.0
// @description  Parse the YouTube video ID and request the MP3 version.
// @author       Mr. Polywhirl
// @match        https://www.youtube.com/*
// @match        https://youtube.com/*
// @match        https://youtu.be/*
// @grant        GM_log
// ==/UserScript==
(function() {
'use strict';
// See: https://stackoverflow.com/a/8260383/1762224
const ytUrlParser = (url) => {
var regExp = /^.*((youtu.be/)|(v/)|(/u/w/)|(embed/)|(watch?))??v?=?([^#&?]*).*/;
var match = url.match(regExp);
return (match && match[7].length == 11) ? match[7] : false;
}
const videoId = ytUrlParser(window.location.href);
if (videoId) {
const reqUrl = 'download-video-youtube1.p.rapidapi.com/mp3/' + videoId;
const reqHead = new Headers();
reqHead.append('x-rapidapi-host', 'download-video-youtube1.p.rapidapi.com');
reqHead.append('x-rapidapi-key', '[my apikey here]');
const reqObj = new Request(reqUrl, {
method: 'GET',
headers: reqHead,
mode: 'cors',
cache: 'default',
});
fetch(reqObj)
.then(function(response) {
if (!response.ok) { throw Error(response.statusText); }
return response;
})
.then(response => response.vidInfo[0].dloadUrl)
.then(url => { window.location.href = "https://" + url })
.catch(error => console.log(error));
}
})();

缩小:

javascript:!function(){"use strict";const e=(o=window.location.href,!(!(t=o.match(/^.*((youtu.be/)|(v/)|(/u/w/)|(embed/)|(watch?))??v?=?([^#&?]*).*/))||11!=t[7].length)&&t[7]);var o,t;if(e){const o="download-video-youtube1.p.rapidapi.com/mp3/"+e,t=new Headers;t.append("x-rapidapi-host","download-video-youtube1.p.rapidapi.com"),t.append("x-rapidapi-key","[my apikey here]");const a=new Request(o,{method:"GET",headers:t,mode:"cors",cache:"default"});fetch(a).then(function(e){if(!e.ok)throw Error(e.statusText);return e}).then(e=>e.vidInfo[0].dloadUrl).then(e=>{window.location.href="https://"+e}).catch(e=>console.log(e))}}();

最新更新