Add functionality for getting and observing zoom factor

This commit is contained in:
toasted-nutbread 2019-12-23 11:59:47 -05:00
parent f662137481
commit 8ef57936d7
3 changed files with 39 additions and 3 deletions

View File

@ -51,9 +51,12 @@ class Backend {
this.onOptionsUpdated('background'); this.onOptionsUpdated('background');
if (chrome.commands !== null && typeof chrome.commands === 'object') { if (isObject(chrome.commands) && isObject(chrome.commands.onCommand)) {
chrome.commands.onCommand.addListener((command) => this._runCommand(command)); chrome.commands.onCommand.addListener((command) => this._runCommand(command));
} }
if (isObject(chrome.tabs) && isObject(chrome.tabs.onZoomChange)) {
chrome.tabs.onZoomChange.addListener((info) => this._onZoomChange(info));
}
chrome.runtime.onMessage.addListener(this.onMessage.bind(this)); chrome.runtime.onMessage.addListener(this.onMessage.bind(this));
const options = this.getOptionsSync(this.optionsContext); const options = this.getOptionsSync(this.optionsContext);
@ -94,6 +97,11 @@ class Backend {
} }
} }
_onZoomChange({tabId, oldZoomFactor, newZoomFactor}) {
const callback = () => this.checkLastError(chrome.runtime.lastError);
chrome.tabs.sendMessage(tabId, {action: 'zoomChanged', params: {oldZoomFactor, newZoomFactor}}, callback);
}
applyOptions() { applyOptions() {
const options = this.getOptionsSync(this.optionsContext); const options = this.getOptionsSync(this.optionsContext);
if (!options.general.enable) { if (!options.general.enable) {
@ -527,6 +535,24 @@ class Backend {
return await requestText(url, 'GET'); return await requestText(url, 'GET');
} }
_onApiGetZoom(params, sender) {
if (!sender || !sender.tab) {
return Promise.reject(new Error('Invalid tab'));
}
return new Promise((resolve, reject) => {
const tabId = sender.tab.id;
chrome.tabs.getZoom(tabId, (zoomFactor) => {
const e = chrome.runtime.lastError;
if (e) {
reject(new Error(e.message));
} else {
resolve({zoomFactor});
}
});
});
}
// Command handlers // Command handlers
async _onCommandSearch(params) { async _onCommandSearch(params) {
@ -741,7 +767,8 @@ Backend._messageHandlers = new Map([
['injectStylesheet', (self, ...args) => self._onApiInjectStylesheet(...args)], ['injectStylesheet', (self, ...args) => self._onApiInjectStylesheet(...args)],
['getEnvironmentInfo', (self, ...args) => self._onApiGetEnvironmentInfo(...args)], ['getEnvironmentInfo', (self, ...args) => self._onApiGetEnvironmentInfo(...args)],
['clipboardGet', (self, ...args) => self._onApiClipboardGet(...args)], ['clipboardGet', (self, ...args) => self._onApiClipboardGet(...args)],
['getDisplayTemplatesHtml', (self, ...args) => self._onApiGetDisplayTemplatesHtml(...args)] ['getDisplayTemplatesHtml', (self, ...args) => self._onApiGetDisplayTemplatesHtml(...args)],
['getZoom', (self, ...args) => self._onApiGetZoom(...args)]
]); ]);
Backend._commandHandlers = new Map([ Backend._commandHandlers = new Map([

View File

@ -105,6 +105,10 @@ function apiGetDisplayTemplatesHtml() {
return _apiInvoke('getDisplayTemplatesHtml'); return _apiInvoke('getDisplayTemplatesHtml');
} }
function apiGetZoom() {
return _apiInvoke('getZoom');
}
function _apiInvoke(action, params={}) { function _apiInvoke(action, params={}) {
const data = {action, params}; const data = {action, params};
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {

View File

@ -244,7 +244,8 @@ const yomichan = (() => {
this._messageHandlers = new Map([ this._messageHandlers = new Map([
['getUrl', this._onMessageGetUrl.bind(this)], ['getUrl', this._onMessageGetUrl.bind(this)],
['optionsUpdate', this._onMessageOptionsUpdate.bind(this)] ['optionsUpdate', this._onMessageOptionsUpdate.bind(this)],
['zoomChanged', this._onMessageZoomChanged.bind(this)]
]); ]);
chrome.runtime.onMessage.addListener(this._onMessage.bind(this)); chrome.runtime.onMessage.addListener(this._onMessage.bind(this));
@ -274,6 +275,10 @@ const yomichan = (() => {
_onMessageOptionsUpdate({source}) { _onMessageOptionsUpdate({source}) {
this.trigger('optionsUpdate', {source}); this.trigger('optionsUpdate', {source});
} }
_onMessageZoomChanged({oldZoomFactor, newZoomFactor}) {
this.trigger('zoomChanged', {oldZoomFactor, newZoomFactor});
}
} }
return new Yomichan(); return new Yomichan();