-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
248 lines (218 loc) · 10.1 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//content.js
if (!window.XPostAssistant) {
window.XPostAssistant = {
// First update the SELECTORS object
SELECTORS: {
composeButton: '[data-testid="SideNav_NewTweet_Button"]',
tweetTextarea: '[data-testid="tweetTextarea_0"]',
tweetTextInput: '[role="textbox"][data-testid="tweetTextarea_0"]',
postButton: '[data-testid="tweetButton"]',
originalPost: 'article[data-testid="tweet"]',
originalPostText: '[data-testid="tweetText"]',
replyButton: '[data-testid="reply"]',
timeElement: 'time',
tweetLink: 'a[href*="/status/"]'
},
debugLog: function(message) {
console.log(`[X Post Assistant Content] ${message}`);
},
sleep: function(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
// Add the new getCurrentPostId function here
getCurrentPostId: function() {
try {
// First try to get from URL
const url = window.location.href;
const urlMatch = url.match(/status\/(\d+)/);
if (urlMatch) return urlMatch[1];
// If not in URL, try to get from the tweet element
const tweetArticle = document.querySelector(this.SELECTORS.originalPost);
if (tweetArticle) {
const timeElement = tweetArticle.querySelector('time');
if (timeElement) {
const link = timeElement.closest('a');
if (link) {
const href = link.getAttribute('href');
const statusMatch = href.match(/status\/(\d+)/);
if (statusMatch) return statusMatch[1];
}
}
}
return null;
} catch (error) {
this.debugLog('Error getting post ID: ' + error.message);
return null;
}
},
// Get the original post content
getOriginalPostContent: function() {
try {
const tweetArticle = document.querySelector(this.SELECTORS.originalPost);
if (!tweetArticle) {
throw new Error('Could not find original post');
}
const tweetText = tweetArticle.querySelector(this.SELECTORS.originalPostText);
if (!tweetText) {
throw new Error('Could not find tweet text');
}
// Enhanced image detection
let imageData = null;
const imageContainer = tweetArticle.querySelector('div[data-testid="tweetPhoto"]');
if (imageContainer) {
const img = imageContainer.querySelector('img');
if (img) {
imageData = {
url: img.src,
alt: img.alt || ''
};
this.debugLog('Found image URL: ' + imageData.url);
}
}
// Get author info
let authorName = '';
const authorElement = tweetArticle.querySelector('a[role="link"] div[dir="ltr"] span');
if (authorElement) {
authorName = authorElement.textContent;
}
return {
text: tweetText.textContent,
author: authorName,
id: this.getCurrentPostId(),
hasImage: !!imageData,
imageData: imageData
};
} catch (error) {
this.debugLog('Error getting original post: ' + error.message);
return null;
}
},
waitForElement: function(selector, timeout = 5000) {
this.debugLog(`Waiting for element: ${selector}`);
return new Promise((resolve, reject) => {
// Check if element already exists
const element = document.querySelector(selector);
if (element) {
this.debugLog(`Element ${selector} found immediately`);
resolve(element);
return;
}
const observer = new MutationObserver((mutations, obs) => {
const element = document.querySelector(selector);
if (element) {
obs.disconnect();
this.debugLog(`Element ${selector} found after waiting`);
resolve(element);
}
});
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true
});
setTimeout(() => {
observer.disconnect();
this.debugLog(`Timeout waiting for element: ${selector}`);
reject(new Error(`Element ${selector} not found within ${timeout}ms`));
}, timeout);
});
},
simulateTyping: function(element, text) {
return new Promise(async (resolve, reject) => {
try {
this.debugLog('Starting typing simulation');
// Focus the element
element.focus();
await this.sleep(100);
// Clear existing content
element.innerHTML = '';
// Create and dispatch beforeinput event
const beforeInputEvent = new InputEvent('beforeinput', {
bubbles: true,
cancelable: true,
inputType: 'insertText',
data: text
});
element.dispatchEvent(beforeInputEvent);
// Set the content
element.innerHTML = text;
// Create and dispatch input event
const inputEvent = new InputEvent('input', {
bubbles: true,
cancelable: true,
inputType: 'insertText',
data: text
});
element.dispatchEvent(inputEvent);
// Dispatch composition events
element.dispatchEvent(new Event('compositionstart', { bubbles: true }));
element.dispatchEvent(new Event('compositionend', { bubbles: true }));
// Additional events that X might be listening for
element.dispatchEvent(new Event('change', { bubbles: true }));
element.dispatchEvent(new Event('blur', { bubbles: true }));
element.dispatchEvent(new Event('focus', { bubbles: true }));
await this.sleep(100);
this.debugLog('Typing simulation complete');
resolve();
} catch (error) {
this.debugLog('Error in typing simulation: ' + error.message);
reject(error);
}
});
},
handlePost: function(content, replyToId) {
return new Promise(async (resolve, reject) => {
try {
this.debugLog('Starting post process');
if (replyToId) {
// For replies, find and click the reply button if not already clicked
const replyButton = document.querySelector(this.SELECTORS.replyButton);
if (replyButton) {
replyButton.click();
await this.sleep(500);
}
} else {
// For new posts, click compose button
const composeButton = await this.waitForElement(this.SELECTORS.composeButton);
composeButton.click();
await this.sleep(500);
}
// Find the text input
const textInput = await this.waitForElement(this.SELECTORS.tweetTextInput);
if (!textInput) {
throw new Error('Could not find tweet input area');
}
// Input the content
await this.simulateTyping(textInput, content);
this.debugLog('Content inserted successfully');
// Auto post if enabled
await this.sleep(500);
const postButton = await this.waitForElement(this.SELECTORS.postButton);
if (postButton && !postButton.disabled) {
postButton.click();
}
resolve({ success: true });
} catch (error) {
this.debugLog('Error in handlePost: ' + error.message);
resolve({ success: false, error: error.message });
}
});
}
};
// Set up message listener
browser.runtime.onMessage.addListener((request, sender) => {
const assistant = window.XPostAssistant;
assistant.debugLog('Received message: ' + request.type);
switch (request.type) {
case 'postToX':
return assistant.handlePost(request.content, request.replyToId);
case 'getCurrentPostId':
return Promise.resolve({
postId: assistant.getCurrentPostId(),
originalPost: assistant.getOriginalPostContent()
});
default:
return Promise.resolve({ success: false, error: 'Unknown message type' });
}
});
}