Skip to content

Commit

Permalink
Convert extension to event page
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Staliś committed Sep 27, 2017
1 parent 58a79cd commit eda7810
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 129 deletions.
2 changes: 2 additions & 0 deletions api/event.js
Expand Up @@ -6,6 +6,8 @@ class Event extends Api {
super();
this.event_ = event;
/*
TODO: Add support for rules.
this.ruleID_ = 0;
this.rules_ = new Map();
*/
Expand Down
9 changes: 7 additions & 2 deletions api/webstore_private.js
@@ -1,7 +1,12 @@
'use strict';

class WebstorePrivate extends Api {
get Result() { return chrome.installer.Result; }
constructor(installer) {
super();
this.installer_ = installer;
}

get Result() { return this.installer_.Result; }

get WebGlStatus() {
return {
Expand All @@ -13,7 +18,7 @@ class WebstorePrivate extends Api {
beginInstallWithManifest3({id, manifest}, callback) {
manifest = JSON.parse(manifest);

chrome.installer.install(
this.installer_.install(
{id, manifest},
() => callback(''),
() => callback(this.Result.USER_CANCELLED));
Expand Down
118 changes: 39 additions & 79 deletions background/app.js
@@ -1,94 +1,54 @@
'use strict';

class App {
static get ACTION_ADD_TO_OPERA() { return 'add-to-opera'; }
static get CRX_HOST_PATTERN() { return '*://clients2.google.com/*'; }
static get WEBSTORE_HOST_PATTERN() { return '*://*.chrome.google.com/*'; }
{
const WEBSTORE_HOST_PATTERN = '*://*.chrome.google.com/*';
const YANDEX_UA_CODE = 'YaBrowser';
const YANDEX_ERROR_MESSAGE = 'welcomeYandex';

class EventPageExtension {
constructor() {
if (!this.isSupportedBrowser_()) {
chrome.management.uninstallSelf();
return;
}

constructor() {
if (!this.isSupportedBrowser_()) {
chrome.management.uninstallSelf();
return;
this.registerListeners_();
}
chrome.installer = Installer.get();

this.proxy_ = new ContentProxy({
'management': Management.get(),
'webstore': Webstore.get(),
'webstorePrivate': WebstorePrivate.get(),
});

this.reloadWebstoreTabs_();
this.setupInstallButton_();
}

isSupportedBrowser_() {
if (navigator.userAgent.includes('YaBrowser')) {
alert(chrome.i18n.getMessage('welcomeYandex'));
return false;
isSupportedBrowser_() {
if (navigator.userAgent.includes(YANDEX_UA_CODE)) {
alert(chrome.i18n.getMessage(YANDEX_ERROR_MESSAGE));
return false;
}
return true;
}
return true;
}

onTabChange_(tab) {
if (!tab.url || !tab.url.includes('chrome.google.com/webstore')) {
return;
registerListeners_() {
chrome.runtime.onConnect.addListener(port => this.createProxy_(port));
chrome.runtime.onInstalled.addListener(({reason}) => {
const {INSTALL, UPDATE} = chrome.runtime.OnInstalledReason;
if (reason === INSTALL || reason === UPDATE) {
this.reloadWebstoreTabs_();
}
});
}

if (tab.url &&
tab.url.includes('chrome.google.com/webstore/detail/')) {
chrome.pageAction.show(tab.id);
} else {
chrome.pageAction.hide(tab.id);
}
}
createProxy_(port) {
const installer = Installer.get();
const management = Management.get();
const webstorePrivate = WebstorePrivate.get(installer);

reloadWebstoreTabs_() {
chrome.tabs.query({'url': this.constructor.WEBSTORE_HOST_PATTERN}, tabs => {
tabs.forEach(tab => chrome.tabs.reload(tab.id));
});
}

setupInstallButton_() {
chrome.pageAction.onClicked.addListener(tab => {
const id = chrome.installer.getIdFromUrl(tab.url);
if (!id) {
return;
}
new ContentProxy(port, installer, {management, webstorePrivate});
}

chrome.installer.isInstalled(id).then(isInstalled => {
chrome.pageAction.hide(tab.id);
if (isInstalled) {
chrome.installer.navigateToExtensionDetails(id);
return;
reloadWebstoreTabs_() {
chrome.tabs.query({url: WEBSTORE_HOST_PATTERN}, tabs => {
for (let {id} of tabs) {
chrome.tabs.reload(id);
}

let onInstallRequest;
new Promise((resolve, reject) => {
onInstallRequest = extensionId => {
if (id === extensionId) {
resolve();
}
};
chrome.installer.onInstallRequest.addListener(onInstallRequest);
chrome.tabs.sendMessage(tab.id, this.constructor.ACTION_ADD_TO_OPERA);
setTimeout(reject, 3000);
}).then(() => {
chrome.installer.onInstallRequest.removeListener(onInstallRequest);
}).catch(() => {
chrome.installer.onInstallRequest.removeListener(onInstallRequest);
chrome.installer.install({id});
});
});
});

chrome.tabs.onActivated.addListener(info => {
chrome.tabs.get(info.tabId, tab => this.onTabChange_(tab));
});
chrome.tabs.onUpdated.addListener(
(id, changes, tab) => this.onTabChange_(tab));
chrome.tabs.onCreated.addListener(tab => this.onTabChange_(tab));
}
}
}

let app = new App();
new EventPageExtension();
}

0 comments on commit eda7810

Please sign in to comment.