Skip to content

Commit

Permalink
Merge commit 'refs/pull/40/head' of github.com:IITC-CE/IITC-Button in…
Browse files Browse the repository at this point in the history
…to 1.6.0
  • Loading branch information
modos189 committed Dec 2, 2020
2 parents a69c251 + 5c26e86 commit 802cbdd
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 178 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-vue": "^6.2.2",
"prettier": "^1.19.1",
"vue-cli-plugin-browser-extension": "latest",
"vue-cli-plugin-browser-extension": "^0.25.1",
"vue-template-compiler": "^2.6.11"
},
"_id": "[email protected]",
Expand Down
4 changes: 1 addition & 3 deletions public/assets/iconfont/material-icons.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: local('Material Icons'),
local('MaterialIcons-Regular'),
url(MaterialIcons-Regular.woff2) format('woff2');
src: url(MaterialIcons-Regular.woff2) format('woff2');
}

.material-icons {
Expand Down
38 changes: 38 additions & 0 deletions src/background/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ browser.runtime.onMessage.addListener(async request => {
case "toggleIITC":
await onToggleIITC(request.value);
break;
case "xmlHttpRequestHandler":
await xmlHttpRequestHandler(request.value);
break;
}
});

Expand Down Expand Up @@ -59,3 +62,38 @@ browser.runtime.onMessage.addListener(function(request) {
break;
}
});

async function xmlHttpRequestHandler(data) {
async function xmlResponse(tab_id, callback, response) {
const injectedCode = `
document.dispatchEvent(new CustomEvent('onXmlHttpRequestHandler', {
detail: JSON.stringify({
callback: "${String(callback)}",
response: ${String(response)}
})
}));
`;

try {
await browser.tabs.executeScript(data.tab_id, {
code: injectedCode
});
} catch (error) {
console.error(`An error occurred while execute script: ${error.message}`);
}
}

const req = new XMLHttpRequest();
req.onload = function() {
const response = {
readyState: this.readyState,
responseHeaders: this.responseHeaders,
responseText: this.responseText,
status: this.status,
statusText: this.statusText
};
xmlResponse(data.tab_id, data.onload, JSON.stringify(response));
};
req.open(data.method, data.url, true, data.user, data.password);
req.send();
}
13 changes: 7 additions & 6 deletions src/background/injector.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3
export async function injectUserScript(code, tabs) {
const inject = `
if (tabs === undefined) tabs = await getTabsToInject();

for (let tab of Object.values(tabs)) {
const inject = `
document.dispatchEvent(new CustomEvent('IITCButtonInitJS', {
detail: ${JSON.stringify(code)}
detail: ${JSON.stringify({ code: code, tab_id: tab.id })}
}));
`;

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

for (let tab of Object.values(tabs)) {
try {
await browser.tabs.executeScript(tab.id, {
code: inject
code: inject,
runAt: "document_end"
});
} catch (error) {
console.error(`An error occurred while reloading tabs: ${error.message}`);
Expand Down
28 changes: 5 additions & 23 deletions src/background/intel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3
import { getUID, parse_meta, preparationUserScript } from "../helpers";
import { getTabsToInject, injectUserScript } from "./injector";

let lastIITCTab = null;
Expand Down Expand Up @@ -63,9 +62,6 @@ async function initialize() {
"release_iitc_code",
"test_iitc_code",
"local_iitc_code",
"release_iitc_version",
"test_iitc_version",
"local_iitc_version",
"release_plugins_flat",
"test_plugins_flat",
"local_plugins_flat",
Expand All @@ -81,7 +77,6 @@ async function initialize() {

const status = data["IITC_is_enabled"];
const iitc_code = data[channel + "_iitc_code"];
const iitc_version = data[channel + "_iitc_version"];

const plugins_local = data[channel + "_plugins_local"];
const plugins_user = data[channel + "_plugins_user"];
Expand All @@ -90,32 +85,19 @@ async function initialize() {
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") {
userscripts.push(
preparationUserScript(
plugins_flat[uid]["user"] === true
? plugins_user[uid]
: plugins_local[uid],
uid
),
tabs
plugins_flat[uid]["user"] === true
? plugins_user[uid]["code"]
: plugins_local[uid]["code"]
);
}
}

userscripts.push(iitc_code);

await Promise.all(userscripts.map(code => injectUserScript(code, tabs)));
}
}
Expand Down
23 changes: 6 additions & 17 deletions src/background/manager.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3

import {
_,
ajaxGet,
clearWait,
getUID,
parse_meta,
preparationUserScript,
wait
} from "../helpers";
import { _, ajaxGet, clearWait, getUID, parseMeta, wait } from "../helpers";
import { on_extension_update } from "./extension";
import { injectUserScript } from "./injector";

Expand Down Expand Up @@ -303,7 +295,7 @@ async function updateExternalPlugins(local) {
// download meta info
const response_meta = await getUrl(plugin["updateURL"] + hash);
if (response_meta) {
let meta = parse_meta(response_meta);
let meta = parseMeta(response_meta);
// if new version
if (
meta &&
Expand Down Expand Up @@ -377,12 +369,9 @@ export async function managePlugin(uid, category, action) {
}

await injectUserScript(
preparationUserScript(
plugins_flat[uid]["user"] === true
? plugins_user[uid]
: plugins_local[uid],
uid
)
plugins_flat[uid]["user"] === true
? plugins_user[uid]["code"]
: plugins_local[uid]["code"]
);

await save({
Expand All @@ -400,7 +389,7 @@ export async function managePlugin(uid, category, action) {
plugins_local[uid] = plugins_flat[uid];
plugins_local[uid]["code"] = response;

await injectUserScript(preparationUserScript(plugins_local[uid], uid));
await injectUserScript(plugins_local[uid]["code"]);

await save({
plugins_flat: plugins_flat,
Expand Down
4 changes: 2 additions & 2 deletions src/choose_file/BlockDrop.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</template>

<script>
import { _, parse_meta } from "../helpers";
import { _, parseMeta } from "../helpers";
/*
* Validation UserScript and adding to IITC Button
Expand All @@ -45,7 +45,7 @@ const processingFile = async fileList => {
try {
const code = await readUploadedFileAsText(file);
const meta = parse_meta(code);
const meta = parseMeta(code);
if (meta === {} || meta["name"] === undefined) {
message += _("notValidUserScript", file["name"]) + "\n";
Expand Down
4 changes: 2 additions & 2 deletions src/choose_file/BlockURL.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</template>

<script>
import { _, ajaxGet, parse_meta } from "../helpers";
import { _, ajaxGet, parseMeta } from "../helpers";
export default {
name: "BlockURL",
Expand Down Expand Up @@ -52,7 +52,7 @@ export default {
if (code) {
const scripts = [];
let message = "";
const meta = parse_meta(code);
const meta = parseMeta(code);
const filename = url.substr(url.lastIndexOf("/") + 1);
if (meta === {} || meta["name"] === undefined) {
Expand Down
132 changes: 132 additions & 0 deletions src/content-scripts/gm-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
//@license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-v3
export const GM = function() {
window.GM = function(dataKey, tab_id, meta) {
return {
info: {
script: meta
},
_getValueSync: function(key, default_value) {
if (!this._access("getValue")) return undefined;

const items = sessionStorage.getItem(dataKey + "_" + key);
if (items !== null) {
return JSON.parse(items);
} else {
return default_value;
}
},
getValue: function(key, default_value) {
return new Promise((resolve, reject) => {
if (!this._access("getValue")) return reject;
resolve(this._getValueSync(key, default_value));
});
},
setValue: function(key, value) {
return new Promise((resolve, reject) => {
if (!this._access("setValue")) return reject;

sessionStorage.setItem(dataKey + "_" + key, JSON.stringify(value));
resolve();
});
},
deleteValue: function(key) {
return new Promise((resolve, reject) => {
if (!this._access("deleteValue")) return reject;

sessionStorage.removeItem(dataKey + key);
resolve();
});
},
listValues: function() {
return new Promise((resolve, reject) => {
if (!this._access("listValues")) return reject;

let keys = [];
let prelen = "Vm123".length;
for (let key of Object.keys(sessionStorage)) {
if (key.substr(0, prelen) === dataKey) {
keys.push(key.substr(prelen + 1));
}
}
resolve(keys);
});
},
getResourceUrl: function() {
return new Promise((resolve, reject) => {
if (!this._access("getResourceUrl")) return reject;
});
},
openInTab: function() {},
notification: function() {},
setClipboard: function() {},
xmlHttpRequest: function(details) {
let data = Object.assign(
{
binary: false,
context: {},
data: null,
headers: {},
method: null,
overrideMimeType: null,
url: null,
user: null,
password: null,
timeout: 0,
onabort: null,
onerror: null,
onload: null,
onprogress: null,
onreadystatechange: null,
ontimeout: null
},
details
);

for (let key in data) {
if (key.startsWith("on")) {
data[key] = String(data[key]);
}
}

data.tab_id = tab_id;

document.dispatchEvent(
new CustomEvent("xmlHttpRequestBridge", {
detail: data
})
);
},
_access: function(key) {
console.log("_access");
return (
meta.grant !== undefined &&
meta.grant.some(permission => {
return permission.substr(3) === key;
})
);
}
};
};
document.addEventListener("onXmlHttpRequestHandler", function(e) {
function parseFunction(str) {
var fn_body_idx = str.indexOf("{"),
fn_body = str.substring(fn_body_idx + 1, str.lastIndexOf("}")),
fn_declare = str.substring(0, fn_body_idx),
fn_params = fn_declare.substring(
fn_declare.indexOf("(") + 1,
fn_declare.lastIndexOf(")")
),
args = fn_params.split(",");
args.push(fn_body);
function Fn() {
return Function.apply(this, args);
}
Fn.prototype = Function.prototype;
return new Fn();
}

const detail = JSON.parse(e.detail);
const callback = parseFunction(detail.callback);
callback(detail.response);
});
};
Loading

0 comments on commit 802cbdd

Please sign in to comment.