-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
127 lines (111 loc) · 3.85 KB
/
Copy pathcontent.js
File metadata and controls
127 lines (111 loc) · 3.85 KB
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
// content.js
// 1) Track the most recent context-menu coordinates
let lastCtx = { x: 0, y: 0 };
document.addEventListener('contextmenu', e => {
lastCtx = { x: e.clientX, y: e.clientY };
});
// 2) Keep a global ref to the post you just “designated”
let lastPostElement = null;
// 3) “Designate Post” handler
chrome.runtime.onMessage.addListener((msg, sender) => {
if (msg.action !== 'designatePost') return;
// 3a) Grab the element under the cursor
const el = document.elementFromPoint(lastCtx.x, lastCtx.y);
if (!el) return console.warn('No element at point');
console.log('clicked element:', el);
// 3b) Dump ancestors for selector-tweaking (optional)
let node = el;
while (node && node !== document.documentElement) {
const attrs = Array.from(node.attributes||[])
.map(a=>`${a.name}="${a.value}"`)
.join(' ');
console.log(`ancestor → <${node.tagName.toLowerCase()} ${attrs}>`);
node = node.parentElement;
}
// 3c) Find the FB post wrapper
const post = el.closest(
'div[role="article"], div[aria-posinset], div[data-pagelet^="FeedUnit_"]'
);
console.log('matched post wrapper:', post);
if (!post) {
console.warn('Not inside a FB post—adjust your selector!');
return;
}
// 3d) Extract text, highlight & stash
const content = post.innerText.trim();
// 3d-2) Grab any image alt-text inside this post
const imgAlts = Array.from(post.querySelectorAll('img'))
.map(img => img.alt && img.alt.trim())
.filter(Boolean);
post.style.outline = '3px solid orange';
lastPostElement = post;
chrome.storage.local.get({ posts: [] }, data => {
data.posts.push(content);
chrome.storage.local.set({ posts: data.posts }, () => {
console.log('Post saved:', content.slice(0,60) + '…');
});
});
// 3e) Kick off GPT processing
chrome.runtime.sendMessage({
action: 'processPost',
content
});
chrome.runtime.sendMessage({
action: 'processPost',
content,
imgAlts // <-- send array of alt-texts
});
});
// 4) “Post the Comment” handler
chrome.runtime.onMessage.addListener((msg, sender) => {
if (msg.action !== 'postComment' || !lastPostElement) return;
// 4a) Click the “Comment” trigger to open the editor
const trigger = Array.from(
lastPostElement.querySelectorAll('a, button, [role="button"]')
).find(el => /comment/i.test(el.innerText));
if (trigger) trigger.click();
// 4b) Poll for the editor until it appears (timeout after 2s)
const start = Date.now();
const tryInsert = () => {
if (Date.now() - start > 2000) {
console.warn('Comment editor never appeared');
return;
}
// a) First, check if activeElement is our editor
let editor = document.activeElement;
if (!(editor && editor.getAttribute('contenteditable') === 'true')) {
// b) Fallback to global query
editor = document.querySelector(
'div[contenteditable="true"][role="textbox"]'
);
}
if (!editor) {
return setTimeout(tryInsert, 100);
}
// 4c) Insert the comment text
editor.focus();
document.execCommand('insertText', false, msg.comment);
editor.dispatchEvent(new InputEvent('input', { bubbles: true }));
editor.dispatchEvent(new Event('change', { bubbles: true }));
// 4d) Instead of finding the Post button, simulate pressing Enter
// on the comment textbox (Facebook submits comment on Enter)
// wait 300ms, then “press” Enter:
setTimeout(() => {
editor.dispatchEvent(new KeyboardEvent('keydown', {
key: 'Enter',
code: 'Enter',
which: 13,
keyCode: 13,
bubbles: true
}));
editor.dispatchEvent(new KeyboardEvent('keyup', {
key: 'Enter',
code: 'Enter',
which: 13,
keyCode: 13,
bubbles: true
}));
}, 300);
};
tryInsert();
});