From 6dab45f7e5b7d063d146829f2c14d6647f1e46dd Mon Sep 17 00:00:00 2001 From: Christian Segundo Date: Mon, 20 Nov 2023 01:18:56 +0100 Subject: wip --- page_action.js | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 83 insertions(+), 10 deletions(-) (limited to 'page_action.js') 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 + // Clone + 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); -- cgit v1.2.3