Skip to content
This repository has been archived by the owner on Feb 28, 2022. It is now read-only.

Commit

Permalink
Fix Options page scripts
Browse files Browse the repository at this point in the history
Simplified permissions
Updated fetch function
  • Loading branch information
Glagan committed May 19, 2019
1 parent bd6c288 commit 0d475ba
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
5 changes: 2 additions & 3 deletions manifests/manifest.json
@@ -1,14 +1,13 @@
{ {
"manifest_version": 2, "manifest_version": 2,
"name": "MyMangaDex", "name": "MyMangaDex",
"version": "2.0.8", "version": "2.0.9",
"author": "Glagan", "author": "Glagan",


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


"permissions": [ "permissions": [
"https://myanimelist.net/ownlist/manga/*", "https://*.myanimelist.net/*",
"https://myanimelist.net/mangalist/*",
"https://*.mangadex.org/*", "https://*.mangadex.org/*",
"storage" "storage"
], ],
Expand Down
4 changes: 2 additions & 2 deletions options.html
Expand Up @@ -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="third_party/simpleNotification.min.js"></script>
<script src="scripts/defaultOptions.js"></script> <script src="scripts/defaultOptions.js"></script>
<script src="third_party/chrome-extension-async.js"></script> <script src="third_party/chrome-extension-async.js"></script>
<script src="scripts/minified/sharedFunctions.js"></script> <script src="scripts/sharedFunctions.js"></script>
<script src="scripts/minified/optionsManager.js"></script> <script src="scripts/optionsManager.js"></script>
</body> </body>
</html> </html>
54 changes: 41 additions & 13 deletions scripts/background.js
Expand Up @@ -5,21 +5,49 @@ window.browser = (function () {
window.chrome; window.chrome;
})(); })();


chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action == "fetch") { if ("action" in message && message.action == "fetch") {
fetch(message.url, message.options || {}) // Options
.then(async response => { message.isJson = ("isJson" in message && message.isJson);
return { message.options = Object.assign({}, {
url: response.url, method: "GET", body: null, credentials: "same-origin", headers: {}
status: response.status, }, message.options || {});
headers: response.headers, // XMLHttpRequest or Fetch
body: (message.isJson !== undefined && message.isJson) ? await response.json() : await response.text(), 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.send(message.options.body);
.then(response => sendResponse(response)) } else {
.catch(error => console.error(error)); 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; return true;
} else { } else {
console.error("No corresponding action for this message."); throw new Error("No corresponding action for this message.");
} }
}); });
7 changes: 6 additions & 1 deletion scripts/myMangaDex.js
Expand Up @@ -55,6 +55,7 @@ class MyMangaDex {
method: "GET", method: "GET",
cache: "no-cache", cache: "no-cache",
credentials: "include", credentials: "include",
redirect: "follow",
} }
}); });
this.fetched = true; this.fetched = true;
Expand All @@ -63,7 +64,11 @@ class MyMangaDex {
if (CHROME) { if (CHROME) {
this.notification(NOTIFY.ERROR, "Not logged in", "Login {{here:https://myanimelist.net/login.php}} on MyAnimeList !", this.myAnimeListImage, true); this.notification(NOTIFY.ERROR, "Not logged in", "Login {{here:https://myanimelist.net/login.php}} on MyAnimeList !", this.myAnimeListImage, true);
} else { } 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; this.loggedMyAnimeList = false;
} else { } else {
Expand Down

0 comments on commit 0d475ba

Please sign in to comment.