diff --git a/src/options/index.js b/src/options/index.js index 51e3e58..b9e0a0f 100644 --- a/src/options/index.js +++ b/src/options/index.js @@ -83,12 +83,14 @@ function saveOptions() { const server = plexServers.find((ser) => ser.clientIdentifier === selectedServerId); // Important detail: we get the token from the selected server, NOT the token the user has entered before. - const plexToken = server.accessToken; - const plexMachineId = server.clientIdentifier; - const plexUrlRoot = getBestConnectionUrl(server); + const serverToken = server.accessToken; + const serverId = server.clientIdentifier; + const serverUrl = getBestConnectionUrl(server); let plexSectionsMovie = []; let plexSectionsShow = []; + // With a "user token" you can access multiple servers. A "normal" token is just for one server. + const plexToken = document.getElementById('plex_token').value; const couchpotatoUrlRoot = document.getElementById('couchpotato_url_root').value; const couchpotatoToken = document.getElementById('couchpotato_token').value; const couchpotatoBasicAuthUsername = document.getElementById('couchpotato_basic_auth_username').value; @@ -108,7 +110,7 @@ function saveOptions() { }); } - getSections(plexUrlRoot, plexToken) + getSections(serverUrl, serverToken) .then((sections) => { // Get the relevant movie and TV show sections plexSectionsMovie = sections @@ -120,13 +122,16 @@ function saveOptions() { }) .then(() => { // `plexLibraryId` is a legacy option, it's no longer necessary after the user has saved again. - storage.remove('plexLibraryId'); + storage.remove(['plexLibraryId', 'plexMachineId', 'plexUrlRoot', 'plexToken']); storage.set({ plexToken, - plexMachineId, - plexUrlRoot, - plexSectionsMovie, - plexSectionsShow, + server: { + id: serverId, + token: serverToken, + url: serverUrl, + movieSections: plexSectionsMovie, + showSections: plexSectionsShow, + }, couchpotatoUrlRoot, couchpotatoToken, couchpotatoBasicAuthUsername, @@ -144,7 +149,6 @@ function saveOptions() { // Restores select box and checkbox state using the preferences // stored in chrome.storage. function restoreOptions() { - // Use default value color = 'red' and likesColor = true. storage.get(null, function(items) { document.getElementById('plex_token').value = items.plexToken || ''; document.getElementById('couchpotato_url_root').value = items.couchpotatoUrlRoot || ''; diff --git a/src/utils.js b/src/utils.js index 2c2b743..394a73f 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,11 +1,11 @@ function doPlexRequest(config, options) { // TODO: it is possible that there are multiple movie sections in Plex, so optimally we'd loop through all of them. - const sectionId = config.plexSectionsMovie[0]; - const url = `${config.plexUrlRoot}/library/sections/${sectionId}/all`; + const sectionId = config.server.movieSections[0]; + const url = `${config.server.url}/library/sections/${sectionId}/all`; return fetch(`${url}?title=${options.title}&year=${options.year}`, { headers: { - 'X-Plex-Token': config.plexToken, + 'X-Plex-Token': config.server.token, 'Accept': 'application/json', } }) @@ -40,21 +40,19 @@ function getOptions() { const storage = chrome.storage.sync || chrome.storage.local; return new Promise(function (resolve, reject) { storage.get(null, function (items) { - if (!items.plexToken || !items.plexMachineId || !items.plexUrlRoot) { + if (!items.plexToken || !items.server) { reject(new Error('Unset options.')); return; } - // TODO: `plexLibraryId` is a legacy option. This is for backwards compatibility. - let plexSectionsMovie = [items.plexLibraryId]; - if (items.plexSectionsMovie && items.plexSectionsMovie.length > 0) { - plexSectionsMovie = items.plexSectionsMovie; - } + // TODO: This is all a bit fucked up at the moment because of backwards compatibility. const options = { - plexUrlRoot: items.plexUrlRoot, - plexToken: items.plexToken, - plexMachineId: items.plexMachineId, - plexSectionsMovie, + server: { + id: items.plexMachineId || items.server.id, + url: items.plexUrlRoot || items.server.url, + token: items.server && items.server.token || items.plexToken, + movieSections: items.plexLibraryId || items.server.movieSections, + }, }; if (items.couchpotatoBasicAuthUsername) { options.couchpotatoBasicAuth = { @@ -145,7 +143,7 @@ function addToCouchPotatoRequest(imdbId) { function modifyPlexButton(el, action, title, key) { if (action === 'found') { - el.href = getPlexMediaUrl(config.plexMachineId, key); + el.href = getPlexMediaUrl(config.server.id, key); el.textContent = 'On Plex'; el.classList.add('web-to-plex-button--found'); }