Skip to content

Commit

Permalink
Merge pull request #24 from lucka-me/display-errors-on-extension
Browse files Browse the repository at this point in the history
Present errors in Safari popup and Intel webpage
  • Loading branch information
lucka-me authored Apr 13, 2024
2 parents 160c9ab + 62b68ae commit 7e2891b
Show file tree
Hide file tree
Showing 10 changed files with 361 additions and 46 deletions.
4 changes: 3 additions & 1 deletion IntelStack.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
8475D1932AD8333800D71F59 /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 8475D1922AD8333800D71F59 /* Localizable.xcstrings */; };
8475D1962ADAD19100D71F59 /* images in Resources */ = {isa = PBXBuildFile; fileRef = 8475D1952ADAD19100D71F59 /* images */; };
84A917EF2B58EDCF009D53E8 /* UserScriptMetadataDecoderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84A917EE2B58EDCF009D53E8 /* UserScriptMetadataDecoderTests.swift */; };
84BD9FAF2BCA1CA100771DEA /* Localizable.xcstrings in Resources */ = {isa = PBXBuildFile; fileRef = 8475D1922AD8333800D71F59 /* Localizable.xcstrings */; };
84D068D92ABDB2EB00EC6E7E /* ModelContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84D068D82ABDB2EB00EC6E7E /* ModelContainer.swift */; };
84D068DA2ABDB2EB00EC6E7E /* ModelContainer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84D068D82ABDB2EB00EC6E7E /* ModelContainer.swift */; };
84D068DD2ABDB4D900EC6E7E /* FileConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84D068DC2ABDB4D900EC6E7E /* FileConstants.swift */; };
Expand Down Expand Up @@ -218,7 +219,6 @@
842318892AB7053A000A0A09 /* Assets.xcassets */,
84D068DB2ABDB44000EC6E7E /* Data */,
84D8E96C2AB87DE8000234A9 /* Extensions */,
8475D1912AD832EF00D71F59 /* Localization */,
840F49AF2AB7073A00012C44 /* Views */,
);
path = Shared;
Expand Down Expand Up @@ -408,6 +408,7 @@
children = (
84D8E9572AB7299A000234A9 /* Data */,
84D8E95F2AB73CAD000234A9 /* Extensions */,
8475D1912AD832EF00D71F59 /* Localization */,
84D1F62B2B5A3FE200D260EB /* macOS */,
);
path = Shared;
Expand Down Expand Up @@ -562,6 +563,7 @@
840F49BC2AB70C3600012C44 /* _locales in Resources */,
8475D1962ADAD19100D71F59 /* images in Resources */,
842F9D0A2ACE5A35002E750B /* popup in Resources */,
84BD9FAF2BCA1CA100771DEA /* Localizable.xcstrings in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
1 change: 1 addition & 0 deletions Shared/Data/ScriptManager+ExternalScripts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extension ScriptManager {
try context.save()
}

@discardableResult
static func sync(with context: ModelContext = .init(.default)) throws -> URL? {
guard let externalURL = UserDefaults.shared.externalScriptsBookmarkURL else {
try context.delete(model: Plugin.self, where: Plugin.externalPredicate)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -977,6 +977,118 @@
}
}
},
"SafariWebExtensionHandler.Error.MainScriptNotExists" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "IITC main script does not exists, please open the Intel Stack app, follow the instruction to download main script and internal plugins."
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "IITC主脚本不存在,请打开Intel Stack应用,跟随介绍下载主脚本和内部插件。"
}
}
}
},
"SafariWebExtensionHandler.Error.NoMessage" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "No message in the request."
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "请求中没有消息。"
}
}
}
},
"SafariWebExtensionHandler.Error.PluginNotFound" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "It seems the plugin does not exists."
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "这个插件似乎不存在。"
}
}
}
},
"SafariWebExtensionHandler.Error.RequestContentMissing %@" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "\"%@\" is missing in the request."
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "请求中缺少“%@”。"
}
}
}
},
"SafariWebExtensionHandler.Error.UnableToLoadPlugin %@ %@" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Unable to load the plugin %1$@: %2$@"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "无法加载插件“%1$@”:%2$@"
}
}
}
},
"SafariWebExtensionHandler.Error.UnknownMethod %@" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Method of the request is unknown: %@."
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "请求的方法未知:%@。"
}
}
}
},
"SafariWebExtensionHandler.NoPlugin" : {
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "No plugin exists, please open the Intel Stack app, follow the instruction to download main script and internal plugins."
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "没有插件,请打开Intel Stack应用,跟随介绍下载主脚本和内部插件。"
}
}
}
},
"SettingsView.About.IITCWebsite" : {
"localizations" : {
"en" : {
Expand Down
6 changes: 6 additions & 0 deletions Web Extension/Shared/Resources/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
"extension_description": {
"message": "Manage IITC script and plugin"
},
"popup_alert_cancel": {
"message": "OK"
},
"popup_scripts_enabled": {
"message": "Enabled"
},
"error_invalid_response": {
"message": "The response is unavailable."
}
}
6 changes: 6 additions & 0 deletions Web Extension/Shared/Resources/_locales/zh/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
"extension_description": {
"message": "管理IITC脚本和插件"
},
"popup_alert_cancel": {
"message": "OK"
},
"popup_scripts_enabled": {
"message": "启用"
},
"error_invalid_response": {
"message": "响应不可用。"
}
}
14 changes: 12 additions & 2 deletions Web Extension/Shared/Resources/content/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
async function execute() {
const response = await browser.runtime.sendMessage({ method: "getInjectionData" });
if (!response || !response.scripts) {
console.error(`Invalid response: ${response}`);
if (!response) {
alert(browser.i18n.getMessage("error_invalid_response"));
return;
}

if (response.error) {
alert(response.error);
return;
}

if (response.scripts.length === 0) {
return;
}
Expand All @@ -20,6 +26,10 @@ async function execute() {
node.textContent = script;
document.head.appendChild(node);
}

if (response.warnings) {
alert(response.warnings.join("\n"));
}
}

execute();
31 changes: 31 additions & 0 deletions Web Extension/Shared/Resources/popup/popup.css
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,37 @@ main {
scroll-behavior: smooth;
}

main.error {
color: gray;
justify-content: center;
text-align: center;
}

dialog {
border: none;
border-radius: 0.6em;
padding: 0;
}

dialog > p {
margin: 0;
padding: 1.2em;
text-align: center;
}

dialog > form > button {
background: none;
border: none;
border-top: solid 1px gray;
padding: 0.6em;
width: 100%;
}

dialog::backdrop {
background-color: gray;
opacity: 0.5;
}

section {
display: flex;
flex-flow: column nowrap;
Expand Down
89 changes: 78 additions & 11 deletions Web Extension/Shared/Resources/popup/popup.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
function presentAlert(content) {
const dialogNode = document.createElement("dialog");

const contentNode = document.createElement("p");
contentNode.textContent = content;
dialogNode.appendChild(contentNode);

{
const actionsForm = document.createElement("form");
actionsForm.method = "dialog";

const closeButton = document.createElement("button");
dialogNode.autofocus = true;
closeButton.textContent = browser.i18n.getMessage("popup_alert_cancel");
actionsForm.appendChild(closeButton);

dialogNode.appendChild(actionsForm);
}

document.body.appendChild(dialogNode);

dialogNode.addEventListener("close", () => { document.body.removeChild(dialogNode); });

dialogNode.showModal();
}

function buildScriptsSection(scriptsEnabled, mainNode) {
const sectionNode = document.createElement("section");

Expand Down Expand Up @@ -26,13 +52,26 @@ function buildScriptsSection(scriptsEnabled, mainNode) {
toggleNode.className = "toggle";
toggleNode.checked = scriptsEnabled;
toggleNode.addEventListener("change", async (event) => {
const enable = event.currentTarget.checked;
if (event.target === null) {
return;
}
const enable = event.target.checked;
const response = await browser.runtime.sendMessage({
method: "setScriptsEnabled",
arguments: { enable }
});
if (!response || !response.succeed) {
event.currentTarget.checked = !checked;
if (!response) {
presentAlert(browser.i18n.getMessage("error_invalid_response"));
event.target.checked = !enable;
return;
}
if (response.error) {
presentAlert(response.error);
event.target.checked = !enable;
return;
}
if (!response.succeed) {
event.target.checked = !enable;
}
});
rowNode.appendChild(toggleNode);
Expand Down Expand Up @@ -86,13 +125,26 @@ function buildSection(category, mainNode) {
toggleNode.className = "toggle";
toggleNode.checked = plugin.enabled;
toggleNode.addEventListener("change", async (event) => {
const enable = event.currentTarget.checked;
if (event.target === null) {
return;
}
const enable = event.target.checked;
const response = await browser.runtime.sendMessage({
method: "setPluginEnabled",
arguments: { uuid, enable }
});
if (!response || !response.succeed) {
event.currentTarget.checked = !checked;
if (!response) {
presentAlert(browser.i18n.getMessage("error_invalid_response"));
event.target.checked = !enable;
return;
}
if (response.error) {
presentAlert(response.error);
event.target.checked = !enable;
return;
}
if (!response.succeed) {
event.target.checked = !enable;
}
});
rowNode.appendChild(toggleNode);
Expand All @@ -104,26 +156,41 @@ function buildSection(category, mainNode) {
mainNode.appendChild(sectionNode);
}

function setupWithError(error, mainNode) {
mainNode.classList.add("error");

const textNode = document.createElement("p");
textNode.textContent = error;

mainNode.appendChild(textNode);
}

async function setupContents() {
const response = await browser.runtime.sendMessage({ method: "getPopupContentData" });
if (!response || !response.categories) {
console.error(`Invalid response: ${response}`);

const mainNode = document.createElement("main");
document.body.appendChild(mainNode);

if (!response) {
setupWithError(browser.i18n.getMessage("error_invalid_response"), mainNode);
return;
}

if (response.platform === "macOS") {
document.body.classList.add("desktop");
}

const mainNode = document.createElement("main");
if (response.error) {
console.log(response);
setupWithError(response.error, mainNode);
return;
}

buildScriptsSection(response.scriptsEnabled, mainNode);

for (const category of response.categories) {
buildSection(category, mainNode);
}

document.body.appendChild(mainNode);
}

setupContents();
Loading

0 comments on commit 7e2891b

Please sign in to comment.