Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: ふぁ <[email protected]>
  • Loading branch information
fa0311 committed May 15, 2024
1 parent 4c569f6 commit 9520552
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions js/TwitterSpacesWiretapExtension.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,38 @@ class TwitterSpacesWiretap {
}

getHeaders() {
let cookie = this.getCookieArray();
let headers = {
const cookie = this.getCookieArray();
const headers = {
authorization: this.AUTHORIZATION,
"Content-type": "application/json",
"x-csrf-token": cookie["ct0"],
"x-twitter-active-user": "yes",
"x-twitter-client-language": "ja",
"x-twitter-client-language": "en",
};
if (cookie["gt"] != undefined) {
headers = { ...headers, ...{ "x-guest-token": cookie["gt"] } };
if (cookie["gt"] == undefined) {
return headers;
} else {
return { ...headers, ...{ "x-guest-token": cookie["gt"] } };
}
return headers;
}

getCookieArray() {
let arr = new Array();
const arr = new Array();
if (document.cookie != "") {
let tmp = document.cookie.split("; ");
for (let i = 0; i < tmp.length; i++) {
let data = tmp[i].split("=");
const tmp = document.cookie.split("; ");
Array.from({ length: tmp.length }, (_, i) => {
const data = tmp[i].split("=");
arr[data[0]] = decodeURIComponent(data[1]);
}
});
}
return arr;
}

async AudioSpaceById(id) {
let url = new URL(
const url = new URL(
"https://twitter.com/i/api/graphql/Uv5R_-Chxbn1FEkyUkSW2w/AudioSpaceById"
);
let params = new URLSearchParams({
const params = new URLSearchParams({
variables: JSON.stringify({
id: id,
isMetatagsQuery: true,
Expand All @@ -50,24 +51,24 @@ class TwitterSpacesWiretap {
}),
});

let response = await fetch(url.toString() + "?" + params.toString(), {
const response = await fetch(url.toString() + "?" + params.toString(), {
headers: this.getHeaders(),
method: "GET",
});
return await response.json();
}

async live_video_stream(media_key) {
let url = new URL(
const url = new URL(
"https://twitter.com/i/api/1.1/live_video_stream/status/" + media_key
);
let params = new URLSearchParams({
const params = new URLSearchParams({
client: "web",
use_syndication_guest_id: false,
cookie_set_host: "twitter.com",
});

let response = await fetch(url.toString() + "?" + params.toString(), {
const response = await fetch(url.toString() + "?" + params.toString(), {
headers: this.getHeaders(),
method: "GET",
});
Expand All @@ -76,42 +77,34 @@ class TwitterSpacesWiretap {
}

new MutationObserver(async () => {
href = location.href;
const href = location.href;
if (href.match(new RegExp("https://twitter.com/i/spaces/")) == null) return;
if (document.getElementById("twitter-spaces-wiretap-audio") != null) return;

const audio = document.createElement("audio");
audio.setAttribute("controls", "");
audio.id = "twitter-spaces-wiretap-audio";

let element;
let hrefList = href.split("?")[0].split("/");
let id = hrefList.slice(-1)[0];
if (id == "peek") {
id = hrefList.slice(-2)[0];
element = document.querySelector(
'div[role="menu"] div[aria-haspopup="menu"]'
const hrefList = href.split("?")[0].split("/");
const [peek, id] = hrefList.reverse();
if (peek == "peek") {
const element = document.querySelector(
'div[data-testid="sheetDialog"] > div > div > div > button'
);
if (element == null) return;
element.parentNode.parentNode.prepend(audio);
} else {
element = document.querySelector(
'div[data-testid="placementTracking"] div[role="button"] > div > div'
);
if (element == null) return;
element.append(audio);
element.parentNode.parentNode.insertBefore(audio, element.parentNode.nextElementSibling);
}

console.log(id);

let space = new TwitterSpacesWiretap();
let media_key = (await space.AudioSpaceById(id)).data.audioSpace.metadata
const space = new TwitterSpacesWiretap();
const media_key = (await space.AudioSpaceById(id)).data.audioSpace.metadata
.media_key;
let url = (await space.live_video_stream(media_key)).source.location;
const url = (await space.live_video_stream(media_key)).source.location;
console.log(url);

if (Hls.isSupported()) {
let hls = new Hls();
const hls = new Hls();
hls.loadSource(url);
hls.attachMedia(audio);
audio.play();
Expand Down

0 comments on commit 9520552

Please sign in to comment.