Skip to content
Permalink
Browse files

Always open options in a new tab

Rewrite of the update notifications
Open Options button in update and first time notifications
Version bump to 2.2
  • Loading branch information...
Glagan committed Aug 8, 2019
1 parent 0139198 commit 94e87bcd95be30a8dd9e1fdcc06a3fa3a87f31a2
Showing with 120 additions and 107 deletions.
  1. +0 −2 manifests/chrome.json
  2. +0 −4 manifests/firefox.json
  3. +6 −1 manifests/manifest.json
  4. +44 −38 scripts/background.js
  5. +2 −2 scripts/defaultOptions.js
  6. +68 −60 scripts/sharedFunctions.js
@@ -1,6 +1,4 @@
{
"options_page": "options.html",

"content_scripts": [{
"js": [
"third_party/simpleNotification.min.js",
@@ -10,10 +10,6 @@
}
},

"options_ui": {
"page": "options.html"
},

"content_scripts": [{
"js": [
"third_party/simpleNotification.min.js",
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "MyMangaDex",
"version": "2.1.6",
"version": "2.2",
"author": "Glagan",

"description": "Automatically update your MyAnimeList manga list when reading on MangaDex.",
@@ -22,6 +22,11 @@
"scripts": ["scripts/background.js"]
},

"options_ui": {
"page": "options.html",
"open_in_tab": true
},

"content_scripts": [{
"matches": [
"https://*.mangadex.org/follows",
@@ -6,48 +6,54 @@ window.browser = (function () {
})();

browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if ("action" in message && message.action == "fetch") {
// Options
message.isJson = ("isJson" in message && message.isJson);
message.options = Object.assign({}, {
method: "GET", body: null, credentials: "same-origin", headers: {}
}, message.options || {});
// XMLHttpRequest or Fetch
if ("with" in message && message.with == "xhr") {
let xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", () => {
if (xhr.readyState == 4) {
sendResponse({
url: xhr.responseURL,
status: xhr.status,
headers: xhr.getAllResponseHeaders(),
body: (message.isJson) ? JSON.parse(xhr.responseText) : xhr.responseText,
});
if ("action" in message) {
if (message.action == "fetch") {
// Options
message.isJson = ("isJson" in message && message.isJson);
message.options = Object.assign({}, {
method: "GET", body: null, credentials: "same-origin", headers: {}
}, message.options || {});
// XMLHttpRequest or Fetch
if ("with" in message && message.with == "xhr") {
let xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", () => {
if (xhr.readyState == 4) {
sendResponse({
url: xhr.responseURL,
status: xhr.status,
headers: xhr.getAllResponseHeaders(),
body: (message.isJson) ? JSON.parse(xhr.responseText) : xhr.responseText,
});
}
});
xhr.open(message.options.method, message.url, true);
Object.keys(message.options.headers).forEach(name => {
xhr.setRequestHeader(name, message.options.headers[name]);
});
if (message.options.credentials == "include") {
xhr.withCredentials = true;
}
});
xhr.open(message.options.method, message.url, true);
Object.keys(message.options.headers).forEach(name => {
xhr.setRequestHeader(name, message.options.headers[name]);
});
if (message.options.credentials == "include") {
xhr.withCredentials = true;
xhr.send(message.options.body);
} else {
fetch(message.url, message.options)
.then(async response => {
return {
url: response.url,
status: response.status,
headers: response.headers,
body: (message.isJson) ? await response.json() : await response.text(),
};
})
.then(response => sendResponse(response))
.catch(error => console.error(error));
}
xhr.send(message.options.body);
return true;
} else if (message.action == "openOptions") {
return browser.runtime.openOptionsPage();
} else {
fetch(message.url, message.options)
.then(async response => {
return {
url: response.url,
status: response.status,
headers: response.headers,
body: (message.isJson) ? await response.json() : await response.text(),
};
})
.then(response => sendResponse(response))
.catch(error => console.error(error));
throw new Error("No valid action for this message.");
}
return true;
} else {
throw new Error("No corresponding action for this message.");
throw new Error("No action for this message.");
}
});
@@ -36,6 +36,6 @@ let defaultOptions = {
username: "",
isLoggedIn: false,
token: "",
version: 2.1,
subVersion: 6
version: 2.2,
subVersion: 0
};
@@ -66,50 +66,43 @@ async function loadOptions() {
// If there is nothing in the storage, default options
if (data === undefined) {
SimpleNotification.info({
title: "First time using MyMangaDex",
title: "First Time",
image: mmdImage,
text: "It looks like it is your first time using MyMangaDex, for any help go on {{github|https://github.com/Glagan/MyMangaDex}}, and don't forget to look at the settings !"
text: "It looks like it is your first time using MyMangaDex, for any help go on {{github|https://github.com/Glagan/MyMangaDex}}.\nDon't forget to look at the **Options** !",
buttons: {
value: "Open Options",
type: "message",
onClick: (n) => {
n.close();
browser.runtime.sendMessage({ action: "openOptions" });
}
}
}, { sticky: true, position: "bottom-left" });

await storageSet("options", defaultOptions);
return JSON.parse(JSON.stringify(defaultOptions));
} else {
// Version updates
if (data.version < defaultOptions.version) {
// I saw people using using 1.6.3 ??? So just in case...
if (data.version < 1.7) {
data.auto_md_list = false;
}

if (data.version < 1.8) {
// Options object rework
data = {
lastReadColor: data.colors.last_read,
lowerChaptersColor: data.colors.lower_chapter,
lastOpenColors: data.colors.last_open,
openedChaptersColor: data.colors.opened_chapters,
hideLowerChapters: data.hide_lower,
saveOnlyHigher: data.last_only_higher,
saveAllOpened: data.save_all_opened,
maxChapterSaved: data.max_save_opened,
updateMDList: data.auto_md_list,
showTooltips: true // New options to default
};
}

if (data.version < 1.9) {
data.highlightChapters = defaultOptions.highlightChapters;
data.showNotifications = defaultOptions.showNotifications;
data.showErrors = defaultOptions.showErrors;
data.version = 1.9;

SimpleNotification.info({
title: "MyMangaDex as been updated to 1.9",
image: mmdImage,
text: "You can see the changelog on {{github|https://github.com/Glagan/MyMangaDex}}, new options have been added, you should check them out !",
}, { sticky: true, position: "bottom-left" });
// Subversion updates
if (data.version == defaultOptions.version && data.subVersion != defaultOptions.subVersion) {
if (data.version == 2.1) {
// Fix for history in 2.1.1
if (data.subVersion == undefined || data.subVersion < 1) {
data.subVersion = 1;
if ((await storageGet("history")) == undefined) {
await storageSet("history", { list: [] });
}
}
if (data.subVersion < 5) {
data.saveOnlyNext = false; //.4
data.updateOnlyInList = false; // .5
}
data.subVersion = 5;
}
await storageSet("options", data);
}

// Version updates
if (data.version < defaultOptions.version) {
if (data.version < 2.0) {
data.onlineSave = defaultOptions.onlineSave; // New options to default
data.onlineURL = defaultOptions.onlineURL;
@@ -120,9 +113,17 @@ async function loadOptions() {
data.version = 2.0;

SimpleNotification.info({
title: "MyMangaDex as been updated to 2.0",
title: "MyMangaDex Update",
image: mmdImage,
text: "Online Save as been added, if you wish to use it you need to manually enable it. You can see the changelog on {{github|https://github.com/Glagan/MyMangaDex}}.",
text: "Online Save as been added, if you wish to use it you need to manually enable it.\nYou can see the changelog on {{github|https://github.com/Glagan/MyMangaDex}}.",
buttons: {
value: "Open Options",
type: "message",
onClick: (n) => {
n.close();
browser.runtime.sendMessage({ action: "openOptions" });
}
}
}, { sticky: true, position: "bottom-left" });
}

@@ -132,9 +133,17 @@ async function loadOptions() {
data.version = 2.1;

SimpleNotification.info({
title: "MyMangaDex as been updated to 2.1",
title: "MyMangaDex Update",
image: mmdImage,
text: "Fix for new **MyAnimeList** entries and the {{history|https://mangadex.org/history}} page now display more than your last 10 chapters if you want.\nYou can see the changelog on {{github:https://github.com/Glagan/MyMangaDex}}.",
buttons: {
value: "Open Options",
type: "message",
onClick: (n) => {
n.close();
browser.runtime.sendMessage({ action: "openOptions" });
}
}
}, { sticky: true, position: "bottom-left" });
SimpleNotification.info({
title: "Update Information",
@@ -143,31 +152,30 @@ async function loadOptions() {
}, { sticky: true, position: "bottom-left" });
}

await storageSet("options", data);
await storageSet("history", { list: [] });
}
if (data.version < 2.2) {
data.confirmChapter = defaultOptions.confirmChapter;
data.updateOnlyInList = defaultOptions.updateOnlyInList;
data.showNoMal = undefined;
data.version = 2.2;
data.subVersion = 0;

// Subversion updates
if (data.version == defaultOptions.version && data.subVersion != defaultOptions.subVersion) {
if (data.version == 2.1) {
// Fix for history in 2.1.1
if (data.subVersion == undefined || data.subVersion < 1) {
data.subVersion = 1;
if ((await storageGet("history")) == undefined) {
await storageSet("history", { list: [] });
SimpleNotification.info({
title: "MyMangaDex Update",
image: mmdImage,
text: "You can now **Confirm Updates** that would be rejected (lower chapter) and update a title only if it's in your **MangaDex** list.\nYou can enable these options... in the **options**.\nIf you have a problem, don't forget: **Github**, **Reddit** or **Discord**.",
buttons: {
value: "Open Options",
type: "message",
onClick: (n) => {
n.close();
browser.runtime.sendMessage({ action: "openOptions" });
}
}
}

if (data.subVersion < 6) {
data.saveOnlyNext = false; //.4
data.updateOnlyInList = false; // .5
data.confirmChapter = true;
data.showNoMal = undefined;
}

data.subVersion = defaultOptions.subVersion;
}, { sticky: true, position: "bottom-left" });
}

await storageSet("options", data);
await storageSet("history", { list: [] });
}
return data;
}

0 comments on commit 94e87bc

Please sign in to comment.
You can’t perform that action at this time.