diff --git a/.gitignore b/.gitignore index ea031ba..185cdc2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ */web-ext-artifacts/* */node_modules/* Chrome.crx -Chrome.pem \ No newline at end of file +Chrome.pem +Chrome/_metadata diff --git a/Chrome/background.js b/Chrome/background.js index be18561..515f99b 100644 --- a/Chrome/background.js +++ b/Chrome/background.js @@ -1,5 +1,6 @@ chrome.runtime.onInstalled.addListener(async () => { - for (const cs of chrome.runtime.getManifest().content_scripts) { + + for (const cs of chrome.runtime.getManifest().content_scripts) { for (const tab of await chrome.tabs.query({ url: cs.matches })) { chrome.scripting.executeScript({ target: { tabId: tab.id }, @@ -7,4 +8,30 @@ chrome.runtime.onInstalled.addListener(async () => { }); } } -}); \ No newline at end of file +}); + +chrome.storage.local.onChanged.addListener(async (chan) => { + if (chan.avoidPdfDownload) { + let enabled = chan.avoidPdfDownload.newValue; + await chrome.declarativeNetRequest.updateStaticRules({ + disableRuleIds: enabled ? [] : [1], + enableRuleIds: enabled ? [1] : [], + rulesetId: "ruleset_1" + }) + console.log("updated") + } +}) + +async function pdfSetup() { + let storage = await chrome.storage.local.get('avoidPdfDownload'); + if (Object.keys(storage).includes("avoidPdfDownload") && !storage.avoidPdfDownload) { //If avoidPdfDownload is not in keys -> default setting -> should not disable. + console.log("Disabling pdfDownload because of settings.") + await chrome.declarativeNetRequest.updateStaticRules({ + disableRuleIds: [1], + enableRuleIds: [], + rulesetId: "ruleset_1" + }) + } +} + +pdfSetup(); diff --git a/Chrome/index.js b/Chrome/index.js index 3ddf9df..079c19f 100644 --- a/Chrome/index.js +++ b/Chrome/index.js @@ -1,5 +1,5 @@ var keepAliveIntervalId; -var keepAliveEnabled = localStorage.getItem("keepAlive") === 'true'; +var keepAliveEnabled = localStorage.getItem("keepAlive") !== 'false'; var sesskey = ''; var moodleURLBase = '' @@ -9,6 +9,7 @@ chrome.storage.onChanged.addListener((changes, area) => { if (keepAliveEnabled) { runKeepAlive(); } else { + console.log("clear") clearInterval(keepAliveIntervalId); } localStorage.setItem("keepAlive", keepAliveEnabled); @@ -50,6 +51,7 @@ function setupSesskey(callback) { } function runKeepAlive() { + console.log("ran") if(moodleURLBase === '') { return; } @@ -77,4 +79,4 @@ function runKeepAlive() { } }); }, 1 * 60 * 60 * 1000); -} \ No newline at end of file +} diff --git a/Chrome/manifest.json b/Chrome/manifest.json index 0746d5f..980e784 100644 --- a/Chrome/manifest.json +++ b/Chrome/manifest.json @@ -1,43 +1,59 @@ { - "manifest_version": 3, - "name": "MoodleCustomizer", - "version": "1.1.1", - "author": "Hokkaydo", - "description": "Improve your Moodle experience.", - "icons": { - "16": "icons/logo.png" - }, - "permissions": [ - "activeTab", - "tabs", - "webNavigation", - "scripting", - "storage" - ], - "host_permissions": [ - "" - ], - "content_scripts": [ - { - "matches": [ - "" - ], - "js": [ - "index.js" - ] - } - ], - "action": { - "default_title": "On/Off", - "default_popup": "popup/options.html" - }, - "background": { - "service_worker": "background.js" - }, - "web_accessible_resources":[ - { - "resources": ["retrieveVariables.js"], - "matches": [""] - } + "manifest_version": 3, + "name": "MoodleCustomizer", + "version": "1.1.2", + "author": "Hokkaydo", + "description": "Improve your Moodle experience.", + "icons": { + "16": "icons/logo.png" + }, + "permissions": [ + "activeTab", + "tabs", + "webNavigation", + "scripting", + "storage", + "declarativeNetRequest", + "declarativeNetRequestWithHostAccess" + ], + "host_permissions": [ + "" + ], + "content_scripts": [ + { + "matches": [ + "http://*/*", + "https://*/*" + ], + "js": [ + "index.js" + ] + } + ], + "action": { + "default_title": "On/Off", + "default_popup": "popup/options.html" + }, + "background": { + "service_worker": "background.js" + }, + "declarative_net_request": { + "rule_resources": [ + { + "id": "ruleset_1", + "enabled": true, + "path": "rules.json" + } ] + }, + "web_accessible_resources": [ + { + "resources": [ + "retrieveVariables.js" + ], + "matches": [ + "" + ] + } + ] } \ No newline at end of file diff --git a/Chrome/popup/options.css b/Chrome/popup/options.css index 85271ef..185bb17 100644 --- a/Chrome/popup/options.css +++ b/Chrome/popup/options.css @@ -1,19 +1,52 @@ -.list { - width: 240px; - background-color: white; - display: grid; - grid-template-columns: repeat(2, 1fr); - font-size: small; - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; +body { + width: 300px; + height: 200px; + display: flex; + flex-direction: column; + align-items: center; } -.onoff > img { - width: 70%; + + +.control { + display: flex; + align-items: center; +} +.control > p { + margin-right: 10px; + +} + +input[type="checkbox"] { + position: relative; + appearance: none; + width: 37.5px; + height: 18.75px; + background: #ccc; + border-radius: 50px; + box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2); + cursor: pointer; + transition: 0.4s; } -.list > .onoff { - display: flex; - justify-content: center; - align-items: center; - width: 50px; -} \ No newline at end of file +input:checked[type="checkbox"] { + background: #7da6ff; +} + +input[type="checkbox"]::after { + position: absolute; + content: ""; + width: 18.75px; + height: 18.75px; + top: 0; + left: 0; + background: #fff; + border-radius: 50%; + box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); + transform: scale(1.1); + transition: 0.4s; +} + +input:checked[type="checkbox"]::after { + left: 50%; +} diff --git a/Chrome/popup/options.html b/Chrome/popup/options.html index 6de283f..dbab794 100644 --- a/Chrome/popup/options.html +++ b/Chrome/popup/options.html @@ -1,16 +1,29 @@ + - - - - - Document - + + + + + Document + + -
+

Settings:

+
+
+

Keep Alive:

+ +
+
+

Avoid PDF downloads:

+
- +
+ + + \ No newline at end of file diff --git a/Chrome/popup/options.js b/Chrome/popup/options.js index f3e682a..4bd09e1 100644 --- a/Chrome/popup/options.js +++ b/Chrome/popup/options.js @@ -1,18 +1,19 @@ -const toggleKeepAlive = document.createElement("p"); -const keepAliveOnOff = document.createElement("img"); -var keepAlive = localStorage.getItem("keepAlive") === 'true'; -keepAliveOnOff.src = chrome.runtime.getURL("icons/" + (keepAlive ? "on.png" : "off.png")); -toggleKeepAlive.textContent = "KeepAlive : "; -toggleKeepAlive.addEventListener("click", (_event) => { - const keepAlive = localStorage.getItem("keepAlive") === 'true'; - localStorage.setItem("keepAlive", !keepAlive); - toggleKeepAlive.textContent = "KeepAlive : "; - keepAliveOnOff.src = chrome.runtime.getURL("icons/" + (!keepAlive ? 'on.png': 'off.png')); - chrome.storage.local.set({keepAlive: !keepAlive}) -}); -const list = document.getElementsByClassName("list")[0] -const onoff = document.createElement("div"); -onoff.classList.add("onoff"); -onoff.appendChild(keepAliveOnOff); -list.appendChild(toggleKeepAlive); -list.appendChild(onoff); \ No newline at end of file +let settings = {}; + +for (input of document.getElementsByTagName("input")) { + if (!input.type == "checkbox") { + console.log(input); + continue; + } + let item = input.dataset.item; + input.checked = localStorage.getItem(item) !== 'false'; + settings[item] = input.checked; + input.addEventListener("click", (e) => { + settings[item] = e.target.checked; + chrome.storage.local.set(settings); + localStorage.setItem(item, e.target.checked); + }) +} + +chrome.storage.local.set(settings) //Setting defaults if there are not. + diff --git a/Chrome/retrieveVariables.js b/Chrome/retrieveVariables.js index 49b079a..8735300 100644 --- a/Chrome/retrieveVariables.js +++ b/Chrome/retrieveVariables.js @@ -1,3 +1,5 @@ (async () => { - document.dispatchEvent(new CustomEvent('RetrieveVariable', { detail: "sesskey=" + window.M.cfg.sesskey })); -})(); \ No newline at end of file + if (window.M) { + document.dispatchEvent(new CustomEvent('RetrieveVariable', { detail: "sesskey=" + window.M.cfg.sesskey })); + } +})(); diff --git a/Chrome/rules.json b/Chrome/rules.json new file mode 100644 index 0000000..3206008 --- /dev/null +++ b/Chrome/rules.json @@ -0,0 +1,27 @@ +[ + { + "id": 1, + "priority": 1, + "action": { + "type": "redirect", + "redirect": { + "transform": { + "queryTransform": { + "addOrReplaceParams": [ + { + "key": "forcedownload", + "value": "0" + } + ] + } + } + } + }, + "condition": { + "urlFilter": "*://moodle.*.*/*.pdf", + "resourceTypes": [ + "main_frame" + ] + } + } +] \ No newline at end of file diff --git a/Firefox/background.js b/Firefox/background.js new file mode 100644 index 0000000..a205c41 --- /dev/null +++ b/Firefox/background.js @@ -0,0 +1,17 @@ +browser.webRequest.onBeforeRequest.addListener( + async function (details) { + let settings = await browser.storage.local.get("avoidPdfDownload"); + console.log(settings) + if (!details.url.includes("moodle") || !details.url.includes("forcedownload=1") || settings.avoidPdfDownload === false) { + return; + } + let new_url = details.url.replace("forcedownload=1","forcedownload=0"); + return { + redirectUrl: new_url + } + }, + { + urls: [""] + }, + ["blocking"] +) diff --git a/Firefox/index.js b/Firefox/index.js index cb4c42a..a85aa2d 100644 --- a/Firefox/index.js +++ b/Firefox/index.js @@ -1,5 +1,5 @@ var keepAliveIntervalId; -var keepAliveEnabled = localStorage.getItem("keepAlive") === 'true'; +var keepAliveEnabled = localStorage.getItem("keepAlive") !== 'false'; // not === true to avoid disabled by default. var moodleURLBase = '' browser.storage.local.onChanged.addListener(changes => { @@ -8,6 +8,7 @@ browser.storage.local.onChanged.addListener(changes => { if (keepAliveEnabled) { runKeepAlive(); } else { + console.log("clear") clearInterval(keepAliveIntervalId); } localStorage.setItem("keepAlive", keepAliveEnabled); @@ -21,6 +22,9 @@ if (document.URL.includes("moodle") && location.hostname !== 'moodle') { } } + + + function runKeepAlive() { wrappedJSObject.console.log("ran") if(moodleURLBase === '') { @@ -46,4 +50,4 @@ function runKeepAlive() { } }); }, 1 * 60 * 60 * 1000); -} \ No newline at end of file +} diff --git a/Firefox/manifest.json b/Firefox/manifest.json index da748bf..e966be8 100644 --- a/Firefox/manifest.json +++ b/Firefox/manifest.json @@ -1,37 +1,45 @@ { - "name": "MoodleCustomizer", - "version": "1.1.1", - "manifest_version": 2, - "browser_specific_settings": { - "gecko": { - "id": "{34ba4e89-9b36-487b-8866-a27b304f3db6}" - } - }, - "author": "Hokkaydo", - "permissions": [ - "activeTab", - "storage", - "*://*/*" - ], - "description": "Improve Moodle global experience.", - "content_scripts": [ - { - "matches": [ - "*://*/*" - ], - "js": [ - "index.js" - ] - } + "name": "MoodleCustomizer", + "version": "1.1.2", + "manifest_version": 2, + "browser_specific_settings": { + "gecko": { + "id": "{34ba4e89-9b36-487b-8866-a27b304f3db6}" + } + }, + "author": "Hokkaydo", + "permissions": [ + "activeTab", + "storage", + "webRequest", + "webRequestBlocking", + "*://*/*" + ], + "background": { + "scripts": [ + "background.js" ], - "browser_action": { - "default_icon": { - "48": "icons/logo.png" - }, - "default_title": "On/Off", - "default_popup": "popup/options.html" - }, - "icons": { - "48": "icons/logo.png" + "persistent": false + }, + "description": "Improve Moodle global experience.", + "content_scripts": [ + { + "matches": [ + "*://*/*" + ], + "js": [ + "index.js" + ] } + ], + "browser_action": { + "default_icon": { + "48": "icons/logo.png" + }, + "default_title": "On/Off", + "default_popup": "popup/options.html" + }, + "icons": { + "48": "icons/logo.png" + } } \ No newline at end of file diff --git a/Firefox/moodlecustomizer-1.1.1.zip b/Firefox/moodlecustomizer-1.1.2.zip similarity index 94% rename from Firefox/moodlecustomizer-1.1.1.zip rename to Firefox/moodlecustomizer-1.1.2.zip index e06f6ee..890d915 100644 Binary files a/Firefox/moodlecustomizer-1.1.1.zip and b/Firefox/moodlecustomizer-1.1.2.zip differ diff --git a/Firefox/popup/options.css b/Firefox/popup/options.css index 34f2a79..185bb17 100644 --- a/Firefox/popup/options.css +++ b/Firefox/popup/options.css @@ -1,19 +1,52 @@ -.list { - background-color: white; - display: grid; - grid-template-columns: repeat(2, 1fr); - font-size: small; - font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; +body { + width: 300px; + height: 200px; + display: flex; + flex-direction: column; + align-items: center; } -.onoff > img { - width: 70%; + + +.control { + display: flex; + align-items: center; +} +.control > p { + margin-right: 10px; + } -.list > .onoff { - display: flex; - justify-content: center; - align-items: center; - width: 50px; +input[type="checkbox"] { + position: relative; + appearance: none; + width: 37.5px; + height: 18.75px; + background: #ccc; + border-radius: 50px; + box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2); + cursor: pointer; + transition: 0.4s; +} -} \ No newline at end of file +input:checked[type="checkbox"] { + background: #7da6ff; +} + +input[type="checkbox"]::after { + position: absolute; + content: ""; + width: 18.75px; + height: 18.75px; + top: 0; + left: 0; + background: #fff; + border-radius: 50%; + box-shadow: 0 0 5px rgba(0, 0, 0, 0.2); + transform: scale(1.1); + transition: 0.4s; +} + +input:checked[type="checkbox"]::after { + left: 50%; +} diff --git a/Firefox/popup/options.html b/Firefox/popup/options.html index 723f9b4..dbab794 100644 --- a/Firefox/popup/options.html +++ b/Firefox/popup/options.html @@ -1,16 +1,29 @@ + - - - - - Document + + + + + Document + -
+

Settings:

+
+
+

Keep Alive:

+ +
+
+

Avoid PDF downloads:

+
- +
+ + + \ No newline at end of file diff --git a/Firefox/popup/options.js b/Firefox/popup/options.js index b8cd60b..2d9616f 100644 --- a/Firefox/popup/options.js +++ b/Firefox/popup/options.js @@ -1,20 +1,19 @@ -const toggleKeepAlive = document.createElement("p"); -const keepAliveOnOff = document.createElement("img"); -var keepAlive = localStorage.getItem("keepAlive") === 'true'; -keepAliveOnOff.src = browser.runtime.getURL("icons/" + (keepAlive ? "on.png" : "off.png")); -toggleKeepAlive.textContent = "KeepAlive : "; +let settings = {}; -toggleKeepAlive.addEventListener("click", (_event) => { - const keepAlive = localStorage.getItem("keepAlive") === 'true'; - localStorage.setItem("keepAlive", !keepAlive); - toggleKeepAlive.textContent = "KeepAlive : "; - keepAliveOnOff.src = browser.runtime.getURL("icons/" + (!keepAlive ? 'on.png': 'off.png')); - browser.storage.local.set({keepAlive: !keepAlive}) -}); +for (input of document.getElementsByTagName("input")) { + if (!input.type == "checkbox") { + console.log(input); + continue; + } + let item = input.dataset.item; + input.checked = localStorage.getItem(item) !== 'false'; + settings[item] = input.checked; + input.addEventListener("click", (e) => { + settings[item] = e.target.checked; + browser.storage.local.set(settings); + localStorage.setItem(item, e.target.checked); + }) +} + +browser.storage.local.set(settings) //Setting defaults if there are not. -const list = document.getElementsByClassName("list")[0] -const onoff = document.createElement("div"); -onoff.classList.add("onoff"); -onoff.appendChild(keepAliveOnOff); -list.appendChild(toggleKeepAlive); -list.appendChild(onoff); \ No newline at end of file diff --git a/README.md b/README.md index 061fb2f..0c05fde 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Improve your Moodle experience. ### Firefox - Go to [MoodleCustomizer](https://addons.mozilla.org/addon/moodlecustomizer)'s add-on page and install it from there.\ - You can also choose to use install it by yourself. \ - 1. Download [the latest firefox zip archive](https://github.com/Hokkaydo/MoodleCustomizer/blob/master/Firefox/moodlecustomizer-1.1.1.zip). + 1. Download [the latest firefox zip archive](https://github.com/Hokkaydo/MoodleCustomizer/blob/master/Firefox/moodlecustomizer-1.1.2.zip). 2. Once done, go to your extensions tab click on the settings icon in the top right corner. 3. Select "Install module from a file" 4. Go to your download location and selected the previously downloaded archive @@ -25,7 +25,9 @@ All features can be enabled or disabled by clicking on the addon icon in the top Currently supported features are : - **KeepAlive**\ This prevents Moodle from disconnecting your session while you're working and studying and long PDF or whatever else. +- **PDF Previews**\ + This prevents Moodle to force you to download pdfs, and will always preview then in your browser. ## Contribution Feel free to contribute using the [Pull Request](https://github.com/Hokkaydo/MoodleCustomizer/pulls) system. \ -You can help by suggesting new ideas in the [Issues](https://github.com/Hokkaydo/MoodleCustomizer/issues) section. \ No newline at end of file +You can help by suggesting new ideas in the [Issues](https://github.com/Hokkaydo/MoodleCustomizer/issues) section.