Uncertain about the issue at hand, upon inspecting the code (as reverse-engineered), it appears to be fine. If you are the creator of the bookmarklet, please ensure that you provide the complete source code.
- Consider including the protocol in the script URL.
- Additionally, verify that the jQuery version is compatible with your Safari/Android browser.
Note: As an alternative, you may choose to discard jQuery and utilize promises instead. Explore the 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);
})();
Re-minified using 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)}();
Using the fetch API
Although ~17% longer after minification, this method does not rely on jQuery. It also includes a YouTube video ID extraction feature, enhancing its robustness.
The following script is a UserScript designed for use with Greasemonkey, Tampermonkey, or 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';
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));
}
})();
Minified:
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))}}();