-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
155 lines (133 loc) · 5.55 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
let isScrolling = false;
let collectedTweets = [];
let tweetLenghth = 100;
let tweetPostUrl = '';
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
chrome.storage.local.get("numberOfTweets", (result) => {
tweetLenghth = result.numberOfTweets || 200;
});
if (request.action === "startScrolling") {
isScrolling = true;
startTweetCollection();
sendResponse({ status: "Scrolling started" });
} else if (request.action === "stopScrolling") {
isScrolling = false;
chrome.storage.local.get("tweetPostUrl", (result) => {
tweetPostUrl = result.tweetPostUrl
postTweetsToOllama(
collectedTweets,
result.tweetPostUrl || "http://localhost:1234/v1/chat/completions"
);
sendResponse({ status: "Scrolling stopped" });
});
}
});
// Function to start the tweet collection process
function startTweetCollection() {
chrome.storage.local.get("scrollAmount", (result) => {
let scrollAmount = result.scrollAmount || 100; // Default to 5000 if not set
loop(scrollAmount);
});
}
let i = 1;
let dict = {};
let like_regex = /\b(\d+) Like|Likes\b/;
let retweet_regex = /\b(\d+) Retweet|Retweets\b/;
let reply_regex = /\b(\d+) Repl(ies|y)\. Reply\b/;
let time_regex = /\btime datetime=\"(.*?)\">\b/;
let status_regex = /\b\/status\/(\d+)\b/;
function postTweetsToOllama(tweets, url) {
console.log("Posting tweets to Ollama:", tweets);
let systemPrompt = `
You are a highly knowledgeable AI assistant specializing in literature analysis and cognitive diversity. Your task is to analyze a set of tweets and recommend books that will challenge and expand the author's worldview.
Your primary goals are to:
1. Identify the author's current perspectives, biases, and areas of interest from their tweets.
2. Recommend books that present contrasting viewpoints or introduce entirely new concepts.
3. Ensure recommendations come from diverse cultural, philosophical, and ideological backgrounds.
4. Explain how each recommended book could broaden the author's understanding of the world.
Remember:
- Focus on intellectual growth, not reinforcing existing beliefs.
- Prioritize books that offer unique or unconventional perspectives.
- Consider works from various time periods, cultures, and disciplines.
- Aim for a balance between accessibility and intellectual challenge.
Your recommendations should be thought-provoking and have the potential to significantly alter the author's worldview. Provide a brief, compelling explanation for each recommendation.
The responses should all be in properly formated markdown.
`;
let userPrompt = `
Analyze the following list of tweets:
${tweets.join("\n")}
Based on these tweets, first recommend 3-5 books that will challenge the author's current worldview wildly and expose them to radically different perspectives. The recommended books should:
1. Present ideas that contrast with or contradict the views expressed in the tweets
2. Introduce novel concepts or ways of thinking not evident in the tweet content
3. Originate from diverse cultural, philosophical, or ideological backgrounds
4. Have the potential to significantly alter the author's understanding of the world
Then recommend 2-3 books that will expand the author's current perspectives on ideas he already holds and allow the author to dig deeper into his existing interests. The recommended books should:
1. Offer nuanced or alternative views on topics the author is already interested in
2. Provide in-depth analysis or exploration of themes present in the tweets
3. Introduce new dimensions or aspects of familiar ideas
4. Challenge the author to think more critically or broadly about his existing beliefs
For each book recommendation, briefly explain why it was chosen and how it could expand the author's perspective.
Add a conclusion that summarizes the key takeaways from the tweet analysis and broad statement on why the suggested books are valuable for the author.
The response should all be in properly formated markdown.
`;
collectedTweets = [];
// Show loading message to the user
console.log("Waiting for response from Ollama...");
fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
],
temperature: 0.7,
max_tokens: -1,
stream: false,
}),
})
.then((response) => response.json())
.then((data) => {
chrome.storage.local.set({
ollamaResponse: data.choices[0].message.content,
});
})
.catch((error) => {
console.error("Error:", error);
chrome.storage.local.set({ ollamaResponse: "Error: " + error.message });
});
}
const tweetLimit = 200;
function loop(scrollAmount) {
if (!isScrolling) return;
if (collectedTweets.length >= tweetLimit) {
postTweetsToOllama(
collectedTweets,
tweetPostUrl || "http://localhost:1234/v1/chat/completions"
);
return;
}
setTimeout(function () {
window.scrollBy(0, scrollAmount);
let elements = document.getElementsByTagName("span");
for (let index = 0; index < elements.length; index++) {
try {
let tweetText = elements[index].innerText
.toLowerCase()
.trim()
.replace(/\s+/g, " ");
if (!collectedTweets.includes(tweetText)) {
collectedTweets.push(tweetText);
console.log(tweetText);
}
} catch (error) {
console.log(error);
}
}
if (isScrolling) {
loop(scrollAmount);
}
}, 500);
}