Skip to content

Commit

Permalink
Progress on Issue OpenAdaptAI#51
Browse files Browse the repository at this point in the history
  • Loading branch information
Krish Patel committed May 19, 2023
1 parent 958efaa commit 10a55f6
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 53 deletions.
35 changes: 26 additions & 9 deletions native_chrome_extension/background.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
/**
the background.js file listens for messages from the content script, and
sends them to PuterBot via native messaging
sends them to OpenAdapt via native messaging
*/

chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {
if (sender.id !== chrome.runtime.id) {
return;
}
// Connect to the native messaging host
let port = chrome.runtime.connectNative("com.openadapt.domlistener");

// Receive messages from the native messaging host
port.onMessage.addListener(onReceived);

// Send initial message to the native messaging host
port.postMessage("hello");

// Send the message to PuterBot
chrome.runtime.sendNativeMessage("puterbot", message, function(response) {
console.log(response);
});
// Handle received messages
function onReceived(response) {
console.log(response);
}

// Listen for messages from the content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// Forward the message to the native messaging host
port.postMessage(message);
});

// Listen for DOM changes in the active tab
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === "complete") {
// Inject content script to the updated tab
chrome.tabs.executeScript(tabId, { file: "content.js" });
}
});
23 changes: 9 additions & 14 deletions native_chrome_extension/browser.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import json
""" Module for the browser native messaging host. """
import nativemessaging

def main():
messages = []

def main() -> None:
"""
Main function for the browser native messaging host.
"""
reply_num = 0
while True:
# Receive a message from the Chrome Extension
message = nativemessaging.get_message()
if not message:
break
print(message)
nativemessaging.send_message(nativemessaging.encode_message(str(reply_num)))
reply_num += 1

# Parse the message as JSON
message_data = json.loads(message)

# Store the message in a list
messages.append(message_data)

# Do something with the collected messages
print(messages)

if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions native_chrome_extension/com.openadapt.domlistener.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
python -u "P:\OpenAdapt AI - MLDS AI\cloned_repo\OpenAdapt\native_chrome_extension\browser.py"
9 changes: 9 additions & 0 deletions native_chrome_extension/com.openadapt.domlistener_chrome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "com.openadapt.domlistener",
"description": "OpenAdapt Native Messaging Host",
"path": "P:\\OpenAdapt AI - MLDS AI\\cloned_repo\\OpenAdapt\\native_chrome_extension\\com.openadapt.domlistener.bat",
"type": "stdio",
"allowed_origins": [
"chrome-extension://fipijfheidmdeingpecoiflbjefbnfjn/"
]
}
24 changes: 7 additions & 17 deletions native_chrome_extension/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,13 @@ The content.js file listens for changes to the DOM, and
sends a message to the background script when a change is detected.
*/

// Connect to the background script
const port = chrome.runtime.connect();

// Listen for changes to the DOM
// Ref.: https://stackoverflow.com/questions/8882502/how-to-track-dom-change-in-chrome-extension solution

const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => { // change occurs in DOM
try {
port.postMessage(mutation); // then, Send a message to the background script: background.js
} catch (e) {
console.error(e);
port.disconnect();
const newPort = chrome.runtime.connect();
newPort.onMessage.addListener(msg => console.log(msg));
// DOM mutation observer
let observer = new MutationObserver(mutations => {
for (let mutation of mutations) {
// Send the mutation to the background script
chrome.runtime.sendMessage(mutation);
}
});
});

observer.observe(document, { childList: true, subtree: true });
// Start observing DOM mutations
observer.observe(document, { childList: true, subtree: true });
18 changes: 9 additions & 9 deletions native_chrome_extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "PuterBot DOM Listener",
"name": "OpenAdapt DOM Listener",
"version": "1.0",
"manifest_version": 3,
"icons": {
Expand All @@ -13,11 +13,11 @@
"background": {
"service_worker": "background.js"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"all_frames": true,
"js": ["content.js"]
}
]
}
"action": {
"default_popup": "popup.html"
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["content.js"]
}]
}
8 changes: 4 additions & 4 deletions native_chrome_extension/native-manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "browser",
"description": "PuterBot DOM Listener",
"path": "./browser.py",
"name": "com.openadapt.domlistener",
"description": "OpenAdapt Native Messaging Host",
"path": "browser.py",
"type": "stdio",
"allowed_origins": [
"chrome-extension://aeedjbflenakjffcnhodfddbphbnpemf/"
"chrome-extension://fipijfheidmdeingpecoiflbjefbnfjn/"
]
}
15 changes: 15 additions & 0 deletions native_chrome_extension/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OpenAdapt DOM Listener</title>
</head>

<body>
<h1>PuterBot DOM Listener</h1>
<p>Extension is actively listening for DOM changes.</p>
</body>

</html>
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ sqlalchemy==1.4.43
torch==2.0.0
tqdm==4.64.0
transformers==4.28.1
nativemessaging==1.0.1

0 comments on commit 10a55f6

Please sign in to comment.