Skip to content

Commit

Permalink
Merge pull request #41 from modos189/fix-install-window
Browse files Browse the repository at this point in the history
  • Loading branch information
modos189 authored Dec 3, 2020
2 parents 6466f4d + 75ffbb7 commit 6c9176c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 53 deletions.
23 changes: 15 additions & 8 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
managePlugin,
runExtension
} from "./manager";
import { onBeforeRequest } from "./requests";

const { onUpdated, onRemoved } = browser.tabs;
onUpdated.addListener(onUpdatedListener);
Expand All @@ -33,15 +34,21 @@ browser.runtime.onMessage.addListener(async request => {
}
});

browser.webNavigation.onBeforeNavigate.addListener(
async requestDetails => {
browser.tabs.create({
url: await browser.extension.getURL(
`/jsview.html?url=${requestDetails.url}`
)
});
browser.webRequest.onBeforeRequest.addListener(
onBeforeRequest,
{
urls: [
// 1. *:// comprises only http/https
// 2. the API ignores #hash part
// 3. Firefox: onBeforeRequest does not work with file:// or moz-extension://
"*://*/*.user.js",
"*://*/*.user.js?*",
"file://*/*.user.js",
"file://*/*.user.js?*"
],
types: ["main_frame"]
},
{ url: [{ pathSuffix: ".user.js" }] }
["blocking"]
);

browser.runtime.onMessage.addListener(function(request) {
Expand Down
57 changes: 57 additions & 0 deletions src/background/requests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import { parseMeta, ajaxGet, getUniqId } from "../helpers";

const whitelist = [
"^https://github.com/[^/]*/[^/]*/raw/[^/]*/[^/]*?\\.user\\.js([?#]|$)",
"^https://gist.github.com/.*?/[^/]*?.user.js([?#]|$)",
"^https://gitlab.com/[^/]*/[^/]*/(|-/)raw/[^/]*/[^/]*?\\.user\\.js([?#]|$)"
].map(re => new RegExp(re));
const blacklist = ["//(?:(?:gist.|)github.com|gitlab.com)/"].map(
re => new RegExp(re)
);

export function onBeforeRequest(req) {
const { method, tabId, url } = req;
if (method !== "GET") {
return;
}

if (!blacklist.some(matches, url) || whitelist.some(matches, url)) {
maybeInstallUserJs(tabId, url).then();
return { redirectUrl: "javascript:void 0" }; // eslint-disable-line no-script-url
}
}

async function maybeInstallUserJs(tabId, url) {
let code = undefined;
try {
code = await ajaxGet(url);
} catch {
if (tabId >= 0) browser.tabs.update(tabId, { url });
}

if (code && parseMeta(code).name) {
const tab = (tabId >= 0 && (await browser.tabs.get(tabId))) || {};
await confirmInstall(url, code);
if (tab.pendingUrl && tab.url === "chrome://newtab/") {
browser.tabs.remove(tabId);
}
}
}

async function confirmInstall(url, code) {
const cache = {};
const uniqId = getUniqId("tmp");
cache[uniqId] = { url: url, code: code };
await browser.storage.local.set(cache);

await browser.tabs.create({
url: await browser.extension.getURL(`/jsview.html?uniqId=${uniqId}`)
});
}

/** @this {string} */
function matches(re) {
return re.test(this);
}
1 change: 1 addition & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export function getUniqId(prefix = "VM") {
);
}

/* exported getUID */
export function getUID(plugin) {
const available_fields = [];

Expand Down
62 changes: 28 additions & 34 deletions src/jsview/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</template>

<script>
import { _, ajaxGet, parseMeta } from "../helpers";
import { _, parseMeta } from "../helpers";
export default {
name: "App",
Expand All @@ -32,42 +32,36 @@ export default {
_: _
},
async mounted() {
const url = new URL(window.location.href).searchParams.get("url");
const uniqId = new URL(window.location.href).searchParams.get("uniqId");
const data = await browser.storage.local.get(uniqId);
const { url, code } = data[uniqId];
await browser.storage.local.remove(uniqId);
let code;
try {
code = await ajaxGet(url);
} catch {
this.status = _("addressNotAvailable");
}
if (code) {
this.code = code;
this.show_header = true;
this.code = code;
this.show_header = true;
const meta = parseMeta(code);
if (meta["name"] !== undefined) {
this.plugin_name = meta["name"];
const filename = url.substr(url.lastIndexOf("/") + 1);
const btn_install = document.getElementById("install");
btn_install.addEventListener(
"click",
async () => {
const message =
_("addedUserScriptTo", [filename, meta["category"]]) + "\n";
meta["filename"] = filename;
const script = [{ meta: meta, code: code }];
const meta = parseMeta(code);
if (meta["name"] !== undefined) {
this.plugin_name = meta["name"];
const filename = url.substr(url.lastIndexOf("/") + 1);
const btn_install = document.getElementById("install");
btn_install.addEventListener(
"click",
async () => {
const message =
_("addedUserScriptTo", [filename, meta["category"]]) + "\n";
meta["filename"] = filename;
const script = [{ meta: meta, code: code }];
alert(message);
await browser.runtime.sendMessage({
type: "addUserScripts",
scripts: script
});
this.show_header = false;
},
false
);
}
alert(message);
await browser.runtime.sendMessage({
type: "addUserScripts",
scripts: script
});
this.show_header = false;
},
false
);
}
}
};
Expand Down
13 changes: 5 additions & 8 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@
"tabs",
"storage",
"<all_urls>",
"webNavigation"
"webRequest",
"webRequestBlocking"
],
"default_locale": "en",
"background": {
"scripts": [
"js/background.js"
],
"persistent": false
"page": "background.html"
},
"content_scripts": [
{
"matches" : ["<all_urls>"],
"matches" : ["https://intel.ingress.com/*"],
"run_at": "document_start",
"js": ["js/content-script.js"],
"all_frames": true
"js": ["js/content-script.js"]
}
],
"browser_action": {
Expand Down
8 changes: 5 additions & 3 deletions vue.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
module.exports = {
pages: {
background: {
template: "public/browser-extension.html",
entry: "src/background/background.js",
title: "background"
},
popup: {
template: "public/browser-extension.html",
entry: "./src/popup/main.js",
Expand All @@ -21,9 +26,6 @@ module.exports = {
pluginOptions: {
browserExtension: {
componentOptions: {
background: {
entry: "src/background/background.js"
},
contentScripts: {
entries: {
"content-script": ["src/content-scripts/loader.js"]
Expand Down

0 comments on commit 6c9176c

Please sign in to comment.