首页 > 解决方案 > 书签不适用于 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 下载链接。

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

再见

标签: javascriptjqueryajax

解决方案


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

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

注意:如果需要,可以将 jQuery 扔进垃圾箱并使用 Promise。看看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)}();

使用获取 API

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

下面的脚本是一个用户脚本,可以与 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))}}();

推荐阅读