Skip to content

Commit

Permalink
fix reload
Browse files Browse the repository at this point in the history
  • Loading branch information
Shurelol committed Jun 9, 2023
1 parent 097b4b2 commit df46cbb
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 107 deletions.
4 changes: 2 additions & 2 deletions Emby.CustomCssJS/Configuration/customcssjs.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<title>Custom Css and JavaScript</title>
</head>
<body>
<div id="customcssjsConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-controller="__plugin/customcssjsjs" data-helpurl="https://github.com/Shurelol/Emby.CustomCssJS">
<div id="customcssjsConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-controller="__plugin/customcssjsjs" data-helpurl="https://github.com/Shurelol/Emby.CustomCssJS" data-title="${Custom Css and JavaScrip}">
<div data-role="content">
<div class="content-primary">

<form id="customcssjsConfigurationForm">
<form id="customcssjsConfigurationForm" class="padded-top">
<div>
<button id="customcssjsLoadBtn" is="emby-button" type="submit" class="raised button-submit block">
<span>${Load Scripts}</span>
Expand Down
26 changes: 22 additions & 4 deletions Emby.CustomCssJS/Configuration/customcssjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,28 @@ define([
function renderLoadBtn() {
let loadBtnNode = view.querySelector(`#customcssjsLoadBtn`);
loadBtnNode.addEventListener("click", function () {
try {
window.customcssjsLoadConfiguration();
} catch (e) {
window.location.reload();
let timerCount = Math.max(setInterval(function () {}, 1000), setTimeout(function () {}, 1000));
for (let i = 1; i <= timerCount; i++) {
clearInterval(i);
clearTimeout(i);
if (i === timerCount) {
document.body.remove();
if (typeof MainActivity === "undefined") {
let href = window.location.href;
if (href.match(/autostart=false/i)) {
window.location.href = `index.html?autostart=false`;
} else if (href.match(/autostart=true/i)) {
window.location.href = `index.html?autostart=true`;
} else {
window.location.reload();
}
} else {
MainActivity.exitApp();
setTimeout(function () {
window.open("emby://items", "_blank")
}, 200);
}
}
}
});
}
Expand Down
4 changes: 2 additions & 2 deletions Emby.CustomCssJS/Configuration/customcssjs_provider.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<title>Custom Css and JavaScript</title>
</head>
<body>
<div id="customcssjsConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-controller="__plugin/customcssjs_providerjs" data-helpurl="https://github.com/Shurelol/Emby.CustomCssJS">
<div id="customcssjsConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-controller="__plugin/customcssjs_providerjs" data-helpurl="https://github.com/Shurelol/Emby.CustomCssJS" data-title="${CustomCssJS Provider}">
<div data-role="content">
<div class="content-primary">

<form id="customcssjsConfigurationForm">
<form id="customcssjsConfigurationForm" class="padded-top">

<div class="sectionTitleContainer sectionTitleContainer-cards">
<h2 class="sectionTitle sectionTitle-cards">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<title>Custom Css and JavaScript</title>
</head>
<body>
<div id="customcssjsConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-controller="__plugin/customcssjs_provider_updatejs">
<div id="customcssjsConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-controller="__plugin/customcssjs_provider_updatejs" data-title="${CustomCssJS Provider}">
<div data-role="content">
<div class="content-primary">

<form id="customcssjsConfigurationForm" style="max-width: unset">
<form id="customcssjsConfigurationForm" class="padded-top" style="max-width: unset">
<div>
<button id="customjscssSaveBtn" is="emby-button" type="submit" class="raised button-submit block" style="max-width: 90ch">
<i class="md-icon button-icon button-icon button-icon-left">save</i>
Expand Down
4 changes: 2 additions & 2 deletions Emby.CustomCssJS/Configuration/customcssjs_update.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<title>Custom Css and JavaScript</title>
</head>
<body>
<div id="customcssjsConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-controller="__plugin/customcssjs_updatejs">
<div id="customcssjsConfigurationPage" data-role="page" class="page type-interior pluginConfigurationPage" data-controller="__plugin/customcssjs_updatejs" data-title="${Custom Css and JavaScrip}">
<div data-role="content">
<div class="content-primary">

<form id="customcssjsConfigurationForm" style="max-width: unset">
<form id="customcssjsConfigurationForm" class="padded-top" style="max-width: unset">
<div id="customjscssSaveBtnDiv">
<button id="customjscssSaveBtn" is="emby-button" type="submit" class="raised button-submit block" style="max-width: 90ch">
<i class="md-icon button-icon button-icon button-icon-left">save</i>
Expand Down
40 changes: 21 additions & 19 deletions Emby.CustomCssJS/strings/zh-CN.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
{
"Code error" :"代码错误",
"Content:" :"内容:",
"Custom Css" :"自定义Css",
"Custom JavaScript" :"自定义JavaScript",
"deleted" :"已删除",
"Description:" :"描述:",
"For current Client" :"应用于当前客户端",
"Forced On" :"强制使用",
"From Local" :"来自本地",
"From Server" :"来自服务器",
"Load Scripts" :"载入",
"Name:" :"名称:",
"Name cannot be empty" :"名称不能为空",
"Name already exists" :"名称已存在",
"On" :"开启",
"Please Note Cross-Site Scripting Attacks" :"请注意跨站脚本攻击",
"Provided for All users" :"提供给所有用户",
"saved" :"已保存",
"State:" :"状态:"
"Code error": "代码错误",
"Content:": "内容:",
"Custom Css": "自定义Css",
"Custom Css and JavaScrip": "个性化",
"CustomCssJS Provider": "个性化管理",
"Custom JavaScript": "自定义JavaScript",
"deleted": "已删除",
"Description:": "描述:",
"For current Client": "应用于当前客户端",
"Forced On": "强制使用",
"From Local": "来自本地",
"From Server": "来自服务器",
"Load Scripts": "载入",
"Name:": "名称:",
"Name cannot be empty": "名称不能为空",
"Name already exists": "名称已存在",
"On": "开启",
"Please Note Cross-Site Scripting Attacks": "请注意跨站脚本攻击",
"Provided for All users": "提供给所有用户",
"saved": "已保存",
"State:": "状态:"
}
115 changes: 39 additions & 76 deletions src/CustomCssJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,23 @@ define([

function loadCss(name, content, source) {
try {
let customcssjsCount = window.customcssjsCount + 1;
let oldStyles = Array.from(document.getElementsByTagName("style"));
let s = document.createElement("style");
s.type = "text/css";
s.innerHTML = content;
document.head.appendChild(s);
let newStyles = Array.from(document.getElementsByTagName("style"));
// record style
let addedStyles = newStyles.filter(item => !oldStyles.includes(item));
addedStyles.forEach(item => item.id = `customcssjsNode_${customcssjsCount}`);
window.customcssjsCount = customcssjsCount;
console.warn(`load CustomCss from ${source}: ${name} with style Node customcssjsNode_${customcssjsCount}`);
console.warn(`load CustomCss from ${source}: ${name}`);
} catch (e) {
console.error(`load CustomCss from ${source} ${name} error: ${e}`);
}
}

function loadJS(name, content, source) {
try {
let customcssjsCount = window.customcssjsCount;
let oldScripts = Array.from(document.getElementsByTagName("script"));
let oldStyles = Array.from(document.getElementsByTagName("style"));
let startTimer = setInterval(function () { }, 10000);
let s = document.createElement("script");
s.type = "text/javascript";
s.innerHTML = content;
document.body.appendChild(s);
let endTimer = setInterval(function () { }, 10000);
let newScripts = Array.from(document.getElementsByTagName("script"));
let newStyles = Array.from(document.getElementsByTagName("style"));
let message = `load CustomJS from ${source}: ${name}`
// record script and style Nodes
let addedScripts = newScripts.filter(item => !oldScripts.includes(item));
let addedStyles = newStyles.filter(item => !oldStyles.includes(item));
for (let i = 0; i < addedScripts.length; i++) {
let index = customcssjsCount + i + 1;
addedScripts[i].id = `customcssjsNode_${index}`;
message += ` with script Node customcssjsNode_${index}`;
}
customcssjsCount += addedScripts.length;
for (let i = 0; i < addedStyles.length; i++) {
let index = customcssjsCount + i + 1;
addedStyles[i].id = `customcssjsNode_${index}`;
message += ` with style Node customcssjsNode_${index}`;
}
window.customcssjsCount = customcssjsCount + addedStyles.length;
// record timer
let timerCount = endTimer - startTimer;
let timerList = [];
if (timerCount > 1) {
for (let i = startTimer + 1; i < endTimer; i++) {
timerList.push(i);
}
window.customcssjsTimers = window.customcssjsTimers.concat(timerList);
message += ` with Timer ${timerList}`;
}
clearInterval(startTimer);
clearInterval(endTimer);
console.warn(message);
console.warn(`load CustomJS from ${source}: ${name}`);
} catch (e) {
console.error(`load CustomJS from ${source} ${name} error: ${e}`);
}
Expand Down Expand Up @@ -116,32 +74,8 @@ define([
return [customServer, customLocal];
}

function clearCustom() {
// clear timer
let customTimers = window.customcssjsTimers;
if (customTimers) {
for (let timer of customTimers) {
clearInterval(timer);
}
console.warn(`clear Timers: ${customTimers}`);
}
window.customcssjsTimers = [];
// clear Node
let customcssjsCount = window.customcssjsCount;
if (customcssjsCount) {
for (let i = 1; i <= customcssjsCount; i++) {
let node = document.getElementById(`customcssjsNode_${i}`);
if (node) {
node.remove();
}
}
console.warn(`clear Nodes: ${customcssjsCount}`);
}
window.customcssjsCount = 0;
}

function loadConfiguration() {
clearCustom();
window.serverId = ApiClient.serverId();
ApiClient.getPluginConfiguration(pluginUniqueId).then(function (config) {
let [customjsServer, customjsLocal] = getCustom("js", config);
let [customcssServer, customcssLocal] = getCustom("css", config);
Expand All @@ -152,15 +86,44 @@ define([
});
}

let pluginUniqueId = "98F76C3D-695F-4082-9220-AD5752E0859D";

window.customcssjsLoadConfiguration = function () {
loadConfiguration();
function loadCustomCssJS() {
return function () {
let serverId = ApiClient.serverId();
switch (window.serverId) {
case serverId:
return;
case undefined:
loadConfiguration();
break;
default:
let timerCount = Math.max(setInterval(function () {}, 1000), setTimeout(function () {}, 1000));
for (let i = 1; i <= timerCount; i++) {
clearInterval(i);
clearTimeout(i);
if (i === timerCount) {
document.body.remove();
if (typeof MainActivity === "undefined") {
let href = window.location.href;
if (href.match(/autostart=false/i)) {
window.location.href = `index.html?autostart=false`;
} else if (href.match(/autostart=true/i)) {
window.location.href = `index.html?autostart=true`;
} else {
window.location.reload();
}
} else {
MainActivity.exitApp();
window.open("emby://items", "_blank");
}
}
}
break;
}
}
}
let pluginUniqueId = "98F76C3D-695F-4082-9220-AD5752E0859D";

events.on(connectionManager, "localusersignedin", function () {
loadConfiguration();
});
events.on(connectionManager, "localusersignedin", loadCustomCssJS());

}
});
Binary file modified src/Emby.CustomCssJS.dll
Binary file not shown.

0 comments on commit df46cbb

Please sign in to comment.