From dff3a416763d54b287f1d85fc2fdb3e12fae37a6 Mon Sep 17 00:00:00 2001 From: "Stephane Lacoin (aka nxmatic)" Date: Wed, 28 Feb 2024 17:36:37 +0100 Subject: [PATCH] WIP loading in chrome --- app/about.html | 6 +-- app/es-reindex.html | 12 ++--- app/json.html | 11 ++-- app/popup.html | 15 +++--- app/scripts/bkg/common.js | 36 ++++++++----- app/scripts/bkg/designer-live-preview.js | 12 +++-- app/scripts/bkg/execute.js | 41 +++++++++++++++ app/scripts/popup.js | 28 +++++----- app/vendor.babel/chrome/manifest.json | 48 ----------------- app/vendor/chrome/manifest.json | 67 ++++++++++++++---------- gulpfile.mjs | 27 ++++++---- package.json | 1 + pnpm-lock.yaml | 9 ++++ 13 files changed, 170 insertions(+), 143 deletions(-) delete mode 100644 app/vendor.babel/chrome/manifest.json diff --git a/app/about.html b/app/about.html index 464f7a92..d6280131 100644 --- a/app/about.html +++ b/app/about.html @@ -20,9 +20,9 @@ About - Nuxeo Dev Tools - - - + + +
diff --git a/app/es-reindex.html b/app/es-reindex.html index a163c799..dbda750d 100644 --- a/app/es-reindex.html +++ b/app/es-reindex.html @@ -21,12 +21,12 @@ - - - - - - + + + + + +
diff --git a/app/json.html b/app/json.html index 8ce82424..27fba167 100644 --- a/app/json.html +++ b/app/json.html @@ -24,11 +24,10 @@

 
     
- - - - - - + + + + + diff --git a/app/popup.html b/app/popup.html index f280817e..f398ddd7 100644 --- a/app/popup.html +++ b/app/popup.html @@ -20,13 +20,12 @@ Nuxeo Dev Tools - - - - - - - + + + + + +
@@ -76,7 +75,7 @@
  • - +

    Highlight JSON

    diff --git a/app/scripts/bkg/common.js b/app/scripts/bkg/common.js index f629f784..333a9665 100644 --- a/app/scripts/bkg/common.js +++ b/app/scripts/bkg/common.js @@ -28,26 +28,26 @@ const studioExt = window.studioExt = window.studioExt || {}; let dependencies; -let CONNECT_DOMAIN = 'connect.nuxeo.com'; -let CONNECT_URL = `https://${CONNECT_DOMAIN}`; +let CONNECT_URL = new URL('https://connect.nuxeo.com'); // Use the new Promise-based functions -getFromStorage('org.nuxeo.connect.url').then((res) => { - if (!res.value || res.value.length === 0) { +getFromStorage('org.nuxeo.connect.url').then((value) => { + if (!value || value.length === 0) { return; } - CONNECT_DOMAIN = new URL(res.value).hostname; - CONNECT_URL = res.value + CONNECT_URL = new URL(value); }).catch((error) => { console.error(error); }); -const setStudioUrl = window.setStudioUrl = (domain) => setToStorage({ value: domain }).then(() => { - CONNECT_DOMAIN = domain; - CONNECT_URL = `https://${CONNECT_DOMAIN}`; -}).catch((error) => { - console.error(error); -}); +const setConnectUrl = window.setConnectUrl = (url) => + setToStorage({ + 'org.nuxeo.connect.url': url.toString() + }).then(() => { + CONNECT_URL = url; + }).catch((error) => { + console.error(error); + }); const notification = window.notification = (idP, titleP, messageP, img, interaction, clickHandler) => { const click = clickHandler; @@ -123,6 +123,17 @@ const defaultServerError = { }; const getCurrentTabUrl = window.getCurrentTabUrl = (callback) => { + // Check if tabUrl is available in the query parameters + const urlParams = new URLSearchParams(window.location.search); + const tabUrl = urlParams.get('tabUrl'); + + if (tabUrl) { + // If tabUrl is available, call the callback with tabUrl and return + callback(tabUrl); + return; + } + + // If tabUrl is not available, continue with the original logic const queryInfo = { active: true, currentWindow: true, @@ -151,7 +162,6 @@ const getCurrentTabUrl = window.getCurrentTabUrl = (callback) => { console.error(error); }); }; - function newDefaultNuxeo() { return newNuxeo({ baseURL: window.studioExt.server.url }); } function handleErrors(error, serverError) { diff --git a/app/scripts/bkg/designer-live-preview.js b/app/scripts/bkg/designer-live-preview.js index 0e66d010..e35ecbb2 100644 --- a/app/scripts/bkg/designer-live-preview.js +++ b/app/scripts/bkg/designer-live-preview.js @@ -121,9 +121,11 @@ const enable = (projectName, nuxeoInstanceBaseUrl) => { // URL's port is not allowed in urlPattern, thus had to be removed. nuxeoBaseUrl = nuxeoInstanceBaseUrl; const urlPattern = `${nuxeoInstanceBaseUrl.replace(/:\d+/, '')}*`; + const connectLocation = CONNECT_URL.toString(); + const workspaceLocation = new URL('/nuxeo/site/studio/v2/project/${projectName}/workspace/ws.resources', CONNECT_URL).toString(); browser.cookies - .getAll({ domain: CONNECT_DOMAIN }) + .getAll({ domain: CONNECT_URL.hostname }) .then((cookies) => { const cookieHeader = cookies .map(x => x.name + '=' + x.value) @@ -136,7 +138,7 @@ const enable = (projectName, nuxeoInstanceBaseUrl) => { }, ['blocking']); browser.webRequest.onBeforeRequest.addListener(addNewResources, { - urls: [`${CONNECT_URL}/nuxeo/site/studio/v2/project/${projectName}/workspace/ws.resources`], + urls: [workspaceLocation], }, ['requestBody']); let extraInfoSpec = ['blocking', 'requestHeaders'] @@ -145,15 +147,15 @@ const enable = (projectName, nuxeoInstanceBaseUrl) => { } // https://groups.google.com/a/chromium.org/g/chromium-extensions/c/vYIaeezZwfQ browser.webRequest.onBeforeSendHeaders.addListener(addCookieHeaderForConnectRequest, { - urls: [`${CONNECT_URL}/*`], + urls: [`${connectLocation}/*`], }, extraInfoSpec); browser.webRequest.onCompleted.addListener(revertToDefault, { - urls: [`${CONNECT_URL}/*`], + urls: [`${connectLocation}/*`], }, ['responseHeaders']); // fetch available resources from Studio Project - return fetch(`${CONNECT_URL}/nuxeo/site/studio/v2/project/${projectName}/workspace/ws.resources`, { + return fetch(workspaceLocation, { credentials: 'include', }).then((response) => { if (response.ok) { diff --git a/app/scripts/bkg/execute.js b/app/scripts/bkg/execute.js index e69de29b..fae207be 100644 --- a/app/scripts/bkg/execute.js +++ b/app/scripts/bkg/execute.js @@ -0,0 +1,41 @@ +/* +Copyright 2016-2022 Nuxeo + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +window.executeScript = (script, stopSearch, callback) => { + const blob = new Nuxeo.Blob({ + content: new Blob([script], { + type: 'text/plain', + }), + name: 'readPackage.groovy', + mymeType: 'text/plain', + }); + + newDefaultNuxeo() + .operation('RunInputScript') + .params({ + type: 'groovy', + }) + .input(blob) + .execute() + .then(res => res.text()) + .then(callback) + .catch((e) => { + if (stopSearch) { + stopSearch(); + } + console.error(e); + }); +}; diff --git a/app/scripts/popup.js b/app/scripts/popup.js index 495edac5..d8765c67 100644 --- a/app/scripts/popup.js +++ b/app/scripts/popup.js @@ -42,9 +42,7 @@ limitations under the License. println JsonOutput.toJson([installed: addons]);`; - chrome.runtime.getBackgroundPage((bkg) => { - console.log('bkg is of type ' + typeof bkg); - console.log('bkg is of type ' + bkg.constructor.name); + function handleBackgroundPage(bkg) { function debounce(fn, delay) { let timer = null; return function (...args) { @@ -114,10 +112,10 @@ limitations under the License. $('#designer-live-preview-button').removeClass('enabled'); } - const studioUrl = `${bkg.CONNECT_URL}/nuxeo/site/studio/ide?project=${packageName}`; - $('#log-into-studio').attr('href', studioUrl); + const packageLocation = new URL(`/nuxeo/site/studio/ide?project=${packageName}`, bkg.CONNECT_URL).toString(); + $('#log-into-studio').attr('href', new URL('/nuxeo', bkg.CONNECT_URL).toString()); $('#studio').click(() => { - app.browser.createTabs(`${bkg.CONNECT_URL}/nuxeo/site/studio/ide?project=${packageName}`, bkg.studioExt.server.tabId); + app.browser.createTabs(packageLocation, bkg.studioExt.server.tabId); }); $('#hot-reload-button').click(() => { bkg.bkgHotReload(startLoadingHR, stopLoading, true, showDependencyError); @@ -223,8 +221,8 @@ limitations under the License. $('#connect-url').toggle(); }); - if (bkg.CONNECT_DOMAIN !== 'connect.nuxeo.com') { - $('#connect-url-input').val(bkg.CONNECT_DOMAIN); + if (bkg.CONNECT_URL.hostname !== 'connect.nuxeo.com') { + $('#connect-url-input').val(bkg.CONNECT_URL.toString()); } chrome.storage.sync.get('highlight', (res) => { @@ -245,7 +243,7 @@ limitations under the License. const input = $('#connect-url-input').val(); const highlight = $('#highlight-input').prop('checked'); if (input.length > 0) { - bkg.setStudioUrl(input, () => { + bkg.setConnectUrl(input, () => { $('#connect-url').hide(); }); } @@ -261,7 +259,7 @@ limitations under the License. confirmButton: 'Reset', cancelButton: 'Cancel', confirm: () => { - bkg.setStudioUrl('connect.nuxeo.com', () => { + bkg.setConnectUrl('https://connect.nuxeo.com', () => { $('#connect-url-input').val(''); $('#connect-url').hide(); }); @@ -331,8 +329,6 @@ limitations under the License. } }); } - console.log('from current TAB URL'); - bkg.getCurrentTabUrl((url) => { nuxeo = bkg.newNuxeo({ baseURL: url, @@ -346,7 +342,7 @@ limitations under the License. }).catch((e) => { console.log(e); }); - chrome.tabs.executeScript(checkStudioPkg, noStudioPackageFound, (text) => { + bkg.executeScript(checkStudioPkg, noStudioPackageFound, (text) => { const pkgName = JSON.parse(text).studio; if (pkgName) { studioPackageFound(pkgName); @@ -731,5 +727,9 @@ limitations under the License. console.log('O Canada!'); }); }); - }); + } + + window.handleBackgroundPage = handleBackgroundPage; + + app.browser.getBackgroundPage(handleBackgroundPage); })(window); diff --git a/app/vendor.babel/chrome/manifest.json b/app/vendor.babel/chrome/manifest.json deleted file mode 100644 index 8bef27fd..00000000 --- a/app/vendor.babel/chrome/manifest.json +++ /dev/null @@ -1,48 +0,0 @@ -{ - "name": "Nuxeo Dev Tools", - "version": "3.1.0", - "manifest_version": 2, - "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'", - "description": "A handy collection of shortcuts and tools for the Nuxeo developer", - "icons": { - "16": "images/nuxeo-16.png", - "128": "images/nuxeo-128.png" - }, - "default_locale": "en", - "background": { - "scripts": [ - "../../../node_modules/webextension-polyfill/dist/browser-polyfill.min.js", - "../../../node_modules/nuxeo/nuxeo.js", - "../../../app/libs/purify.min.js", - "../../../app/vendor/chrome/chrome-bkg.js", - "../../../app/scripts/bkg.js" - ], - "persistent": true - }, - "permissions": [ - "storage", - "cookies", - "tabs", - "http://*/*", - "https://*/*", - "activeTab", - "https://ajax.googleapis.com/", - "notifications", - "background", - "", - "webRequest", - "webRequestBlocking", - "https://app.prodpad.com/" - ], - "omnibox": { - "keyword": "nx" - }, - "page_action": { - "default_icon": { - "19": "images/nuxeo-19.png", - "38": "images/nuxeo-38.png" - }, - "default_title": "Nuxeo Dev Tools", - "default_popup": "popup.html" - } -} diff --git a/app/vendor/chrome/manifest.json b/app/vendor/chrome/manifest.json index 973e116a..95172c40 100644 --- a/app/vendor/chrome/manifest.json +++ b/app/vendor/chrome/manifest.json @@ -1,32 +1,41 @@ { - "name": "Nuxeo Dev Tools", - "version": "3.1.0", - "description": "A handy collection of shortcuts and tools for the Nuxeo developer", - "icons": { - "16": "images/nuxeo-16.png", - "128": "images/nuxeo-128.png" + "name": "Nuxeo Dev Tools", + "version": "3.1.0", + "manifest_version": 2, + "content_security_policy": "script-src 'self' https://ssl.google-analytics.com; object-src 'self'", + "description": "A handy collection of shortcuts and tools for the Nuxeo developer", + "icons": { + "16": "images/nuxeo-16.png", + "128": "images/nuxeo-128.png" + }, + "default_locale": "en", + "background": { + "persistent": true + }, + "permissions": [ + "storage", + "cookies", + "tabs", + "http://*/*", + "https://*/*", + "activeTab", + "https://ajax.googleapis.com/", + "notifications", + "background", + "", + "webRequest", + "webRequestBlocking", + "https://app.prodpad.com/" + ], + "omnibox": { + "keyword": "nx" + }, + "page_action": { + "default_icon": { + "19": "images/nuxeo-19.png", + "38": "images/nuxeo-38.png" }, - "default_locale": "en", - "permissions": [ - "storage", "cookies", "tabs", "activeTab", "notifications", "background", "webRequest", "declarativeNetRequest" - ], - "host_permissions": [ - "https://ajax.googleapis.com/", - "https://app.prodpad.com/" - ], - "optional_host_permissions": [ - "http://*/*", - "https://*/*" - ], - "omnibox": { - "keyword": "nx" - }, - "action": { - "default_icon": { - "19": "images/nuxeo-19.png", - "38": "images/nuxeo-38.png" - }, - "default_title": "Nuxeo Dev Tools", - "default_popup": "popup.html" - } + "default_title": "Nuxeo Dev Tools", + "default_popup": "popup.html" + } } diff --git a/gulpfile.mjs b/gulpfile.mjs index c613f138..76a21508 100644 --- a/gulpfile.mjs +++ b/gulpfile.mjs @@ -190,15 +190,18 @@ gulp.task('extras', () => gulp gulp.task('libs', () => gulp .src( [ - 'libs/**', - '!libs/highlight.js', + 'node_modules/bootstrap/dist/css/bootstrap.min.css', + 'node_modules/bootstrap/dist/js/bootstrap.min.js', + 'node_modules/bootstrap/dist/js/bootstrap.min.js.map', + 'node_modules/jquery/dist/jquery.min.js', + 'node_modules/jquery/dist/jquery.min.map', + 'node_modules/jquery-confirm/dist/jquery-confirm.min.css', + 'node_modules/jquery-confirm/dist/jquery-confirm.min.js', + 'node_modules/dompurify/dist/purify.min.js', + 'node_modules/dompurify/dist/purify.min.js.map', ], - { - base: 'libs', - dot: true, - }, ) - .pipe(gulp.dest('build/base'))); + .pipe(gulp.dest('build/base/libs'))); gulp.task('styles', () => gulp.src('app/styles/*.css').pipe(gulp.dest('build/base/styles'))); @@ -237,7 +240,7 @@ gulp.task('babel:vendor:chrome', () => gulp .pipe(gulp.dest('build/chrome.babel'))); gulp.task('babel:vendor:firefox', () => gulp - .src('app/vendor/firefox/*. js') + .src('app/vendor/firefox/*.js') .pipe( babel({ presets: ['@babel/env'], @@ -309,7 +312,7 @@ gulp.task('background:chrome', () => ( .src([ './node_modules/webextension-polyfill/dist/browser-polyfill.js', './node_modules/nuxeo/nuxeo.js', - './node_modules/purify/lib/purify.js', + './node_modules/dompurify/dist/purify.js', './app/vendor/chrome/bkg.js', './build/base.babel/bkg.js', ]) @@ -373,12 +376,14 @@ gulp.task('build:chrome:json', () => gulp .pipe( cheerio(($) => { const $body = $('body'); - appendScript($body, 'libs/highlight.js'); + appendScript($body, 'node_modules/highlight/lib/highlight.js'); }), ) .pipe(gulp.dest('build/chrome'))); -gulp.task('build:chrome:highlight', () => gulp.src('libs/highlight.js').pipe(gulp.dest('build/chrome/libs'))); +gulp.task('build:chrome:highlight', () => gulp + .src('node_modules/highlight/lib/highlight.js') + .pipe(gulp.dest('build/chrome/libs'))); gulp.task( 'build:chrome', diff --git a/package.json b/package.json index 6cd4bc14..70499473 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,7 @@ "gulp-zip": "^5.1.0", "highlight": "^0.2.4", "jquery": "^3.7.1", + "jquery-confirm": "^3.3.4", "jsdom": "^24.0.0", "list-directory-contents": "0.0.3", "merge-stream": "^2.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index aa58264d..61b7da9c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -100,6 +100,9 @@ devDependencies: jquery: specifier: ^3.7.1 version: 3.7.1 + jquery-confirm: + specifier: ^3.3.4 + version: 3.3.4 jsdom: specifier: ^24.0.0 version: 24.0.0 @@ -5112,6 +5115,12 @@ packages: supports-color: 8.1.1 dev: true + /jquery-confirm@3.3.4: + resolution: {integrity: sha512-SF3mX2kWAK9FP/Hut1uLHI30LPZ9DAki3Jq6JId9vg6AlMVugjlh40APtuxPQ/0C44XTkQFSVyRrg57NMbaicA==} + dependencies: + jquery: 3.7.1 + dev: true + /jquery@3.7.1: resolution: {integrity: sha512-m4avr8yL8kmFN8psrbFFFmB/If14iN5o9nw/NgnnM+kybDJpRsAynV2BsfpTYrTRysYUdADVD7CkUUizgkpLfg==} dev: true