From 8e66ba315aae1291ab1dd2877798868a52206ab0 Mon Sep 17 00:00:00 2001 From: Bill Thornton Date: Tue, 5 Apr 2022 15:15:54 -0400 Subject: [PATCH 1/3] Remove getWindowLocationSearch global and duplicate implementation --- .eslintrc.js | 1 - src/components/appRouter.js | 14 +------------- src/scripts/clientUtils.js | 1 + src/scripts/site.js | 16 +--------------- src/utils/url.ts | 13 +++++++++++++ 5 files changed, 16 insertions(+), 29 deletions(-) create mode 100644 src/utils/url.ts diff --git a/.eslintrc.js b/.eslintrc.js index b78b730950..7242938a8e 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -194,7 +194,6 @@ module.exports = { 'DashboardPage': 'writable', 'Emby': 'readonly', 'getParameterByName': 'writable', - 'getWindowLocationSearch': 'writable', 'Globalize': 'writable', 'Hls': 'writable', 'dfnshelper': 'writable', diff --git a/src/components/appRouter.js b/src/components/appRouter.js index 4e45f343c8..4a7224fe5e 100644 --- a/src/components/appRouter.js +++ b/src/components/appRouter.js @@ -12,6 +12,7 @@ import Dashboard from '../scripts/clientUtils'; import ServerConnections from './ServerConnections'; import alert from './alert'; import reactControllerFactory from './reactControllerFactory'; +import { getWindowLocationSearch } from '../utils/url.ts'; class AppRouter { allRoutes = []; @@ -678,19 +679,6 @@ class AppRouter { }; } - getWindowLocationSearch() { - const currentPath = this.currentRouteInfo ? (this.currentRouteInfo.path || '') : ''; - - const index = currentPath.indexOf('?'); - let search = ''; - - if (index !== -1) { - search = currentPath.substring(index); - } - - return search || ''; - } - showGuide() { Dashboard.navigate('livetv.html?tab=1'); } diff --git a/src/scripts/clientUtils.js b/src/scripts/clientUtils.js index ac50e57c5a..bc9e899490 100644 --- a/src/scripts/clientUtils.js +++ b/src/scripts/clientUtils.js @@ -10,6 +10,7 @@ import datetime from '../scripts/datetime'; import DirectoryBrowser from '../components/directorybrowser/directorybrowser'; import dialogHelper from '../components/dialogHelper/dialogHelper'; import itemIdentifier from '../components/itemidentifier/itemidentifier'; +import { getWindowLocationSearch } from '../utils/url.ts'; export function getCurrentUser() { return window.ApiClient.getCurrentUser(false); diff --git a/src/scripts/site.js b/src/scripts/site.js index 263fed80d2..593e3475c6 100644 --- a/src/scripts/site.js +++ b/src/scripts/site.js @@ -26,6 +26,7 @@ import './routes'; import '../components/themeMediaPlayer'; import './autoBackdrops'; import { pageClassOn, serverAddress } from './clientUtils'; +import { getWindowLocationSearch } from '../utils/url.ts'; import './screensavermanager'; import './serverNotifications'; import '../components/playback/playerSelectionMenu'; @@ -41,21 +42,6 @@ import SyncPlayHtmlAudioPlayer from '../components/syncPlay/ui/players/HtmlAudio import { currentSettings } from './settings/userSettings'; import taskButton from './taskbutton'; -// TODO: Move this elsewhere -window.getWindowLocationSearch = function(win) { - let search = (win || window).location.search; - - if (!search) { - const index = window.location.href.indexOf('?'); - - if (index != -1) { - search = window.location.href.substring(index); - } - } - - return search || ''; -}; - // TODO: Move this elsewhere window.getParameterByName = function(name, url) { name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]'); diff --git a/src/utils/url.ts b/src/utils/url.ts new file mode 100644 index 0000000000..fa4489c626 --- /dev/null +++ b/src/utils/url.ts @@ -0,0 +1,13 @@ +export const getWindowLocationSearch = (win: Window | null | undefined) => { + let search = (win || window).location.search; + + if (!search) { + const index = window.location.href.indexOf('?'); + + if (index != -1) { + search = window.location.href.substring(index); + } + } + + return search || ''; +}; From 67169e2a6a8dd3898cd386ca2aed9949d32e4150 Mon Sep 17 00:00:00 2001 From: Bill Thornton Date: Tue, 5 Apr 2022 15:58:12 -0400 Subject: [PATCH 2/3] Remove getParameterByName global --- .eslintrc.js | 1 - src/components/favoriteitems.js | 1 + src/controllers/dashboard/devices/device.js | 1 + src/controllers/dashboard/dlna/profile.js | 1 + .../dashboard/notifications/notification/index.js | 1 + .../dashboard/scheduledtasks/scheduledtask.js | 1 + src/controllers/livetvguideprovider.js | 1 + src/controllers/livetvtuner.js | 1 + src/scripts/editorsidebar.js | 1 + src/scripts/libraryMenu.js | 1 + src/scripts/site.js | 13 ------------- src/utils/url.ts | 15 ++++++++++++++- 12 files changed, 23 insertions(+), 15 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 7242938a8e..324f3d3e44 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -193,7 +193,6 @@ module.exports = { 'DlnaProfilePage': 'writable', 'DashboardPage': 'writable', 'Emby': 'readonly', - 'getParameterByName': 'writable', 'Globalize': 'writable', 'Hls': 'writable', 'dfnshelper': 'writable', diff --git a/src/components/favoriteitems.js b/src/components/favoriteitems.js index 4230217429..7fd4baa5d0 100644 --- a/src/components/favoriteitems.js +++ b/src/components/favoriteitems.js @@ -5,6 +5,7 @@ import { appHost } from './apphost'; import imageLoader from './images/imageLoader'; import globalize from '../scripts/globalize'; import layoutManager from './layoutManager'; +import { getParameterByName } from '../utils/url.ts'; import '../assets/css/scrollstyles.scss'; import '../elements/emby-itemscontainer/emby-itemscontainer'; diff --git a/src/controllers/dashboard/devices/device.js b/src/controllers/dashboard/devices/device.js index 607986c7f3..e2f000fef5 100644 --- a/src/controllers/dashboard/devices/device.js +++ b/src/controllers/dashboard/devices/device.js @@ -3,6 +3,7 @@ import dom from '../../../scripts/dom'; import '../../../elements/emby-input/emby-input'; import '../../../elements/emby-button/emby-button'; import Dashboard from '../../../scripts/clientUtils'; +import { getParameterByName } from '../../../utils/url.ts'; /* eslint-disable indent */ diff --git a/src/controllers/dashboard/dlna/profile.js b/src/controllers/dashboard/dlna/profile.js index 00088cb6b6..4fd2f6415b 100644 --- a/src/controllers/dashboard/dlna/profile.js +++ b/src/controllers/dashboard/dlna/profile.js @@ -9,6 +9,7 @@ import '../../../elements/emby-checkbox/emby-checkbox'; import '../../../components/listview/listview.scss'; import Dashboard from '../../../scripts/clientUtils'; import toast from '../../../components/toast/toast'; +import { getParameterByName } from '../../../utils/url.ts'; /* eslint-disable indent */ diff --git a/src/controllers/dashboard/notifications/notification/index.js b/src/controllers/dashboard/notifications/notification/index.js index 2b562ea073..8c3325c579 100644 --- a/src/controllers/dashboard/notifications/notification/index.js +++ b/src/controllers/dashboard/notifications/notification/index.js @@ -2,6 +2,7 @@ import escapeHtml from 'escape-html'; import 'jquery'; import '../../../../elements/emby-checkbox/emby-checkbox'; import Dashboard from '../../../../scripts/clientUtils'; +import { getParameterByName } from '../../../../utils/url.ts'; function fillItems(elem, items, cssClass, idPrefix, currentList, isEnabledList) { let html = '
'; diff --git a/src/controllers/dashboard/scheduledtasks/scheduledtask.js b/src/controllers/dashboard/scheduledtasks/scheduledtask.js index bd11ed3ca6..c326a689fc 100644 --- a/src/controllers/dashboard/scheduledtasks/scheduledtask.js +++ b/src/controllers/dashboard/scheduledtasks/scheduledtask.js @@ -7,6 +7,7 @@ import '../../../elements/emby-input/emby-input'; import '../../../elements/emby-button/emby-button'; import '../../../elements/emby-select/emby-select'; import confirm from '../../../components/confirm/confirm'; +import { getParameterByName } from '../../../utils/url.ts'; /* eslint-disable indent */ diff --git a/src/controllers/livetvguideprovider.js b/src/controllers/livetvguideprovider.js index ecdb186e9f..ea7c35d332 100644 --- a/src/controllers/livetvguideprovider.js +++ b/src/controllers/livetvguideprovider.js @@ -2,6 +2,7 @@ import { Events } from 'jellyfin-apiclient'; import loading from '../components/loading/loading'; import globalize from '../scripts/globalize'; import Dashboard, { pageIdOn } from '../scripts/clientUtils'; +import { getParameterByName } from '../utils/url.ts'; function onListingsSubmitted() { Dashboard.navigate('livetvstatus.html'); diff --git a/src/controllers/livetvtuner.js b/src/controllers/livetvtuner.js index 4d2ecb738e..18e4d83721 100644 --- a/src/controllers/livetvtuner.js +++ b/src/controllers/livetvtuner.js @@ -6,6 +6,7 @@ import '../elements/emby-button/emby-button'; import '../elements/emby-checkbox/emby-checkbox'; import '../elements/emby-select/emby-select'; import Dashboard from '../scripts/clientUtils'; +import { getParameterByName } from '../utils/url.ts'; function isM3uVariant(type) { return ['nextpvr'].indexOf(type || '') !== -1; diff --git a/src/scripts/editorsidebar.js b/src/scripts/editorsidebar.js index 55a90f9d04..2861999974 100644 --- a/src/scripts/editorsidebar.js +++ b/src/scripts/editorsidebar.js @@ -3,6 +3,7 @@ import 'jquery'; import globalize from './globalize'; import 'material-design-icons-iconfont'; import Dashboard from './clientUtils'; +import { getParameterByName } from '../utils/url.ts'; /* eslint-disable indent */ diff --git a/src/scripts/libraryMenu.js b/src/scripts/libraryMenu.js index f4051ea83c..7bf5cad478 100644 --- a/src/scripts/libraryMenu.js +++ b/src/scripts/libraryMenu.js @@ -19,6 +19,7 @@ import '../assets/css/flexstyles.scss'; import Dashboard, { pageClassOn } from './clientUtils'; import ServerConnections from '../components/ServerConnections'; import Headroom from 'headroom.js'; +import { getParameterByName } from '../utils/url.ts'; /* eslint-disable indent */ diff --git a/src/scripts/site.js b/src/scripts/site.js index 593e3475c6..7f8c4d751f 100644 --- a/src/scripts/site.js +++ b/src/scripts/site.js @@ -26,7 +26,6 @@ import './routes'; import '../components/themeMediaPlayer'; import './autoBackdrops'; import { pageClassOn, serverAddress } from './clientUtils'; -import { getWindowLocationSearch } from '../utils/url.ts'; import './screensavermanager'; import './serverNotifications'; import '../components/playback/playerSelectionMenu'; @@ -43,18 +42,6 @@ import { currentSettings } from './settings/userSettings'; import taskButton from './taskbutton'; // TODO: Move this elsewhere -window.getParameterByName = function(name, url) { - name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]'); - const regexS = '[\\?&]' + name + '=([^&#]*)'; - const regex = new RegExp(regexS, 'i'); - const results = regex.exec(url || getWindowLocationSearch()); - - if (results == null) { - return ''; - } - - return decodeURIComponent(results[1].replace(/\+/g, ' ')); -}; function loadCoreDictionary() { const languages = ['af', 'ar', 'be-by', 'bg-bg', 'bn_bd', 'ca', 'cs', 'da', 'de', 'el', 'en-gb', 'en-us', 'eo', 'es', 'es-419', 'es-ar', 'es_do', 'es-mx', 'fa', 'fi', 'fil', 'fr', 'fr-ca', 'gl', 'gsw', 'he', 'hi-in', 'hr', 'hu', 'id', 'it', 'ja', 'kk', 'ko', 'lt-lt', 'mr', 'ms', 'nb', 'nl', 'pl', 'pr', 'pt', 'pt-br', 'pt-pt', 'ro', 'ru', 'sk', 'sl-si', 'sq', 'sv', 'ta', 'th', 'tr', 'uk', 'ur_pk', 'vi', 'zh-cn', 'zh-hk', 'zh-tw']; diff --git a/src/utils/url.ts b/src/utils/url.ts index fa4489c626..836617e96c 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -1,4 +1,4 @@ -export const getWindowLocationSearch = (win: Window | null | undefined) => { +export const getWindowLocationSearch = (win?: Window | null | undefined) => { let search = (win || window).location.search; if (!search) { @@ -11,3 +11,16 @@ export const getWindowLocationSearch = (win: Window | null | undefined) => { return search || ''; }; + +export const getParameterByName = (name: string, url?: string | null | undefined) => { + name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]'); + const regexS = '[\\?&]' + name + '=([^&#]*)'; + const regex = new RegExp(regexS, 'i'); + const results = regex.exec(url || getWindowLocationSearch()); + + if (results == null) { + return ''; + } + + return decodeURIComponent(results[1].replace(/\+/g, ' ')); +}; From 3412201532615571cfe9e7d2a87b4f4f3508a859 Mon Sep 17 00:00:00 2001 From: Bill Thornton Date: Wed, 6 Apr 2022 17:31:54 -0400 Subject: [PATCH 3/3] Simplify url utils --- src/components/appRouter.js | 3 +-- src/scripts/clientUtils.js | 3 +-- src/scripts/editorsidebar.js | 4 ++-- src/scripts/site.js | 2 -- src/utils/url.ts | 26 ++++---------------------- 5 files changed, 8 insertions(+), 30 deletions(-) diff --git a/src/components/appRouter.js b/src/components/appRouter.js index 4a7224fe5e..ce10d6e93a 100644 --- a/src/components/appRouter.js +++ b/src/components/appRouter.js @@ -12,7 +12,6 @@ import Dashboard from '../scripts/clientUtils'; import ServerConnections from './ServerConnections'; import alert from './alert'; import reactControllerFactory from './reactControllerFactory'; -import { getWindowLocationSearch } from '../utils/url.ts'; class AppRouter { allRoutes = []; @@ -121,7 +120,7 @@ class AppRouter { const regexS = '[\\?&]' + name + '=([^&#]*)'; const regex = new RegExp(regexS, 'i'); - const results = regex.exec(url || getWindowLocationSearch()); + const results = regex.exec(url || window.location.search); if (results == null) { return ''; } else { diff --git a/src/scripts/clientUtils.js b/src/scripts/clientUtils.js index bc9e899490..6d4e3be683 100644 --- a/src/scripts/clientUtils.js +++ b/src/scripts/clientUtils.js @@ -10,7 +10,6 @@ import datetime from '../scripts/datetime'; import DirectoryBrowser from '../components/directorybrowser/directorybrowser'; import dialogHelper from '../components/dialogHelper/dialogHelper'; import itemIdentifier from '../components/itemidentifier/itemidentifier'; -import { getWindowLocationSearch } from '../utils/url.ts'; export function getCurrentUser() { return window.ApiClient.getCurrentUser(false); @@ -113,7 +112,7 @@ export function navigate(url, preserveQueryString) { throw new Error('url cannot be null or empty'); } - const queryString = getWindowLocationSearch(); + const queryString = window.location.search; if (preserveQueryString && queryString) { url += queryString; diff --git a/src/scripts/editorsidebar.js b/src/scripts/editorsidebar.js index 2861999974..220597d64e 100644 --- a/src/scripts/editorsidebar.js +++ b/src/scripts/editorsidebar.js @@ -300,9 +300,9 @@ import { getParameterByName } from '../utils/url.ts'; if (itemId) { return itemId; } - const url = window.location.hash || window.location.href; - return getParameterByName('id', url); + return getParameterByName('id'); } + let nodesToLoad = []; let selectedNodeId; $(document).on('itemsaved', '.metadataEditorPage', function (e, item) { diff --git a/src/scripts/site.js b/src/scripts/site.js index 7f8c4d751f..b346e7dcc7 100644 --- a/src/scripts/site.js +++ b/src/scripts/site.js @@ -41,8 +41,6 @@ import SyncPlayHtmlAudioPlayer from '../components/syncPlay/ui/players/HtmlAudio import { currentSettings } from './settings/userSettings'; import taskButton from './taskbutton'; -// TODO: Move this elsewhere - function loadCoreDictionary() { const languages = ['af', 'ar', 'be-by', 'bg-bg', 'bn_bd', 'ca', 'cs', 'da', 'de', 'el', 'en-gb', 'en-us', 'eo', 'es', 'es-419', 'es-ar', 'es_do', 'es-mx', 'fa', 'fi', 'fil', 'fr', 'fr-ca', 'gl', 'gsw', 'he', 'hi-in', 'hr', 'hu', 'id', 'it', 'ja', 'kk', 'ko', 'lt-lt', 'mr', 'ms', 'nb', 'nl', 'pl', 'pr', 'pt', 'pt-br', 'pt-pt', 'ro', 'ru', 'sk', 'sl-si', 'sq', 'sv', 'ta', 'th', 'tr', 'uk', 'ur_pk', 'vi', 'zh-cn', 'zh-hk', 'zh-tw']; const translations = languages.map(function (language) { diff --git a/src/utils/url.ts b/src/utils/url.ts index 836617e96c..637f22fed8 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -1,26 +1,8 @@ -export const getWindowLocationSearch = (win?: Window | null | undefined) => { - let search = (win || window).location.search; - - if (!search) { - const index = window.location.href.indexOf('?'); - - if (index != -1) { - search = window.location.href.substring(index); - } - } - - return search || ''; -}; - export const getParameterByName = (name: string, url?: string | null | undefined) => { - name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]'); - const regexS = '[\\?&]' + name + '=([^&#]*)'; - const regex = new RegExp(regexS, 'i'); - const results = regex.exec(url || getWindowLocationSearch()); - - if (results == null) { - return ''; + if (!url) { + url = window.location.search; } - return decodeURIComponent(results[1].replace(/\+/g, ' ')); + // eslint-disable-next-line compat/compat + return new URLSearchParams(url).get(name) || ''; };