Created
October 17, 2021 13:03
-
-
Save rigwild/68a74cd2e46bce2c4fd5e14cba10f532 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <reference path="./types.ts" /> | |
// @ts-check | |
'use strict' | |
/** @param {string}str */ | |
const strToUint8Array = str => new TextEncoder().encode(str) | |
/** @param {Uint8Array}uint8Array */ | |
const uint8ArrayToStr = uint8Array => new TextDecoder('utf-8').decode(uint8Array) | |
const createPostUri = 'https://www.linkedin.com/voyager/api/contentcreation/normShares' | |
/** @type {Map<string, EventRequestObjWithBodyCreatePost>} */ | |
const requestJar = new Map() | |
/** @param {EventRequestObjWithBodyCreatePost['requestBodyJson']} requestBodyJson */ | |
const transformRequestBodyJson = requestBodyJson => { | |
requestBodyJson.commentaryV2.text = 'Hola senior' | |
} | |
chrome.webRequest.onBeforeRequest.addListener( | |
/** @param {EventRequestObjWithBodyCreatePost} details */ | |
details => { | |
if (details.method !== 'POST') return | |
console.log(`[${details.requestId}] request: `, details) | |
let jsonBody | |
try { | |
const rawTextBody = details.requestBody.raw | |
.map(data => String.fromCharCode.apply(null, new Uint8Array(data.bytes))) | |
.join('') | |
jsonBody = JSON.parse(rawTextBody) | |
} catch (err) { | |
throw new Error(`Failed to parse JSON request body - ${err.message}`) | |
} | |
details.requestBodyJson = jsonBody | |
console.log(`[${details.requestId}] jsonBody: `, jsonBody) | |
requestJar.set(details.requestId, details) | |
}, | |
{ | |
urls: [createPostUri] | |
}, | |
['requestBody', 'blocking'] | |
) | |
chrome.webRequest.onBeforeSendHeaders.addListener( | |
/** @param {EventRequestObjWithHeaders} details */ | |
details => { | |
if (details.method !== 'POST') return | |
console.log(`[${details.requestId}] details: `, details) | |
// Convert `{ name: 'name', value: 'value' }` to `{ [name: string]: string }` | |
const headers = /** @type {Record<string, string>} */ ( | |
details.requestHeaders.reduce((acc, cur) => { | |
acc[cur.name] = cur.value | |
return acc | |
}, {}) | |
) | |
console.log(`[${details.requestId}] headers: `, headers) | |
const transformedRequestBodyJson = transformRequestBodyJson(requestJar.get(details.requestId).requestBodyJson) | |
console.log(`[${details.requestId}] transformedRequestBodyJson: `, transformedRequestBodyJson) | |
;(async () => { | |
const res = await fetch(createPostUri, { | |
// credentials: 'include', | |
method: 'POST', | |
body: strToUint8Array(JSON.stringify(transformedRequestBodyJson)), | |
headers | |
}).then(res => res.text()) | |
console.log('res', res) | |
})() | |
return { cancel: true } | |
}, | |
{ urls: [createPostUri] }, | |
['requestHeaders', 'extraHeaders', 'blocking'] | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
declare var chrome: any | |
type EventRequestObj = { | |
frameId: number | |
initiator: string | |
method: string | |
parentFrameId: number | |
requestId: string | |
tabId: number | |
timeStamp: number | |
type: string | |
url: string | |
} | |
type EventRequestObjWithBody = EventRequestObj & { | |
requestBody: { raw: [{ bytes: Uint8Array }] } | |
requestBodyJson: unknown | |
} | |
type EventRequestObjWithHeaders = EventRequestObj & { requestHeaders: { name: string; value: string }[] } | |
type CreatePostBody = { | |
visibleToConnectionsOnly: boolean | |
externalAudienceProviders: [] | |
commentaryV2: { text: string; attributes: [] } | |
origin: string | |
allowedCommentersScope: string | |
media: [] | |
} | |
type EventRequestObjWithBodyCreatePost = EventRequestObjWithBody & { requestBodyJson: CreatePostBody } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment