Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
samrawal committed Apr 24, 2024
0 parents commit 8e7e3ca
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
Empty file added README.md
Empty file.
61 changes: 61 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === "fetchText") {
// Use chrome.tabs.query to get the active tab in the current window
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
// tabs[0] will be the active tab in the current window
const currentTab = tabs[0];
chrome.scripting.executeScript({
target: { tabId: currentTab.id },
function: getTextFromPage
}, (injectionResults) => {
for (const frameResult of injectionResults)
sendTextToApi(frameResult.result, sendResponse);
});
});
return true; // Will respond asynchronously.
}
});

function getTextFromPage() {
return document.body.innerText;
}

function sendTextToApi(text, sendResponse) {
fetch('http:https://localhost:11434/api/generate', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(
{
model: "phi3",
prompt: text,
system: "Given text from a webpage, generate a brief summary. Answer in only plaintext format.",
stream: false,
}
)
})
.then(response => {
// Log the text response for debugging purposes
response.text().then(text => {
console.log("Received text:", text);
try {
// Attempt to parse it as JSON
const data = JSON.parse(text);
if (data && data.response) {
sendResponse(data.response.replace(/\n/g, "\n")); // Send only the "response" key with newline replaced by <br>
} else {
sendResponse("No response found."); // Fallback text
}
} catch (e) {
// Log parsing errors
// console.error("Error parsing JSON:", e);
console.error("Error parsing JSON:", text);
sendResponse("Error in parsing response.");
}
});
})
.catch(error => {
console.error('Error:', error);
sendResponse("Error in fetching response."); // Error handling
});
return true; // To allow asynchronous response
}
1 change: 1 addition & 0 deletions content.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// This file intentionally left blank as content injection is handled by background.js
Binary file added icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icon512.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"manifest_version": 3,
"name": "localsumm",
"version": "0.1",
"permissions": ["activeTab", "scripting"],
"action": {
"default_popup": "popup.html",
"default_icon": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
}
},
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_idle"
}
],
"icons": {
"16": "icon16.png",
"48": "icon48.png",
"128": "icon128.png"
}
}
46 changes: 46 additions & 0 deletions popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<!DOCTYPE html>
<html>
<head>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:ital,wght@0,100..800;1,100..800&display=swap" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css">
<title>API Response</title>
<style>
h1 {
font-family: "JetBrains Mono", monospace;
font-size: 3.4em;
}
body {
width: 600px;
height: 400px;
margin: 10px;
}
#output {
overflow-y: auto; /* Adds a scrollbar if text is too long */
font-size: 1.4em;
}
.blink_me {
animation: blinker 1.5s linear infinite;
}

@keyframes blinker {
50% {
opacity: 0;
}
}
</style>
</head>
<body>
<h1><span style="color: darkorange">local</span><span style="color: teal">summ</span></h1>
<hr>
<div id="output">
<center>
<h2 class="blink_me">Summarizing...</h2>
<h3 class="blink_me">Model: Phi3</h3>
<img src="icon512.png" style="width:240px" class="no_blink_me"/>
</center>
</div>
<script src="popup.js"></script>
</body>
</html>
5 changes: 5 additions & 0 deletions popup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
document.addEventListener('DOMContentLoaded', function() {
chrome.runtime.sendMessage({ action: "fetchText" }, function(response) {
document.getElementById('output').textContent = response;
});
});

0 comments on commit 8e7e3ca

Please sign in to comment.