Skip to content
Permalink
Browse files

Fix Options page scripts

Simplified permissions
Updated fetch function
  • Loading branch information...
Glagan committed May 19, 2019
1 parent bd6c288 commit 0d475ba9adc10270f955f35e0529ce4b642122ee
Showing with 51 additions and 19 deletions.
  1. +2 −3 manifests/manifest.json
  2. +2 −2 options.html
  3. +41 −13 scripts/background.js
  4. +6 −1 scripts/myMangaDex.js
@@ -1,14 +1,13 @@
{
"manifest_version": 2,
"name": "MyMangaDex",
"version": "2.0.8",
"version": "2.0.9",
"author": "Glagan",

"description": "Automatically update your MyAnimeList manga list when reading on MangaDex.",

"permissions": [
"https://myanimelist.net/ownlist/manga/*",
"https://myanimelist.net/mangalist/*",
"https://*.myanimelist.net/*",
"https://*.mangadex.org/*",
"storage"
],
@@ -399,7 +399,7 @@ <h1 class="text-container py-2 px-2"><i class="fas fa-trash"></i> Delete</h1>
<script src="third_party/simpleNotification.min.js"></script>
<script src="scripts/defaultOptions.js"></script>
<script src="third_party/chrome-extension-async.js"></script>
<script src="scripts/minified/sharedFunctions.js"></script>
<script src="scripts/minified/optionsManager.js"></script>
<script src="scripts/sharedFunctions.js"></script>
<script src="scripts/optionsManager.js"></script>
</body>
</html>
@@ -5,21 +5,49 @@ window.browser = (function () {
window.chrome;
})();

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action == "fetch") {
fetch(message.url, message.options || {})
.then(async response => {
return {
url: response.url,
status: response.status,
headers: response.headers,
body: (message.isJson !== undefined && message.isJson) ? await response.json() : await response.text(),
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,
});
}
});
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;
}
})
.then(response => sendResponse(response))
.catch(error => console.error(error));
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));
}
return true;
} else {
console.error("No corresponding action for this message.");
throw new Error("No corresponding action for this message.");
}
});
@@ -55,6 +55,7 @@ class MyMangaDex {
method: "GET",
cache: "no-cache",
credentials: "include",
redirect: "follow",
}
});
this.fetched = true;
@@ -63,7 +64,11 @@ class MyMangaDex {
if (CHROME) {
this.notification(NOTIFY.ERROR, "Not logged in", "Login {{here:https://myanimelist.net/login.php}} on MyAnimeList !", this.myAnimeListImage, true);
} else {
this.notification(NOTIFY.ERROR, "Not logged in", "Login {{here:https://myanimelist.net/login.php}} on MyAnimeList !\r\nIf you see this error while logged in, see {{this issue:https://github.com/Glagan/MyMangaDex/issues/5}} on **Github**.", this.myAnimeListImage, true);
this.notification(NOTIFY.ERROR, "Not logged in",
[
"Login {{here:https://myanimelist.net/login.php}} on MyAnimeList !\r\n",
"If you see this error while logged in, see {{this issue:https://github.com/Glagan/MyMangaDex/issues/5}} on **Github**.",
].join(""), this.myAnimeListImage, true);
}
this.loggedMyAnimeList = false;
} else {

0 comments on commit 0d475ba

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