From f25d95320f86f9d7a35fbd6ec7710e05f111947d Mon Sep 17 00:00:00 2001 From: GTeam Date: Thu, 30 Dec 2021 18:07:45 +0100 Subject: [PATCH] 2.1 fix send url in selection --- background.js | 70 ++++++++++++++++++++++++++++----------------------- manifest.json | 4 +-- versions.md | 3 +++ 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/background.js b/background.js index b82b1e7..c3a03c8 100644 --- a/background.js +++ b/background.js @@ -38,6 +38,7 @@ function urlConstruct(url, autostart) { return JD2_API_URL + "autostart=" + autostart + "&urls=" + encodeURIComponent(url); } +// Return an url from a text, good enought :-) function linkify(text) { let urlRegex =/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; let url = text.match(urlRegex); @@ -65,24 +66,44 @@ browser.menus.create({ } }, onCreated); -browser.menus.onClicked.addListener((info, tab) => { - switch (info.menuItemId){ - case mnGrabberId: - url_for_JD2 = urlConstruct(info.linkUrl, 0); - break; - case mnDlId: - url_for_JD2 = urlConstruct(info.linkUrl, 1); - break; +// Choose from info (object menus.OnClickData) +// linkUrl over selectionText +// return link or '' +function infoToUrl(info){ + var url = ''; + if (typeof info.linkUrl !== 'undefined'){ + url = info.linkUrl; + } else { + if (typeof info.selectionText !== 'undefined'){ + url = linkify(info.selectionText); + } + } + return url; +} + +browser.menus.onClicked.addListener((info, tab) => { + var url = infoToLink(info); + if (url !== ''){ + switch (info.menuItemId){ + case mnGrabberId: + url_for_JD2 = urlConstruct(url, 0); + break; + case mnDlId: + url_for_JD2 = urlConstruct(url, 1); + break; + } + console.log(url); + console.log(url_for_JD2); + // makeRequest(url_for_JD2); } - makeRequest(url_for_JD2); }); -function updateMenuItem(link) { +function updateMenuItem(url) { browser.menus.update(mnDlId, { - title: 'Download: ' + link + title: 'Download: ' + url }); browser.menus.update(mnGrabberId, { - title: 'Linkgrabber: ' + link + title: 'Linkgrabber: ' + url }); browser.menus.refresh(); } @@ -97,27 +118,14 @@ function allMenusVisible(visible){ } browser.menus.onShown.addListener(info => { - // console.log('jd2 link: ' + info.linkUrl); - // console.log('jd2 selection: ' + info.selectionText); - - // linkUrl over selectionText - if (typeof info.linkUrl !== 'undefined'){ + var url = infoToLink(info); + if (url !== ''){ allMenusVisible(true); - updateMenuItem(info.linkUrl); - return + updateMenuItem(url); + return; } else { - if (typeof info.selectionText !== 'undefined'){ - var url = linkify(info.selectionText); - // console.log('jd2: url: ' + url); - if (url !== ''){ - allMenusVisible(true) - updateMenuItem(url); - return - } else { - allMenusVisible(false) - browser.menus.refresh(); - } - } + allMenusVisible(false); + browser.menus.refresh(); } }); diff --git a/manifest.json b/manifest.json index 37523e8..d2b0602 100644 --- a/manifest.json +++ b/manifest.json @@ -1,8 +1,8 @@ { "manifest_version": 2, "name": "UrlToJD2", - "version": "2.0", - "description": "Push the current url navigator to JDownloader 2 GrabberLink or Download", + "version": "2.1", + "description": "Push an url to JDownloader 2 GrabberLink or Download", "developer": { "name": "GTeam", diff --git a/versions.md b/versions.md index ed0d18f..efff3ba 100644 --- a/versions.md +++ b/versions.md @@ -1,5 +1,8 @@ # Versions +## 2.1 (2021-12-30) +- fix the (undefined) url send to JD2 when text selection + ## 2.0 (2021-12-20) - Large Refactoring - context menu and pageAction can send to Linkgrabber or Download JD2 list.