refine raw source code opening prevention

This commit is contained in:
buzz-lightsnack-2007 2024-03-17 18:37:39 +08:00
parent a3c7152497
commit 1e74f8b1ea
3 changed files with 41 additions and 2 deletions

View file

@ -1 +1,3 @@
<html> <head> <script type="module"> import {restrict} from "./scripts/compat.js"; restrict(); </script> </head> </html>
<html> <head>
<script type="module" src="scripts/popup.js"> </script>
</head> </html>

View file

@ -1 +1,26 @@
/* compat.js You shouldn't be opening the HTMLs elsewhere. */ let browser_data = {}; export function refresh() { /* Update variable containing the browser data. Returns: (dict) the updated browser data */ browser_data['URL'] = window.location.href; return (browser_data['URL']); }; export function restrict() { /* Restrict pages containing this function from being loaded outside the extension. Returns: (bool) page open is correct */ refresh(); const browser_extension_URL_contents = new RegExp(`^(?!file:\/\/|http:\/\/|https:\/\/)(.*-extension:\/\/.*)`); const browser_condition_met = browser_extension_URL_contents.test(browser_data.URL); if (!browser_condition_met) { alert("This page can not be opened. \r此网页无法打开。"); }; return (browser_condition_met); };
/* compat.js
You shouldn't be opening the HTMLs elsewhere. */
let browser_data = {};
export function refresh() {
/* Update variable containing the browser data.
Returns: (dict) the updated browser data */
browser_data['URL'] = window.location.href;
return (browser_data['URL']);
};
export function restrict() {
/* Restrict pages containing this function from being loaded outside the extension.
Returns: (bool) page open is correct */
refresh();
const browser_extension_URL_contents = new RegExp(`^(?!file:\/\/|http:\/\/|https:\/\/)(.*-extension:\/\/.*)`);
const browser_condition_met = browser_extension_URL_contents.test(browser_data.URL);
if (!browser_condition_met) {
alert("This page can not be opened. \r此网页无法打开。");
};
console.log(browser_data.URL);
return (browser_condition_met);
};

12
gui/scripts/popup.js Normal file
View file

@ -0,0 +1,12 @@
// Call the main function.
function main() {
}
/* Run detection */
import {restrict} from "./compat.js";
if (!restrict()) {
main();
}