Skip to content

Commit

Permalink
Merge pull request #64 from IITC-CE/missions
Browse files Browse the repository at this point in the history
Plugins for missions.ingress.com, parser meta block
  • Loading branch information
modos189 authored Aug 23, 2021
2 parents 349669c + 614d6c6 commit 7350951
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 128 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iitc-button",
"version": "1.6.6",
"version": "1.7.0",
"repository": "https://github.com/IITC-CE/IITC-Button.git",
"license": "GPLv3",
"private": true,
Expand Down
4 changes: 3 additions & 1 deletion src/background/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { getUID } from "../helpers";
import { isSemVer } from "./issemver";

export async function on_extension_update(last_version) {
const release_plugins = await browser.storage.local.get("release_plugins").then(obj => obj["release_plugins"]);
const release_plugins = await browser.storage.local
.get("release_plugins")
.then(obj => obj["release_plugins"]);

if (release_plugins || isSemVer(last_version, "< 1.4.0")) {
const local = await browser.storage.local.get([
Expand Down
2 changes: 1 addition & 1 deletion src/background/injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export async function injectUserScript(code, tabs) {
// Fetch all completly loaded Ingress Intel tabs
export async function getTabsToInject() {
return await browser.tabs.query({
url: "https://intel.ingress.com/*",
url: ["https://intel.ingress.com/*", "https://missions.ingress.com/*"],
status: "complete"
});
}
32 changes: 20 additions & 12 deletions src/background/intel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ let lastIITCTab = null;
export async function onRequestOpenIntel() {
if (lastIITCTab) {
const tabInfo = await getTabInfo(lastIITCTab);
if (isIngressUrl(tabInfo.url)) {
console.debug(`detected intel.ingress.com page on tab ${lastIITCTab}`);
await setTabActive(lastIITCTab);
if (isIngressIntelUrl(tabInfo.url)) {
return await setTabActive(lastIITCTab);
}
}

Expand All @@ -27,10 +26,7 @@ export async function onToggleIITC(value) {
await browser.storage.local.set({ IITC_is_enabled: value });

// Fetch all completly loaded Ingress Intel tabs
const tabs = await browser.tabs.query({
url: "https://intel.ingress.com/*",
status: "complete"
});
const tabs = await getTabsToInject();

for (let tab of Object.values(tabs)) {
await browser.tabs.reload(tab.id);
Expand All @@ -42,9 +38,10 @@ export async function onUpdatedListener(tabId, status) {
if (status.status === "complete") {
const tabInfo = await getTabInfo(tabId);
if (isIngressUrl(tabInfo.url)) {
console.debug(`detected intel.ingress.com page on tab ${tabId}`);
await initialize();
lastIITCTab = tabId;
if (isIngressIntelUrl(tabInfo.url)) {
lastIITCTab = tabId;
}
}
}
}
Expand Down Expand Up @@ -85,7 +82,7 @@ async function initialize() {
const plugins_local = data[channel + "_plugins_local"];
const plugins_user = data[channel + "_plugins_user"];

if ((status === undefined || status === true) && iitc_code !== undefined) {
if (status !== false && iitc_code !== undefined) {
const tabs = await getTabsToInject();
const userscripts = [];

Expand Down Expand Up @@ -123,9 +120,20 @@ async function getTabInfo(tabId) {
return await browser.tabs.get(tabId);
}

function isIngressUrl(url) {
function isIngressIntelUrl(url) {
if (url) {
return /^https:\/\/intel\.ingress\.com/.test(url);
}
return false;
}

function isIngressMissionsUrl(url) {
if (url) {
return /intel.ingress.com/.test(url);
return /^https:\/\/missions\.ingress\.com/.test(url);
}
return false;
}

function isIngressUrl(url) {
return isIngressIntelUrl(url) || isIngressMissionsUrl(url);
}
4 changes: 3 additions & 1 deletion src/background/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ export async function runExtension() {

async function versionCheck() {
const currentVersion = browser.runtime.getManifest().version;
const lastVersion = await browser.storage.local.get("lastversion").then(obj => obj["lastversion"]);
const lastVersion = await browser.storage.local
.get("lastversion")
.then(obj => obj["lastversion"]);

if (lastVersion !== currentVersion) {
if (lastVersion) {
Expand Down
30 changes: 20 additions & 10 deletions src/background/requests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

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

const whitelist = [
"^https://github.com/[^/]*/[^/]*/raw/[^/]*/[^/]*?\\.user\\.js([?#]|$)",
Expand All @@ -17,10 +22,14 @@ export function onBeforeRequest(req) {
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
}
browser.storage.local.get(["IITC_is_enabled"]).then(data => {
if (data["IITC_is_enabled"] !== false) {
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) {
Expand All @@ -31,10 +40,11 @@ async function maybeInstallUserJs(tabId, url) {
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/") {
if (code) {
const meta = parseMeta(code);

if (meta.name && check_meta_match_pattern(meta)) {
await confirmInstall(url, code);
browser.tabs.remove(tabId);
}
}
Expand All @@ -47,7 +57,7 @@ async function confirmInstall(url, code) {
await browser.storage.local.set(cache);

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

Expand Down
1 change: 0 additions & 1 deletion src/content-scripts/gm-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export const GM = function() {
);
},
_access: function(key) {
console.log("_access");
return (
meta.grant !== undefined &&
meta.grant.some(permission => {
Expand Down
79 changes: 10 additions & 69 deletions src/content-scripts/loader.js
Original file line number Diff line number Diff line change
@@ -1,81 +1,22 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3
import { getUID, getUniqId, parseMeta } from "../helpers";
import { GM } from "./gm-api";

const loaded_plugins = [];

document.addEventListener("DOMContentLoaded", function() {
window.onload = function() {};
document.body.onload = function() {};
});

function inject(code) {
const script = document.createElement("script");
script.appendChild(document.createTextNode(code));
(document.body || document.head || document.documentElement).appendChild(
script
);
script.parentElement.removeChild(script);
}
import { inject, xmlHttpRequestBridge, IITCButtonInitJS } from "./utils";

function preparePage() {
document.addEventListener("DOMContentLoaded", function() {
if (window.location.hostname === "intel.ingress.com") {
window.onload = function() {};
document.body.onload = function() {};
}
});

inject(
`((${GM.toString()}))()\n//# sourceURL=${browser.runtime.getURL(
"js/GM_api.js"
)}`
);

document.addEventListener("xmlHttpRequestBridge", async function(e) {
const data = e.detail;
await browser.runtime.sendMessage({
type: "xmlHttpRequestHandler",
value: data
});
});

document.addEventListener("IITCButtonInitJS", function(e) {
const tab_id = e.detail.tab_id;
const code = e.detail.code;

const meta = parseMeta(code);
const uid = getUID(meta);
meta["uid"] = uid;

let dataKey = sessionStorage.getItem(uid);
if (!dataKey) {
dataKey = getUniqId("VMin");
sessionStorage.setItem(uid, dataKey);
}

if (loaded_plugins.includes(uid)) {
console.debug(`Plugin ${uid} is already loaded. Skip`);
} else {
loaded_plugins.push(uid);
console.debug(`Plugin ${uid} loaded`);

const name = encodeURIComponent(meta.name);
const injectedCode = [
"((GM)=>{",
// an implementation of GM API v3 based on GM API v4
"const GM_info = GM.info; const unsafeWindow = window;",
"const GM_getValue = (key, value) => GM._getValueSync(key, value);",
"const GM_setValue = (key, value) => GM._setValueSync(key, value);",
"const GM_xmlhttpRequest = (details) => GM.xmlHttpRequest(details);",

code,
// adding a new line in case the code ends with a line comment
code.endsWith("\n") ? "" : "\n",
`})(GM("${dataKey}", ${tab_id}, ${JSON.stringify(meta)}))`,

// Firefox lists .user.js among our own content scripts so a space at start will group them
`\n//# sourceURL=${browser.runtime.getURL(
"plugins/%20" + name + ".user.js"
)}`
].join("");

inject(injectedCode);
}
});
document.addEventListener("xmlHttpRequestBridge", xmlHttpRequestBridge);
document.addEventListener("IITCButtonInitJS", IITCButtonInitJS);
}

browser.storage.local.get(["IITC_is_enabled"]).then(data => {
Expand Down
76 changes: 76 additions & 0 deletions src/content-scripts/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

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

const LOADED_PLUGINS = [];

export function inject(code) {
const script = document.createElement("script");
script.appendChild(document.createTextNode(code));
(document.body || document.head || document.documentElement).appendChild(
script
);
script.parentElement.removeChild(script);
}

export async function xmlHttpRequestBridge(e) {
const data = e.detail;
await browser.runtime.sendMessage({
type: "xmlHttpRequestHandler",
value: data
});
}

export async function IITCButtonInitJS(e) {
const tab_id = e.detail.tab_id;
const code = e.detail.code;

const meta = parseMeta(code);
const uid = getUID(meta);
meta["uid"] = uid;

let dataKey = sessionStorage.getItem(uid);
if (!dataKey) {
dataKey = getUniqId("VMin");
sessionStorage.setItem(uid, dataKey);
}

if (LOADED_PLUGINS.includes(uid)) {
console.debug(`Plugin ${uid} is already loaded. Skip`);
} else {
if (!check_meta_match_pattern(meta, window.location.hostname)) {
console.debug(`Skip ${uid}, not right domain`);
return;
}

LOADED_PLUGINS.push(uid);
console.debug(`Plugin ${uid} loaded`);

const name = encodeURIComponent(meta.name);
const injectedCode = [
"((GM)=>{",
// an implementation of GM API v3 based on GM API v4
"const GM_info = GM.info; const unsafeWindow = window;",
"const GM_getValue = (key, value) => GM._getValueSync(key, value);",
"const GM_setValue = (key, value) => GM._setValueSync(key, value);",
"const GM_xmlhttpRequest = (details) => GM.xmlHttpRequest(details);",

code,
// adding a new line in case the code ends with a line comment
code.endsWith("\n") ? "" : "\n",
`})(GM("${dataKey}", ${tab_id}, ${JSON.stringify(meta)}))`,

// Firefox lists .user.js among our own content scripts so a space at start will group them
`\n//# sourceURL=${browser.runtime.getURL(
"plugins/%20" + name + ".user.js"
)}`
].join("");

inject(injectedCode);
}
}
Loading

0 comments on commit 7350951

Please sign in to comment.