Skip to content

Commit

Permalink
revert: conversationManage.js injection
Browse files Browse the repository at this point in the history
  • Loading branch information
F33RNI committed Apr 2, 2024
1 parent 6d7ff7e commit 9c7cd7f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 83 deletions.
117 changes: 51 additions & 66 deletions src/lmao/ms_copilot/conversationManage.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@
* SOFTWARE.
*/

/**
* Call this from python script to check if this file is injected
* @returns true
*/
function isManageInjected() {
return true;
}
// driver.execute_async_script() callback
const callback = arguments[arguments.length - 1];

/**
* Tries to click on "See all recent chats" button without raising any error
Expand Down Expand Up @@ -93,70 +88,60 @@ function renameChatAndConfirm(conversationID) {
confirmBtn.click();
}

/**
* Asynchronously loads or deletes specific conversation or renames the last one into conversationID
* Pass "load", "delete" or "rename" as 1st argument and conversationID as 2nd
*/
function conversationManage() {
// driver.execute_async_script() callback
const callback = arguments[arguments.length - 1];
// Extract arguments (action, conversation ID)
// Action can be "load", "delete" or "rename"
const action = arguments[0];
const conversationID = arguments[1];

// "load", "delete" or "rename"
const action = arguments[0];
try {
// Open conversation (load it)
if (action === "load") {
// Expand all chats -> wait 500ms -> find conversation -> open it -> return the same conversation ID or "null"
expandChats();
setTimeout(function () {
const cibThreadContainer = searchCibThreadContainer(conversationID, true);
if (cibThreadContainer === null) {
callback("" + null);
}

// ID of conversation to load, delete or rename last into
const conversationID = arguments[1];

try {
// Open conversation (load it)
if (action === "load") {
// Expand all chats -> wait 500ms -> find conversation -> open it -> return the same conversation ID or "null"
expandChats();
setTimeout(function () {
const cibThreadContainer = searchCibThreadContainer(conversationID, true);
if (cibThreadContainer === null) {
callback("" + null);
}

const loadChatBtn = cibThreadContainer.querySelector("div > div > button");
loadChatBtn.focus();
loadChatBtn.click();
callback("" + conversationID);
}, 500);
}

// Delete conversation
else if (action === "delete") {
// Expand all chats -> wait 500ms -> find conversation -> delete it -> return the same conversation ID or "null"
expandChats();
setTimeout(function () {
const cibThreadContainer = searchCibThreadContainer(conversationID, false);
if (cibThreadContainer === null) {
callback("" + null);
}
const loadChatBtn = cibThreadContainer.querySelector("div > div > button");
loadChatBtn.focus();
loadChatBtn.click();
callback("" + conversationID);
}, 500);
}

const loadChatBtn = cibThreadContainer.querySelector("div > div > button");
loadChatBtn.focus();
const deleteChatBtn = cibThreadContainer.querySelector("div > div > div.controls > button.delete.icon-button");
deleteChatBtn.click();
callback("" + conversationID);
}, 500);
}
// Delete conversation
else if (action === "delete") {
// Expand all chats -> wait 500ms -> find conversation -> delete it -> return the same conversation ID or "null"
expandChats();
setTimeout(function () {
const cibThreadContainer = searchCibThreadContainer(conversationID, false);
if (cibThreadContainer === null) {
callback("" + null);
}

// Rename conversation
else if (action === "rename") {
// Enter edit mode -> wait 500ms -> rename and confirm -> return the same conversation ID
startRenameMode();
setTimeout(function () {
renameChatAndConfirm(conversationID);
callback("" + conversationID);
}, 500);
}
const loadChatBtn = cibThreadContainer.querySelector("div > div > button");
loadChatBtn.focus();
const deleteChatBtn = cibThreadContainer.querySelector("div > div > div.controls > button.delete.icon-button");
deleteChatBtn.click();
callback("" + conversationID);
}, 500);
}

// Log and return error as string
catch (error) {
console.error(error);
callback("" + error);
// Rename conversation
else if (action === "rename") {
// Enter edit mode -> wait 500ms -> rename and confirm -> return the same conversation ID
startRenameMode();
setTimeout(function () {
renameChatAndConfirm(conversationID);
callback("" + conversationID);
}, 500);
}
}

// Log and return error as string
catch (error) {
console.error(error);
callback("" + error);
}
20 changes: 3 additions & 17 deletions src/lmao/ms_copilot/ms_copilot_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -812,30 +812,16 @@ def _conversation_manage(self, action: str, conversation_id: str, raise_on_error
Returns:
bool: True if successful, False if not
"""
# Check if conversationManage.js is injected
is_injected = False
try:
is_injected = self.driver.execute_script("return isManageInjected();")
except:
pass

# Inject JS
if not is_injected:
logging.warning("conversationManage is not injected. Injecting it")
self.driver.execute_script(_INJECT_JS, self._conversation_manage_js)
logging.info(f"Injected? {self.driver.execute_script('return isManageInjected();')}")

# Execute script and wait for callback or timeout
logging.info(f"Trying to {action} conversation")
try:
logging.info(f"Trying to asynchronously {action} conversation")
self.driver.set_script_timeout(_WAIT_TIMEOUT)
conversation_id_ = self.driver.execute_async_script("conversationManage();", action, conversation_id)
conversation_id_ = self.driver.execute_async_script(self._conversation_manage_js, action, conversation_id)
if conversation_id_ is None:
raise Exception(f"Unable to {action} conversation to {conversation_id}")
elif conversation_id_ != conversation_id:
raise Exception(str(conversation_id_))
else:
logging.info(f'Operation "conversation {action}" finished successfully')
logging.info(f'"Conversation {action}" finished successfully')
time.sleep(1)
return True
except Exception as e:
Expand Down

0 comments on commit 9c7cd7f

Please sign in to comment.