function g() { return ((1 + Math.random()) * 65536 | 0).toString(16).substring(1); } function generateUUID() { return g() + g() + "-" + g() + "-" + g() + "-" + g() + "-" + g() + g() + g(); } localStorage.pollid = g() + g(); console.log("Initializing with localStorage = ", localStorage); function getInboxObject() { try { return JSON.parse(localStorage.inbox || '{}'); } catch (e) { console.error("Error parsing inbox from localStorage:", e); return {}; } } function setInboxObject(inboxObj) { localStorage.inbox = JSON.stringify(inboxObj); } function refreshWorkflowyData(callback) { console.log("Refreshing WorkFlowy data..."); var xhr = new XMLHttpRequest(); xhr.open('GET', 'https://workflowy.com/get_initialization_data?client_version=15'); xhr.onload = function() { try { var obj = JSON.parse(this.responseText); var globals = []; for (var i in obj.globals) globals[obj.globals[i][0]] = obj.globals[i][1]; localStorage.userid = globals.USER_ID; localStorage.joined = obj.projectTreeData.mainProjectTreeInfo.dateJoinedTimestampInSeconds; localStorage.transid = obj.projectTreeData.mainProjectTreeInfo.initialMostRecentOperationTransactionId; // Preserve existing inbox settings var currentInbox = getInboxObject(); setInboxObject(currentInbox); console.log("Successfully got initialization data:", localStorage); callback(0); } catch (e) { localStorage.clear(); console.error("Error extracting initialization data", e); callback(1); } }; xhr.send(); console.log("Sending XHR"); } // Create context menu item // chrome.contextMenus.create({ // id: "copyImageLink", // title: "Copy Image Link", // contexts: ["image"] // }); chrome.runtime.onInstalled.addListener(() => { chrome.contextMenus.create({ id: "saveToWorkFlowy", title: "Save Text to WorkFlowy", contexts: ["selection"] }); }); // Add click event listener for the context menu item chrome.contextMenus.onClicked.addListener((info, tab) => { // if (info.menuItemId === "copyImageLink") { // // Get the URL of the clicked image // const imageUrl = info.srcUrl; // // Send message to content script to copy image URL to clipboard // chrome.tabs.executeScript(tab.id, { // code: ` // navigator.clipboard.writeText("!" + '${imageUrl}') // .then(() => { // console.log("Image URL copied to clipboard:", '${imageUrl}'); // }) // .catch(error => { // console.error("Error copying image URL:", error); // }); // ` // }); // } if (info.menuItemId === "saveToWorkFlowy") { const selectedText = info.selectionText; if (!localStorage.userid || !localStorage.inbox) { alert("Extension not configured. Please configure it first."); return; } const inboxObject = getInboxObject(); const contentType = "bookmark"; // You can change this to a more appropriate type if needed if (!(contentType in inboxObject)) { alert("Inbox not set for " + contentType); return; } const inboxId = inboxObject[contentType]; const newuuid = generateUUID(); const timestamp = Math.floor((new Date()).getTime() / 1000) - localStorage.joined; const request = [{ "most_recent_operation_transaction_id": localStorage.transid, "operations": [ { "type": "create", "data": { "projectid": newuuid, "parentid": inboxId, "priority": 9999 }, "client_timestamp": timestamp, "undo_data": {} }, { "type": "edit", "data": { "projectid": newuuid, "name": selectedText, "description": "" }, "client_timestamp": timestamp, "undo_data": { "previous_last_modified": timestamp, "previous_name": "", "previous_description": "" } } ] }]; const data = new FormData(); data.append("client_id", "2015-11-17 19:25:15.397732"); data.append("client_version", 15); data.append("push_poll_id", localStorage.pollid); data.append("push_poll_data", JSON.stringify(request)); data.append("crosscheck_user_id", localStorage.userid); const xhr = new XMLHttpRequest(); xhr.open('POST', 'https://workflowy.com/push_and_poll', true); xhr.onload = function () { const respObj = JSON.parse(this.responseText); if (respObj.logged_out) { alert("WorkFlowy thinks you're logged out! Make sure you're logged in, and if this message persists then try resynchronizing the extension."); } else if (!respObj.results) { alert("Unknown error!"); } else if (respObj.results[0].error_encountered_in_remote_operations) { alert("Unknown error!"); } else { const oldid = localStorage.transid; localStorage.transid = respObj.results[0].new_most_recent_operation_transaction_id; alert("Successfully clipped!"); } }; xhr.send(data); } }); function getInboxObject() { try { return JSON.parse(localStorage.inbox || '{}'); } catch (e) { console.error("Error parsing inbox from localStorage:", e); return {}; } }