twitter-api-browser-js 0.0.11 → 0.0.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/inject.js CHANGED
@@ -1,64 +1,64 @@
1
1
  import { INITIAL_STATE_GLOBAL_KEY, INITIAL_STATE_PROP_KEY, OPERATIONS_GLOBAL_KEY, REQUEST_FUNC_GLOBAL_KEY, } from "./consts.js";
2
- export const SETUP_SCRIPT = `
3
- (async () => {
4
- if (globalThis.${REQUEST_FUNC_GLOBAL_KEY}) {
5
- return;
6
- }
7
- const __origApply = Function.prototype.apply;
8
- const client = await new Promise((resolve) => {
9
- Function.prototype.apply = function (thisArg, argsArray) {
10
- if (thisArg && typeof thisArg === "object" && thisArg.dispatch === this) {
11
- resolve(thisArg);
12
- }
13
- return __origApply.bind(this)(thisArg, argsArray);
14
- };
15
- });
16
- Function.prototype.apply = __origApply;
17
- globalThis.${REQUEST_FUNC_GLOBAL_KEY} = (query) => {
18
- return client.dispatch.apply(client, [query]);
19
- };
20
- })();
2
+ export const SETUP_SCRIPT = `
3
+ (async () => {
4
+ if (globalThis.${REQUEST_FUNC_GLOBAL_KEY}) {
5
+ return;
6
+ }
7
+ const __origApply = Function.prototype.apply;
8
+ const client = await new Promise((resolve) => {
9
+ Function.prototype.apply = function (thisArg, argsArray) {
10
+ if (thisArg && typeof thisArg === "object" && thisArg.dispatch === this) {
11
+ resolve(thisArg);
12
+ }
13
+ return __origApply.bind(this)(thisArg, argsArray);
14
+ };
15
+ });
16
+ Function.prototype.apply = __origApply;
17
+ globalThis.${REQUEST_FUNC_GLOBAL_KEY} = (query) => {
18
+ return client.dispatch.apply(client, [query]);
19
+ };
20
+ })();
21
21
  `;
22
- export const INITIAL_STATE_SCRIPT = `
23
- (async () => {
24
- const init_state_promise = new Promise((resolve) => {
25
- Object.defineProperty(window, "${INITIAL_STATE_PROP_KEY}", {
26
- configurable: true,
27
- enumerable: true,
28
- get() {
29
- return undefined;
30
- },
31
- set(v) {
32
- resolve(v);
33
- Object.defineProperty(window, "${INITIAL_STATE_PROP_KEY}", {
34
- value: v,
35
- writable: true,
36
- enumerable: true,
37
- configurable: true,
38
- });
39
- },
40
- });
41
- });
42
-
43
- globalThis.${INITIAL_STATE_GLOBAL_KEY} = init_state_promise;
44
- })();
22
+ export const INITIAL_STATE_SCRIPT = `
23
+ (async () => {
24
+ const init_state_promise = new Promise((resolve) => {
25
+ Object.defineProperty(window, "${INITIAL_STATE_PROP_KEY}", {
26
+ configurable: true,
27
+ enumerable: true,
28
+ get() {
29
+ return undefined;
30
+ },
31
+ set(v) {
32
+ resolve(v);
33
+ Object.defineProperty(window, "${INITIAL_STATE_PROP_KEY}", {
34
+ value: v,
35
+ writable: true,
36
+ enumerable: true,
37
+ configurable: true,
38
+ });
39
+ },
40
+ });
41
+ });
42
+
43
+ globalThis.${INITIAL_STATE_GLOBAL_KEY} = init_state_promise;
44
+ })();
45
45
  `;
46
- export const OPERATIONS_SCRIPT = `
47
- (async () => {
48
- globalThis.${OPERATIONS_GLOBAL_KEY} = [];
49
- const origCall = Function.prototype.call;
50
- Function.prototype.call = function (thisArg, ...args) {
51
- const module = args[0];
52
- const ret = origCall.bind(this)(thisArg, ...args);
53
- try {
54
- const exp = module.exports;
55
- if (exp.operationName) {
56
- globalThis.${OPERATIONS_GLOBAL_KEY}.push(exp);
57
- }
58
- } catch (_) {}
59
- return ret;
60
- };
61
- await new Promise((resolve) => setTimeout(resolve, 5000));
62
- Function.prototype.call = origCall;
63
- })();
46
+ export const OPERATIONS_SCRIPT = `
47
+ (async () => {
48
+ globalThis.${OPERATIONS_GLOBAL_KEY} = [];
49
+ const origCall = Function.prototype.call;
50
+ Function.prototype.call = function (thisArg, ...args) {
51
+ const module = args[0];
52
+ const ret = origCall.bind(this)(thisArg, ...args);
53
+ try {
54
+ const exp = module.exports;
55
+ if (exp.operationName) {
56
+ globalThis.${OPERATIONS_GLOBAL_KEY}.push(exp);
57
+ }
58
+ } catch (_) {}
59
+ return ret;
60
+ };
61
+ await new Promise((resolve) => setTimeout(resolve, 5000));
62
+ Function.prototype.call = origCall;
63
+ })();
64
64
  `;
package/dist/main.d.ts CHANGED
@@ -18,7 +18,7 @@ export declare class TwitterAPIBrowser {
18
18
  * @param waitForReady - The number of seconds to wait for the browser to be ready.
19
19
  * @param headless - Whether to run the browser in headless mode. (Recommended: false)
20
20
  */
21
- setup(waitForReady?: number, headless?: boolean): Promise<void>;
21
+ setup(waitForReady?: number, headless?: boolean, proxy?: string | undefined): Promise<void>;
22
22
  /**
23
23
  * @description Closes the browser context and page.
24
24
  */
package/dist/main.js CHANGED
@@ -32,7 +32,7 @@ export class TwitterAPIBrowser {
32
32
  * @param waitForReady - The number of seconds to wait for the browser to be ready.
33
33
  * @param headless - Whether to run the browser in headless mode. (Recommended: false)
34
34
  */
35
- async setup(waitForReady = 5, headless = false) {
35
+ async setup(waitForReady = 5, headless = false, proxy = void 0) {
36
36
  if (this.browser && this.page) {
37
37
  await this.close();
38
38
  }
@@ -46,6 +46,7 @@ export class TwitterAPIBrowser {
46
46
  "--no-sandbox",
47
47
  "--disable-dev-shm-usage",
48
48
  "--disable-gpu",
49
+ proxy ? `--proxy-server=${proxy}` : "",
49
50
  ],
50
51
  })
51
52
  .then(resolve)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "twitter-api-browser-js",
3
- "version": "0.0.11",
3
+ "version": "0.0.12",
4
4
  "type": "module",
5
5
  "exports": {
6
6
  ".": {
package/dist/example.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/dist/example.js DELETED
@@ -1,109 +0,0 @@
1
- import { TwitterAPIBrowser } from "./main.ts";
2
- import { createInterface } from "node:readline";
3
- function prompt(question) {
4
- const rl = createInterface({
5
- input: process.stdin,
6
- output: process.stdout,
7
- });
8
- return new Promise((resolve) => {
9
- rl.question(question, (answer) => {
10
- rl.close();
11
- resolve(answer);
12
- });
13
- });
14
- }
15
- function formatLog(...args) {
16
- console.dir(args, { depth: null });
17
- }
18
- async function main() {
19
- const userDataDir = "./.data";
20
- const browser = new TwitterAPIBrowser(userDataDir);
21
- console.log("Setup browser");
22
- await browser.setup();
23
- try {
24
- if (await browser.isLoggedIn()) {
25
- console.log("User is logged in");
26
- }
27
- else {
28
- console.log("User is not logged in, please login manually");
29
- await browser.manualLogin();
30
- }
31
- while (true) {
32
- console.log("=".repeat(20));
33
- const operation = await prompt("Choose operation [CreateTweet, HomeTimeline, UserByScreenName, CreateRetweet, FavoriteTweet, SearchTimeline, UsersByRestIds, exit]: ");
34
- if (!operation || operation === "exit") {
35
- return;
36
- }
37
- const request = async () => {
38
- if (operation === "CreateTweet") {
39
- const res = await browser.request("CreateTweet", {
40
- tweet_text: `Hello, World! ${new Date().toISOString()}`,
41
- dark_request: false,
42
- media: { media_entities: [], possibly_sensitive: false },
43
- semantic_annotation_ids: [],
44
- disallowed_reply_options: null,
45
- });
46
- return res;
47
- }
48
- else if (operation === "HomeTimeline") {
49
- const res = await browser.request("HomeTimeline", {
50
- count: 20,
51
- includePromotedContent: true,
52
- latestControlAvailable: true,
53
- withCommunity: true,
54
- });
55
- return res;
56
- }
57
- else if (operation === "UserByScreenName") {
58
- const res = await browser.request("UserByScreenName", {
59
- screen_name: "elonmusk",
60
- withSafetyModeUserFields: true,
61
- withSuperFollowsUserFields: true,
62
- withBirdwatchPivots: false,
63
- }, {
64
- withAuxiliaryUserLabels: true,
65
- });
66
- return res;
67
- }
68
- else if (operation === "CreateRetweet") {
69
- const res = await browser.request("CreateRetweet", {
70
- tweet_id: "1987547856664993831",
71
- dark_request: false,
72
- });
73
- return res;
74
- }
75
- else if (operation === "FavoriteTweet") {
76
- const res = await browser.request("FavoriteTweet", {
77
- tweet_id: "1987547856664993831",
78
- });
79
- return res;
80
- }
81
- else if (operation === "SearchTimeline") {
82
- const res = await browser.request("SearchTimeline", {
83
- rawQuery: "from:elonmusk",
84
- count: 20,
85
- querySource: "typed_query",
86
- product: "Top",
87
- withGrokTranslatedBio: false,
88
- });
89
- return res;
90
- }
91
- else if (operation === "UsersByRestIds") {
92
- const res = await browser.request("UsersByRestIds", {
93
- userIds: ["900282258736545792"],
94
- });
95
- return res;
96
- }
97
- else {
98
- console.log(`Unknown operation: ${operation}`);
99
- }
100
- };
101
- const res = await request();
102
- formatLog(res);
103
- }
104
- }
105
- finally {
106
- await browser.close();
107
- }
108
- }
109
- await main();