Skip to content

Commit

Permalink
Prevent moodle from downloading pdfs (#2)
Browse files Browse the repository at this point in the history
By passing forceDownload=1 to forceDownload=0, we can block moodle from forcing download of pdf's before reading
  • Loading branch information
tdaron authored Nov 17, 2023
1 parent e7c2f57 commit 6e266ff
Show file tree
Hide file tree
Showing 17 changed files with 362 additions and 164 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*/web-ext-artifacts/*
*/node_modules/*
Chrome.crx
Chrome.pem
Chrome.pem
Chrome/_metadata
31 changes: 29 additions & 2 deletions Chrome/background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,37 @@
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 },
files: cs.js,
});
}
}
});
});

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();
6 changes: 4 additions & 2 deletions Chrome/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var keepAliveIntervalId;
var keepAliveEnabled = localStorage.getItem("keepAlive") === 'true';
var keepAliveEnabled = localStorage.getItem("keepAlive") !== 'false';
var sesskey = '';
var moodleURLBase = ''

Expand All @@ -9,6 +9,7 @@ chrome.storage.onChanged.addListener((changes, area) => {
if (keepAliveEnabled) {
runKeepAlive();
} else {
console.log("clear")
clearInterval(keepAliveIntervalId);
}
localStorage.setItem("keepAlive", keepAliveEnabled);
Expand Down Expand Up @@ -50,6 +51,7 @@ function setupSesskey(callback) {
}

function runKeepAlive() {
console.log("ran")
if(moodleURLBase === '') {
return;
}
Expand Down Expand Up @@ -77,4 +79,4 @@ function runKeepAlive() {
}
});
}, 1 * 60 * 60 * 1000);
}
}
96 changes: 56 additions & 40 deletions Chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -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": [
"<all_urls>"
],
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"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": ["<all_urls>"]
}
"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": [
"<all_urls>"
],
"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": [
"<all_urls>"
]
}
]
}
63 changes: 48 additions & 15 deletions Chrome/popup/options.css
Original file line number Diff line number Diff line change
@@ -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;
}
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%;
}
29 changes: 21 additions & 8 deletions Chrome/popup/options.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="options.css">
<title>Document</title>
</head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="options.css">
<title>Document</title>
</head>

<body>
<div class="list">

<h3>Settings:</h3>
<div id="form">
<div class="control">
<p>Keep Alive:</p>
<input data-item="keepAlive" type="checkbox" />
</div>
<div class="control">
<p>Avoid PDF downloads:</p>
<input data-item="avoidPdfDownload" type="checkbox" />
</div>
<script src="options.js"></script>
</div>

<script src="options.js"></script>
</body>

</html>
37 changes: 19 additions & 18 deletions Chrome/popup/options.js
Original file line number Diff line number Diff line change
@@ -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);
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.

6 changes: 4 additions & 2 deletions Chrome/retrieveVariables.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(async () => {
document.dispatchEvent(new CustomEvent('RetrieveVariable', { detail: "sesskey=" + window.M.cfg.sesskey }));
})();
if (window.M) {
document.dispatchEvent(new CustomEvent('RetrieveVariable', { detail: "sesskey=" + window.M.cfg.sesskey }));
}
})();
27 changes: 27 additions & 0 deletions Chrome/rules.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
]
17 changes: 17 additions & 0 deletions Firefox/background.js
Original file line number Diff line number Diff line change
@@ -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: ["<all_urls>"]
},
["blocking"]
)
Loading

0 comments on commit 6e266ff

Please sign in to comment.