Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
sahaib authored Oct 19, 2024
1 parent eab6da5 commit edc0323
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 32 deletions.
9 changes: 8 additions & 1 deletion config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const CONFIG = {
SERVER_URL: 'https://summariser-test-f7ab30f38c51.herokuapp.com/summarize'
SERVER_URL: 'https://summariser-test-f7ab30f38c51.herokuapp.com',
ENDPOINTS: {
SUMMARIZE: '/summarize',
USER_ACTIVITY: '/user_activity',
TTS: '/tts',
GET_TTS_ENDPOINT: '/get_tts_endpoint',
GET_FREE_SUMMARIES_COUNT: '/get_free_summaries_count'
}
};

10 changes: 3 additions & 7 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
{
"manifest_version": 3,
"name": "Polirizer",
"version": "2.1.4",
"version": "2.5.1",
"description": "Summarize privacy policies using AI",
"permissions": [
"storage"
],
"content_security_policy": {
"extension_pages": "script-src 'self'; object-src 'self'"
},
"host_permissions": [
"https://summariser-test-f7ab30f38c51.herokuapp.com/",
"https://*/*",
"<all_urls>",
"https://api.openai.com/"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["purify.min.js", "popup.js"]
"js": ["purify.min.js"]
}
],
"background": {
Expand All @@ -42,7 +38,7 @@
},
"web_accessible_resources": [
{
"resources": ["node_modules/ldrs/dist/*", "loader-setup.js"],
"resources": ["config.js"],
"matches": ["<all_urls>"]
}
]
Expand Down
7 changes: 7 additions & 0 deletions popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
#userInfo {
font-size: 12px;
color: #666;
margin-bottom: 10px;
text-align: right;
}
.modal-content {
background-color: #fefefe;
margin: 35px auto;
Expand Down Expand Up @@ -607,6 +613,7 @@ <h1>Polirizer</h1> <!-- Change the app name -->
</div>
<!-- Add this line right after the header -->
<div id="freeSummariesCounter"></div>
<div id="userInfo"></div>
<textarea id="inputText" placeholder="Enter URL or paste privacy policy text"></textarea>
<select id="modelSelect">
<!-- Options will be dynamically populated by JavaScript -->
Expand Down
166 changes: 142 additions & 24 deletions popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,25 @@ document.addEventListener('DOMContentLoaded', function() {
async function getUserId() {
return new Promise((resolve) => {
chrome.storage.local.get(['userId'], function(result) {
let userId = result.userId;
if (!userId) {
userId = 'user_' + Math.random().toString(36).substr(2, 9);
chrome.storage.local.set({userId: userId});
if (result.userId) {
resolve(result.userId);
} else {
const newUserId = generateUUID();
chrome.storage.local.set({userId: newUserId}, function() {
resolve(newUserId);
});
}
resolve(userId);
});
});
}

function generateUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}

function beautifySummary(summary) {
// Split the summary into paragraphs
const paragraphs = summary.split('\n').filter(p => p.trim() !== '');
Expand Down Expand Up @@ -508,22 +517,31 @@ document.addEventListener('DOMContentLoaded', function() {
// Add this function to fetch the free summaries count
async function fetchFreeSummariesCount() {
try {
const userId = await getUserId();
console.log('Fetching free summaries count for user:', userId);
const response = await fetch('https://summariser-test-f7ab30f38c51.herokuapp.com/get_free_summaries_count', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: await getUserId()
user_id: userId
})
});
const data = await response.json();
console.log('Received data from server:', data);
if (data.free_summaries_left !== undefined) {
updateFreeSummariesDisplay(data.free_summaries_left);
if (data.new_user) {
showToast("Welcome! You've been granted 10 free summaries.");
console.log('New user detected, granted 10 free summaries');
}
} else {
console.log('Free summaries count not found in response');
updateFreeSummariesDisplay(0);
}
} catch (error) {
console.error('Error fetching free summaries count:', error);
updateFreeSummariesDisplay(0);
}
}
Expand Down Expand Up @@ -727,24 +745,6 @@ document.addEventListener('DOMContentLoaded', function() {
return standardizedResponse;
}

// Update the ttsVoiceSelect options to match GPT TTS voices
// const ttsVoiceOptions = [
// { value: 'alloy', text: 'Alloy' },
// { value: 'echo', text: 'Echo' },
// { value: 'fable', text: 'Fable' },
// { value: 'onyx', text: 'Onyx' },
// { value: 'nova', text: 'Nova' },
// { value: 'shimmer', text: 'Shimmer' }
// ];

// // Populate the ttsVoiceSelect dropdown
// ttsVoiceOptions.forEach(option => {
// const optionElement = document.createElement('option');
// optionElement.value = option.value;
// optionElement.textContent = option.text;
// ttsVoiceSelect.appendChild(optionElement);
// });

// Update the preloadAudio function to handle GPT TTS response
function preloadAudio(text, voice, callback) {
isPreloading = true;
Expand Down Expand Up @@ -829,4 +829,122 @@ document.addEventListener('DOMContentLoaded', function() {

// Call this function when the popup loads
fetchTtsEndpointUrl();

// Modify the summarize function to include the new user activity data
async function summarize() {
const inputText = document.getElementById('inputText').value;
const model = document.getElementById('modelSelect').value;
const userId = await getUserId();
const startTime = Date.now();

if (!inputText) {
showError('Please enter a URL or paste the privacy policy text.');
return;
}

showLoader();

try {
const apiKey = await getApiKey(model);
if (!apiKey) {
throw new Error('API key not found for the selected model.');
}

const searchedFor = inputText.startsWith('http://') || inputText.startsWith('https://') ? 'link' : 'plain_text';
let scrapedData = null;

if (searchedFor === 'link') {
scrapedData = await scrapeWebsite(inputText);
}

const response = await fetch('https://summariser-test-f7ab30f38c51.herokuapp.com/summarize', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
input: inputText,
model: model,
api_key: apiKey,
user_id: userId,
searched_for: searchedFor,
scrape_data: scrapedData
})
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
const requestTime = (Date.now() - startTime) / 1000; // Convert to seconds

// Send user activity data to the server
await sendUserActivity(userId, model, searchedFor, inputText, scrapedData, requestTime);

displayResult(data);
updateFreeSummariesCount(data.free_summaries_left);
} catch (error) {
showError(`An error occurred: ${error.message}`);
} finally {
hideLoader();
}
}

async function sendUserActivity(userId, modelSelected, searchedFor, searchedData, scrapeData, requestTime) {
try {
const response = await fetch('https://summariser-test-f7ab30f38c51.herokuapp.com/user_activity', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: userId,
model_selected: modelSelected,
searched_for: searchedFor,
searched_data: searchedData,
scrape_data: scrapeData,
request_time: requestTime
})
});

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const result = await response.json();
console.log('User activity recorded successfully:', result);
} catch (error) {
console.error('Error sending user activity:', error);
}
}

// Make sure to call the summarize function when the summarize button is clicked
document.getElementById('summarizeBtn').addEventListener('click', summarize);

async function displayUserInfo() {
try {
const userId = await getUserId();
const response = await fetch('https://summariser-test-f7ab30f38c51.herokuapp.com/get_user_info', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ user_id: userId })
});
const data = await response.json();
if (data.created_at) {
const createdDate = new Date(data.created_at).toLocaleDateString();
const userInfoElement = document.getElementById('userInfo');
if (userInfoElement) {
userInfoElement.textContent = `Account created on: ${createdDate}`;
}
}
} catch (error) {
console.error('Error fetching user info:', error);
}
}

// Call this function when the popup opens
displayUserInfo();
});

0 comments on commit edc0323

Please sign in to comment.