diff options
author | Christian Segundo | 2023-11-20 01:18:56 +0100 |
---|---|---|
committer | Christian Segundo | 2023-11-20 01:18:56 +0100 |
commit | 6dab45f7e5b7d063d146829f2c14d6647f1e46dd (patch) | |
tree | ce8dd6e865bd832d879d9edb46c58e9edbeda92c | |
parent | c6845a798c99e96aa0e2f6daece0684a8ac50681 (diff) | |
download | moz-run-this-page-action-master.tar.gz |
-rw-r--r-- | .web-extension-id | 3 | ||||
-rw-r--r-- | background.js | 5 | ||||
-rw-r--r-- | log | 3 | ||||
-rw-r--r-- | manifest.json | 7 | ||||
-rw-r--r-- | page_action.js | 93 | ||||
-rwxr-xr-x | ping_pong.py | 53 |
6 files changed, 148 insertions, 16 deletions
diff --git a/.web-extension-id b/.web-extension-id new file mode 100644 index 0000000..911436b --- /dev/null +++ b/.web-extension-id @@ -0,0 +1,3 @@ +# This file was created by https://github.com/mozilla/web-ext +# Your auto-generated extension ID for addons.mozilla.org is: +moz-glab-local-edit@git.segundo.io
\ No newline at end of file diff --git a/background.js b/background.js index 3035416..e72c07a 100644 --- a/background.js +++ b/background.js @@ -8,7 +8,10 @@ chrome.webNavigation.onCommitted.addListener(async details => { const domains = await StorageGetDomains(); for (let i = 0; i < domains.length; i++) { if (details.url.includes(domains[i])) { - browser.pageAction.show(details.tabId); + const gldata = await parsetab(details.tabId); + if (gldata.repo) { + browser.pageAction.show(details.tabId); + } return; } } @@ -1,3 +0,0 @@ -Received URL: https+vim://gitlab.otters.xyz/skkrty/metatron/-/blob/master/.gitlab-ci.yml?ref_type=heads -Repo Path: skkrty/metatron -Repo exists in local diff --git a/manifest.json b/manifest.json index bffc6e2..6033deb 100644 --- a/manifest.json +++ b/manifest.json @@ -2,7 +2,7 @@ { "manifest_version": 2, "name": "GitLab local edit", - "version": "1.0", + "version": "1.2", "description": "Your editor, your rules", "developer": { "name": "Christian Segundo", @@ -10,9 +10,12 @@ }, "homepage_url": "https://git.segundo.io/moz-glab-local-edit/", "permissions": [ + "activeTab", "tabs", "storage", - "webNavigation" + "webNavigation", + "nativeMessaging", + "<all_urls>" ], "page_action": { "default_title": "Open in ViM", diff --git a/page_action.js b/page_action.js index ccb7925..8efa274 100644 --- a/page_action.js +++ b/page_action.js @@ -1,13 +1,86 @@ -function handleClick(tab) { - const newUrl = tab.url.replace('https://', 'https+vim://'); - console.log(newUrl); - newTab = browser.tabs.create({ url: newUrl }); - - // TODO: close the tab after it's been opened - // this doesn't work as it happens too fast and the app isn't open yet - //newTab.then(function(ntab) { - //browser.tabs.remove(ntab.id); - //}) +function onResponse(response) { + console.log(`Received ${response}`); +} + +function onError(error) { + console.log(`Error: ${error}`); +} + +async function handleClick(tab) { + const gldata = await parsetab(tab.id); + browser.runtime.sendNativeMessage("ping_pong", gldata) + .then(onResponse, onError); +} + +async function parsetab(tabId) { + const tab = browser.tabs.get(tabId); + var data = { + host: null, + repo: null, + file: null, + branch: null + }; + + // try to find the Clone button to determine if we're in a root repo + // <span class="js-clone-dropdown-label">Clone</span> + const hasClone = await tab.then(async function(tab) { + const txt = await browser.tabs.executeScript(tab.id, { + code: 'document.querySelector(".js-clone-dropdown-label").innerText' + }).then(function(result) { + if (!result) { throw new Error("No result"); } + return result[0]; + }).catch(function() { + return ''; + }); + + if (txt && txt === 'Clone ') { + return true; + } + return false; + }); + + // try to find the Blame button to determine if we're in a file inside a repo + const hasBlame = await tab.then(async function(tab) { + const txt = await browser.tabs.executeScript(tab.id, { + code: 'document.querySelector(".js-blob-blame-link").innerText' + }).then(function(result) { + if (!result) { throw new Error("No result"); } + return result[0]; + }).catch(function() { + return ''; + }); + + if (txt && txt === 'Blame') { + return true; + } + return false; + }); + + // if we're not in a repo, or file within a repo, bail out + if (!hasClone && !hasBlame) { + return data; + } + + const url = await tab.then(function(tab) { return new URL(tab.url); }); + data.host = url.hostname; + + // if we're in a root repo, the repo path should be the original url removing + // the hostname and leading slash + if (hasClone && !hasBlame) { + data.repo = url.pathname.substring(1); + // if we're in a file, resort to a regex match + } else if (!hasClone && hasBlame) { + data.repo = url.pathname.match(/^\/(.*)\/-/)[1]; + data.branch = url.pathname.match(/^\/.*\/-\/blob\/(.*)\//)[1]; + data.file = url.pathname.match(/^^\/.*\/-\/blob\/.*\/(.*)(\?)?/)[1]; + if (!data.repo) { throw new Error("Could not find repo"); } + else if (!data.branch) { throw new Error("Could not find branch"); } + else if (!data.file) { throw new Error("Could not find file"); } + } else { + throw new Error("Unknown state"); + } + + return data; } browser.pageAction.onClicked.addListener(handleClick); diff --git a/ping_pong.py b/ping_pong.py new file mode 100755 index 0000000..55bb0e6 --- /dev/null +++ b/ping_pong.py @@ -0,0 +1,53 @@ +#!/usr/bin/env -S python3 -u +# Note that running python with the `-u` flag is required on Windows, +# in order to ensure that stdin and stdout are opened in binary, rather +# than text, mode. + +import sys +import json +import struct +import os +from platform import system as platform + +repo_map = { "gitlab.otters.xyz": "~/git/Cabify" } + +# Read a message from stdin and decode it. +def getMessage(): + rawLength = sys.stdin.buffer.read(4) + if len(rawLength) == 0: + sys.exit(0) + messageLength = struct.unpack('@I', rawLength)[0] + message = sys.stdin.buffer.read(messageLength).decode('utf-8') + return json.loads(message) + +# Encode a message for transmission, +# given its content. +def encodeMessage(messageContent): + # https://docs.python.org/3/library/json.html#basic-usage + # To get the most compact JSON representation, you should specify + # (',', ':') to eliminate whitespace. + # We want the most compact representation because the browser rejects # messages that exceed 1 MB. + encodedContent = json.dumps(messageContent, separators=(',', ':')).encode('utf-8') + encodedLength = struct.pack('@I', len(encodedContent)) + return {'length': encodedLength, 'content': encodedContent} + +# Send an encoded message to stdout +def sendMessage(encodedMessage): + sys.stdout.buffer.write(encodedMessage['length']) + sys.stdout.buffer.write(encodedMessage['content']) + sys.stdout.buffer.flush() + +while True: + receivedMessage = getMessage() + if repo_map.get(receivedMessage["host"]): + path_prefix = os.path.expanduser(repo_map[receivedMessage["host"]]) + path = os.path.join(path_prefix, receivedMessage["repo"]) + if not os.path.exists(path): + sendMessage(encodeMessage(json.dumps({"error": "repo not found"}))) + + # tmux new-window -t 0 -n "$repo_path" "cd ~/git/Cabify/$repo_path && nvim ." + os.system("/opt/homebrew/bin/tmux new-window -t 0 -n \"{}\" \"cd {} && nvim .\"".format(receivedMessage["repo"], path)) + os.system("/usr/bin/osascript -e 'activate application \"Alacritty\"'") + sendMessage(encodeMessage(json.dumps(receivedMessage))) + else: + sendMessage(encodeMessage(json.dumps({"error": "host not found in map"}))) |