|
1 | | -import { getCurrentTab } from "./helpers/utils.js"; |
2 | | - |
3 | 1 | export default { |
4 | 2 | icon: "https://a-v2.sndcdn.com/assets/images/sc-icons/favicon-2cadd14bdb.ico", |
5 | 3 | name: { |
6 | | - en: "Soundcloud - Download music", |
7 | | - vi: "Soundcloud - Tải nhạc", |
| 4 | + en: "Soundcloud - Add download button", |
| 5 | + vi: "Soundcloud - Thêm nút tải nhạc", |
8 | 6 | }, |
9 | 7 | description: { |
10 | | - en: "Download music from soundcloud", |
11 | | - vi: "Tải nhạc trên soundcloud", |
| 8 | + en: "Add download button on soundcloud (before like button). Use soundcloud API, no external service <img style='width:100%' src='/scripts/soundcloud_downloadMusic.png' />", |
| 9 | + vi: "Thêm nút tải nhạc trên soundcloud (trước nút like). Sử dụng trực tiếp soundcloud API <img style='width:100%' src='/scripts/soundcloud_downloadMusic.png' />", |
12 | 10 | }, |
13 | 11 |
|
14 | | - onClickExtension: async function () { |
15 | | - let tab = await getCurrentTab(); |
16 | | - let url = prompt("Nhập link soundcloud: ", tab.url); |
17 | | - if (url == null) return; |
18 | | - }, |
19 | | -}; |
| 12 | + whiteList: ["https://soundcloud.com/*"], |
20 | 13 |
|
21 | | -export const shared = { |
22 | | - downloadSoundCloud: async function (url) { |
23 | | - let client_id = await shared.getApiKey(); |
24 | | - let info = await shared.getResourceInfo(url, client_id); |
25 | | - }, |
26 | | - getApiKey: async function () { |
27 | | - let res = await fetch("https://soundcloud.com"); |
| 14 | + infoLink: |
| 15 | + "https://greasyfork.org/en/scripts/394837-local-soundcloud-downloader", |
28 | 16 |
|
29 | | - let text = await res.text(); |
| 17 | + onDocumentStart: async () => { |
| 18 | + function hook(obj, name, callback) { |
| 19 | + const fn = obj[name]; |
| 20 | + obj[name] = function (...args) { |
| 21 | + callback.apply(this, args); |
| 22 | + fn.apply(this, args); |
| 23 | + }; |
| 24 | + return () => { |
| 25 | + // restore |
| 26 | + obj[name] = fn; |
| 27 | + }; |
| 28 | + } |
30 | 29 |
|
31 | | - // prettier-ignore |
32 | | - let jsUrl = |
33 | | - new RegExp('<script crossorigin="" src="(.+?)"></script>').exec(text)?.[-1] || |
34 | | - new RegExp('<script crossorigin src="(.+?)"></script>').exec(text)?.[-1]; |
| 30 | + const btnId = "ufs-soundcloud-downloadBtn"; |
| 31 | + const downloadBtn = document.createElement("button"); |
| 32 | + downloadBtn.id = btnId; |
| 33 | + downloadBtn.textContent = "Download"; |
| 34 | + downloadBtn.classList.add("sc-button"); |
| 35 | + downloadBtn.classList.add("sc-button-medium"); |
| 36 | + downloadBtn.classList.add("sc-button-responsive"); |
| 37 | + downloadBtn.classList.add("sc-button-download"); |
| 38 | + downloadBtn.classList.add("sc-button-cta"); |
35 | 39 |
|
36 | | - res = await fetch(jsUrl); |
37 | | - let jsCode = await res.text(); |
38 | | - let client_id = new RegExp('client_id:"(.+?)"').exec(jsCode)?.[1]; |
| 40 | + setInterval(() => { |
| 41 | + if (!document.querySelector("#" + btnId)) { |
| 42 | + const par = document.querySelector( |
| 43 | + ".listenEngagement__footer .sc-button-toolbar .sc-button-group" |
| 44 | + ); |
| 45 | + if (par) { |
| 46 | + par.prepend(downloadBtn); |
| 47 | + } |
| 48 | + } |
| 49 | + }, [1000]); |
39 | 50 |
|
40 | | - return client_id; |
41 | | - }, |
42 | | - getResourceInfo: async function (resource_url, client_id) { |
43 | | - let res = await fetch(resource_url); |
44 | | - let text = await res.text(); |
| 51 | + let clientId; |
| 52 | + window.ufs_soundcloud_allData = new Map(); |
| 53 | + |
| 54 | + // listen for request => save track |
| 55 | + hook(XMLHttpRequest.prototype, "open", async (method, url) => { |
| 56 | + const u = new URL(url, document.baseURI); |
| 57 | + clientId = u.searchParams.get("client_id") || clientId; |
| 58 | + if (!clientId) return; |
45 | 59 |
|
46 | | - let x = UsefulScriptGlobalPageContext.Utils.escapeRegExp( |
47 | | - "forEach(function(e){n(e)})}catch(e){}})}," |
48 | | - ); |
49 | | - x = new RegExp(x + "(.*)\\);</script>").exec(text); |
| 60 | + const res = await fetch( |
| 61 | + `https://api-v2.soundcloud.com/resolve?url=${encodeURIComponent( |
| 62 | + location.href |
| 63 | + )}&client_id=${clientId}` |
| 64 | + ); |
| 65 | + const json = await res.json(); |
50 | 66 |
|
51 | | - // info = json.loads(x.group(1))[-1]['data'][0] |
| 67 | + if (json && json.id) { |
| 68 | + // save media |
| 69 | + if (json.media) { |
| 70 | + window.ufs_soundcloud_allData.set(json.id, json); |
| 71 | + } |
| 72 | + // save media from tracks |
| 73 | + json.tracks |
| 74 | + ?.filter((track) => track.media) |
| 75 | + ?.forEach((track) => { |
| 76 | + window.ufs_soundcloud_allData.set(track.id, track); |
| 77 | + }); |
52 | 78 |
|
53 | | - // info = info['tracks'] if info.get('track_count') else [info] |
| 79 | + // add download button |
| 80 | + let songTitle = document |
| 81 | + .querySelector(".soundTitle__title") |
| 82 | + ?.textContent?.trim(); |
54 | 83 |
|
55 | | - // ids = [i['id'] for i in info if i.get('comment_count') is None] |
56 | | - // ids = list(map(str, ids)) |
57 | | - // ids_split = ['%2C'.join(ids[i:i+10]) for i in range(0, len(ids), 10)] |
58 | | - // api_url = 'https://api-v2.soundcloud.com/tracks?ids={ids}&client_id={client_id}&%5Bobject%20Object%5D=&app_version=1584348206&app_locale=en' |
| 84 | + let songInCache = Array.from( |
| 85 | + window.ufs_soundcloud_allData.values() |
| 86 | + ).find((_) => _.title === songTitle); |
59 | 87 |
|
60 | | - // res = [] |
61 | | - // for ids in ids_split: |
62 | | - // uri = api_url.format(ids=ids, client_id=client_id) |
63 | | - // cont = get_content(uri, decoded=True) |
64 | | - // res += json.loads(cont) |
| 88 | + const progressive = songInCache?.media?.transcodings?.find?.( |
| 89 | + (t) => t?.format?.protocol === "progressive" |
| 90 | + ); |
| 91 | + if (progressive) { |
| 92 | + downloadBtn.textContent = "Download"; |
| 93 | + downloadBtn.onclick = async (e) => { |
| 94 | + const res = await fetch(progressive.url + `?client_id=${clientId}`); |
| 95 | + const { url } = await res.json(); |
| 96 | + window.open(url); |
| 97 | + }; |
| 98 | + } else { |
| 99 | + downloadBtn.textContent = "Unsupported"; |
| 100 | + downloadBtn.onclick = () => { |
| 101 | + alert("Sorry, downloading this music is currently unsupported."); |
| 102 | + }; |
| 103 | + } |
| 104 | + } |
| 105 | + }); |
65 | 106 |
|
66 | | - // res = iter(res) |
67 | | - // info = [next(res) if i.get('comment_count') is None else i for i in info] |
| 107 | + // for (const f of ["pushState", "replaceState", "forward", "back", "go"]) { |
| 108 | + // hook(history, f, () => { |
68 | 109 |
|
69 | | - // return info |
| 110 | + // }); |
| 111 | + // } |
70 | 112 | }, |
71 | 113 | }; |
0 commit comments