Skip to content

Commit

Permalink
adapt common.js for running using the chrome V3 API
Browse files Browse the repository at this point in the history
  • Loading branch information
nxmatic committed Jan 16, 2024
1 parent c114981 commit 380edf0
Show file tree
Hide file tree
Showing 5 changed files with 648 additions and 39 deletions.
104 changes: 104 additions & 0 deletions app/scripts.babel/bkg/chrome-api-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Higher-order function to select the correct version of a function
function selectVersion(v2Function, v3Function) {
return chrome.runtime.getManifest().manifest_version === 2 ? v2Function : v3Function;
}

// Create a wrapper function for chrome.storage.sync.get that returns a Promise
function getFromStoragePromiseWrapper(key) {
return new Promise((resolve, reject) => {
chrome.storage.sync.get(key, (result) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(result);
}
});
});
}

// Use chrome.storage.sync.get that returns a Promise
function getFromStoragePromise(key) {
return chrome.storage.sync.get(key);
}

// Export the correct functions based on the manifest version
export const getFromStorage = selectVersion(getFromStoragePromiseWrapper, getFromStoragePromise);

// Create a wrapper function for chrome.storage.sync.set that returns a Promise
function setToStoragePromiseWrapper(obj) {
return new Promise((resolve, reject) => {
chrome.storage.sync.set(obj, () => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve();
}
});
});
}

// Use chrome.storage.sync.set that returns a Promise
function setToStoragePromise(obj) {
return chrome.storage.sync.set(obj);
}

// Export the correct functions based on the manifest version
export const setToStorage = selectVersion(setToStoragePromiseWrapper, setToStoragePromise);

// Create a wrapper function for chrome.notifications.create that returns a Promise
function createNotificationPromiseWrapper(id, options) {
return new Promise((resolve, reject) => {
chrome.notifications.create(id, options, (notificationId) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(notificationId);
}
});
});
}

// Use chrome.notifications.create that returns a Promise
function createNotificationPromise(id, options) {
return chrome.notifications.create(id, options);
}

export const createNotification = selectVersion(createNotifactionPromiseWrapper, createNotificationPromise);

// Create a wrapper function for chrome.tabs.query that returns a Promise
function queryTabsPromiseWrapper(queryInfo) {
return new Promise((resolve, reject) => {
chrome.tabs.query(queryInfo, (tabs) => {
if (chrome.runtime.lastError) {
reject(chrome.runtime.lastError);
} else {
resolve(tabs);
}
});
});
}

// Use chrome.tabs.query that returns a Promise
function queryTabsPromise(queryInfo) {
return chrome.tabs.query(queryInfo);
}

export const queryTabs = selectVersion(queryTabsPromiseWrapper, queryTabsNotificationPromise);

// V2 version of addNotificationClickListener
function addNotificationClickListenerWithNotifactions(clickHandler) {
chrome.notifications.onClicked.addListener((notificationId) => {
clickHandler(notificationId);
});
}

// V3 version of addNotificationClickListener
function addNotificationClickListenerWithAction(clickHandler) {
chrome.action.onClicked.addListener((tab) => {
clickHandler(tab.id);
});
}

// Export the correct function based on the manifest version
export const addNotificationClickListener =
selectVersion(addNotificationClickListenerWithNotifactions, addNotificationClickListenerWithAction);
69 changes: 33 additions & 36 deletions app/scripts.babel/bkg/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import {
getFromStorage, setToStorage, createNotification, queryTabs, addNotificationClickListener,
} from './chrome-api-adapter';

const nxPattern = /(^https?:\/\/[A-Za-z_\.0-9:-]+\/[A-Za-z_\.0-9-]+\/)(?:(?:nxdoc|nxpath|nxsearch|nxadmin|nxhome|nxdam|nxdamid|site\/[A-Za-z_\.0-9-]+)\/[A-Za-z_\.0-9-]+|view_documents\.faces|ui\/#!\/|[a-zA-Z0-9_%]+\/?|view_domains\.faces|home\.html|view_home\.faces)/;

window.app = window.app || {};
Expand All @@ -24,52 +28,42 @@ let dependencies;
let CONNECT_DOMAIN = 'connect.nuxeo.com';
let CONNECT_URL = `https://${CONNECT_DOMAIN}`;

chrome.storage.sync.get('value', (res) => {
// Use the new Promise-based functions
getFromStorage('value').then((res) => {
if (res.value && res.value.length > 0) {
CONNECT_DOMAIN = res.value;
CONNECT_URL = `https://${CONNECT_DOMAIN}`;
} else {
CONNECT_DOMAIN = 'connect.nuxeo.com';
CONNECT_URL = `https://${CONNECT_DOMAIN}`;
}
}).catch((error) => {
console.error(error);
});

const setStudioUrl = window.setStudioUrl = (domain, cb) => {
return chrome.storage.sync.set({ value: domain }, () => {
CONNECT_DOMAIN = domain;
CONNECT_URL = `https://${CONNECT_DOMAIN}`;
if (cb) {
cb();
}
});
}
const setStudioUrl = window.setStudioUrl = (domain) => setToStorage({ value: domain }).then(() => {
CONNECT_DOMAIN = domain;
CONNECT_URL = `https://${CONNECT_DOMAIN}`;
}).catch((error) => {
console.error(error);
});

const notification = window.notification = (idP, titleP, messageP, img, interaction, clickHandler) => {
const click = clickHandler;
try {
chrome.notifications.create(idP, {
type: 'basic',
title: titleP,
message: messageP,
iconUrl: img,
requireInteraction: interaction,
}, () => {
console.log(chrome.runtime.lastError);
});

} catch (err) {
chrome.notifications.create(idP, {
type: 'basic',
title: titleP,
message: messageP,
iconUrl: img,
}, () => {
console.log(chrome.runtime.lastError);
});
}
createNotification(idP, {
type: 'basic',
title: titleP,
message: messageP,
iconUrl: img,
requireInteraction: interaction,
}).then(() => {
console.log(chrome.runtime.lastError);
}).catch((error) => {
console.error(error);
});
if (clickHandler) {
browser.notifications.onClicked.addListener((notificationId) => {
console.log('notification clicked ' + notificationId);
chrome.notifications.onClicked.addListener((notificationId) => {
console.log(`notification clicked ${notificationId}`);
if (notificationId === idP) {
console.log('executing handler');
click();
Expand Down Expand Up @@ -133,21 +127,24 @@ const getCurrentTabUrl = window.getCurrentTabUrl = (callback) => {
currentWindow: true,
};

chrome.tabs.query(queryInfo, (tabs) => {
queryTabs({
active: true,
currentWindow: true,
}).then((tabs) => {
const [tab] = tabs;
const matchGroups = nxPattern.exec(tab.url);
if (!matchGroups) {
callback(null);
return;
}

const [, url] = matchGroups;
window.studioExt.server = {
url,
tabId: tab.id,
};

callback(url);
}).catch((error) => {
console.error(error);
});
};

Expand Down
27 changes: 24 additions & 3 deletions gulpfile.babel.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,31 @@ gulp.task('images', () => {
return gulp.src('app/images/*').pipe(gulp.dest('dist/base/images'));
});

const webpack = require('webpack-stream');

gulp.task('babel:bkg', () => {
return gulp.src('app/scripts.babel/bkg/*.js').pipe(concat('bkg.js')).pipe($.babel({
presets: ['@babel/env']
})).pipe(gulp.dest('app/scripts'));
return gulp.src('app/scripts.babel/bkg/*.js')
.pipe(webpack({
mode: 'production',
output: {
filename: 'bkg.js',
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/env'],
},
},
},
],
},
}))
.pipe(gulp.dest('app/scripts'));
});

gulp.task('babel:base', gulp.series('babel:bkg'), () => {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@wdio/spec-reporter": "^5.12.1",
"@wdio/sync": "^5.12.3",
"babel-eslint": "^10.0.3",
"babel-loader": "^9.1.3",
"del": "^5.1.0",
"eslint-config-airbnb-base": "^14.0.0",
"eslint-plugin-import": "^2.18.2",
Expand All @@ -39,6 +40,8 @@
"run-sequence": "^2.2.1",
"wdio-cucumberjs-json-reporter": "^1.1.0",
"webdriverio": "^5.12.5",
"webpack": "^5.89.0",
"webpack-stream": "^7.0.0",
"wiredep": "^4.0.0"
},
"eslintConfig": {
Expand Down
Loading

0 comments on commit 380edf0

Please sign in to comment.