diff --git a/scripts/pages/popup.js b/scripts/pages/popup.js index c67adfc..ac1f31c 100644 --- a/scripts/pages/popup.js +++ b/scripts/pages/popup.js @@ -18,7 +18,27 @@ class Page_Popup extends Page { async background() { // Wait until a change in the session storage. - + observe(async (changes) => { + this.update(); + this.switch(); + // First, update site data but retain the URL. + }); + } + + /* + Update the data used by the page. + + @param {boolean} override override the current data. + */ + async update(override = false) { + // Set the reference website when overriding or unset. + (override || !this[`ref`]) ? this[`ref`] = await session.read([`last`, `URL`]) : false; + + // Update all other data. + (this[`ref`]) ? this[`data`] = await session.read([`last`]) : false; + + // Merge objects + (this[`data`] && this[`ref`]) ? this[`data`] = Object.assign(this[`data`], await session.read([`sites`, this[`ref`]])) : false; } async loading() { @@ -32,26 +52,26 @@ class Page_Popup extends Page { "error": "error.htm" } - // This will be rewritten to only get the active tab. It should not change whenever the user clicks another tab; it's assumed that he will open the pop-up seperately over there. - // Get the last edited site. - let SITE = {}; - SITE = await session.read([`last`], -1); - SITE.details = (SITE.url) ? await session.read([`sites`, SITE.url]) : null; - - // Set the relative chrome URLs - (Object.keys(PAGES)).forEach(PAGE => { - PAGES[PAGE] = chrome.runtime.getURL(`pages/popup/${PAGES[PAGE]}`); - }); - - // Check if the site is available. - if (SITE.url ? (!SITE.done) : true) { - this.elements[`frame`].src = PAGES[`loading`]; - } else if (SITE.details.error) { - // Set the iframe src to display the error. - this.elements[`frame`].src = PAGES[`error`]; - } else { - // Set the iframe src to display the results. - this.elements[`frame`].src = PAGES[`results`]; + // Prepare all the necessary data. + await this.update(); + + // Make sure that the website has been selected! + if (this[`ref`]) { + // Set the relative chrome URLs + (Object.keys(PAGES)).forEach(PAGE => { + PAGES[PAGE] = chrome.runtime.getURL(`pages/popup/${PAGES[PAGE]}`); + }); + + // Check if the site is available. + if ((this[`ref`] && this[`data`]) ? (!this[`data`][`done`]) : true) { + this.elements[`frame`].src = PAGES[`loading`]; + } else if (this[`data`][`error`]) { + // Set the iframe src to display the error. + this.elements[`frame`].src = PAGES[`error`]; + } else { + // Set the iframe src to display the results. + this.elements[`frame`].src = PAGES[`results`]; + } } } @@ -59,6 +79,7 @@ class Page_Popup extends Page { this.elements = {}; this.elements[`frame`] = document.querySelectorAll(`iframe.viewer`); + // Check if the frame is available. if (this.elements[`frame`].length) { await this.switch();