Skip to content

Commit

Permalink
Merge pull request #38 from IITC-CE/1.5.1
Browse files Browse the repository at this point in the history
Fix initialization of plugins
  • Loading branch information
modos189 authored Oct 11, 2020
2 parents c8ee78a + 29b9ff2 commit d8b5d8b
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 26 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.5.0",
"version": "1.5.1",
"repository": "https://github.com/IITC-CE/IITC-Button.git",
"license": "GPLv3",
"private": true,
Expand Down
17 changes: 11 additions & 6 deletions src/background/injector.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3
export async function injectUserScript(code) {
export async function injectUserScript(code, tabs) {
const inject = `
document.dispatchEvent(new CustomEvent('IITCButtonInitJS', {
detail: ${JSON.stringify(code)}
}));
`;
// Fetch all completly loaded Ingress Intel tabs
const tabs = await browser.tabs.query({
url: "https://intel.ingress.com/*",
status: "complete"
});

if (tabs === undefined) tabs = await getTabsToInject();

for (let tab of Object.values(tabs)) {
try {
Expand All @@ -21,3 +18,11 @@ export async function injectUserScript(code) {
}
}
}

// Fetch all completly loaded Ingress Intel tabs
export async function getTabsToInject() {
return await browser.tabs.query({
url: "https://intel.ingress.com/*",
status: "complete"
});
}
29 changes: 20 additions & 9 deletions src/background/intel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3
import { preparationUserScript } from "../helpers";
import { injectUserScript } from "./injector";
import { getUID, parse_meta, preparationUserScript } from "../helpers";
import { getTabsToInject, injectUserScript } from "./injector";

let lastIITCTab = null;

Expand Down Expand Up @@ -87,25 +87,36 @@ async function initialize() {
const plugins_user = data[channel + "_plugins_user"];

if ((status === undefined || status === true) && iitc_code !== undefined) {
const inject_iitc_code = preparationUserScript({
version: iitc_version,
code: iitc_code
});
await injectUserScript(inject_iitc_code);
const tabs = await getTabsToInject();
const userscripts = [];

const iitc_meta = parse_meta(iitc_code);
const iitc_uid = getUID(iitc_meta);
const inject_iitc_code = preparationUserScript(
{
version: iitc_version,
code: iitc_code
},
iitc_uid
);
userscripts.push(inject_iitc_code);

const plugins_flat = data[channel + "_plugins_flat"];
for (const uid of Object.keys(plugins_flat)) {
if (plugins_flat[uid]["status"] === "on") {
await injectUserScript(
userscripts.push(
preparationUserScript(
plugins_flat[uid]["user"] === true
? plugins_user[uid]
: plugins_local[uid],
uid
)
),
tabs
);
}
}

await Promise.all(userscripts.map(code => injectUserScript(code, tabs)));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/background/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ async function updateExternalPlugins(local) {
let exist_updates = false;
const hash = `?${Date.now()}`;

for await (const uid of Object.keys(plugins_user)) {
for (const uid of Object.keys(plugins_user)) {
const plugin = plugins_user[uid];

if (plugin["updateURL"] && plugin["downloadURL"]) {
Expand Down Expand Up @@ -335,7 +335,7 @@ async function updateLocalPlugins(plugins_flat, plugins_local) {
if (plugins_local === undefined) return {};

// Iteration local plugins
for await (const uid of Object.keys(plugins_local)) {
for (const uid of Object.keys(plugins_local)) {
let filename = plugins_local[uid]["filename"];

if (filename && plugins_flat[uid]) {
Expand Down
10 changes: 5 additions & 5 deletions src/content-scripts/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ function preparePage() {

const GM_info_raw = code.substring(0, code.indexOf(";/* END GM_info */"));
const GM_info = new Function("GM_info", GM_info_raw + ";return GM_info")();
const id = GM_info.script.name;
const uid = GM_info.script.uid;

if (loaded_plugins.includes(id)) {
console.debug(`Plugin ${id} is already loaded. Skip`);
if (loaded_plugins.includes(uid)) {
console.debug(`Plugin ${uid} is already loaded. Skip`);
} else {
loaded_plugins.push(id);
console.debug(`Plugin ${id} loaded`);
loaded_plugins.push(uid);
console.debug(`Plugin ${uid} loaded`);
new Function(sandbox + code)();
}
});
Expand Down
7 changes: 4 additions & 3 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ function h(str) {
}

// Implementation of partial sufficient compatibility with GreaseMonkey
export function preparationUserScript(plugin, name) {
if (name === undefined) name = "";
export function preparationUserScript(plugin, uid) {
if (uid === undefined) uid = "";

return `var GM_info = {
"script": {
"uid": "${h(uid)}",
"version": "${h(plugin["version"])}",
"name": "${h(name)}",
"name": "${h(plugin["name"])}",
"description": "${h(plugin["description"])}"
}
};/* END GM_info */
Expand Down

0 comments on commit d8b5d8b

Please sign in to comment.