Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.

Commit

Permalink
Pass BrowserInfo and PlatformInfo to content script
Browse files Browse the repository at this point in the history
  • Loading branch information
ExE-Boss committed Aug 21, 2017
1 parent a7dd8ac commit c7f6d5a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 26 deletions.
62 changes: 47 additions & 15 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,23 @@
*/
/* global browser */

/**
* @typedef {Object} PlatformInfo
* @property {String} os
* @property {String} arch
*/
/**
* @typedef {Object} BrowserInfo
* @property {String} name
* @property {String} vendor
* @property {String} version
* @property {String} buildID
*/

/**
*
* @type Map@type Map
*/
let prevStates = new Map();

(async function() {
Expand All @@ -37,7 +54,7 @@ let prevStates = new Map();
}
})();

browser.runtime.onMessage.addListener((message, sender, resolve) => {
browser.runtime.onMessage.addListener((message, sender) => {
if (/\/popup\/popup\.xhtml$/.test(sender.url)) {
return handlePopupMessage(message);
}
Expand Down Expand Up @@ -94,17 +111,31 @@ var tabHandler = (function() {
*/
async function handlePopupMessage(message) {
let method = String(message.method);
let window, tab;
[window, tab] = await Promise.all([
let window, tab, platformInfo, browserInfo;
[window, tab, platformInfo, browserInfo] = await Promise.all([
browser.windows.getCurrent(),
browser.tabs.query({ active: true, currentWindow: true }).then(tabs => tabs[0])
browser.tabs.query({ active: true, currentWindow: true }).then(tabs => tabs[0]),
browser.runtime.getPlatformInfo(),
browser.runtime.getBrowserInfo()
]);
switch (method) {
case "init": {
let response;
try {
await browser.tabs.executeScript(tab.id, { file: "/content/content.js", runAt: "document_end" });
response = await browser.tabs.sendMessage(tab.id, {method: "init"});
response = await browser.tabs.sendMessage(tab.id, {
method: "init",
platformInfo: {
os: platformInfo.os,
arch: platformInfo.arch
},
browserInfo: {
name: browserInfo.name,
vendor: browserInfo.vendor,
version: browserInfo.version,
buildID: browserInfo.buildID
}
});
} catch (e) {
if (e.message !== "Missing host permission for the tab")
console.warn(e);
Expand Down Expand Up @@ -195,16 +226,6 @@ async function handlePopupMessage(message) {
windows.forEach(({ id }) => browser.windows.remove(id));
} default: {
if (method.startsWith("openHelp")) {
let browserInfo;
let platformInfo;
{
let browserData = await Promise.all([
browser.runtime.getBrowserInfo(),
browser.runtime.getPlatformInfo()
]);
browserInfo = browserData[0];
platformInfo = browserData[1];
}
let lang = browser.i18n.getUILanguage().replace(/_/g, "-");
let os;
switch (platformInfo.os) {
Expand Down Expand Up @@ -244,6 +265,17 @@ async function handlePopupMessage(message) {
}
}
}

message.platformInfo = {
os: platformInfo.os,
arch: platformInfo.arch
};
message.browserInfo = {
name: browserInfo.name,
vendor: browserInfo.vendor,
version: browserInfo.version,
buildID: browserInfo.buildID
};
return browser.tabs.sendMessage(tab.id, message);
}
}
Expand Down
45 changes: 34 additions & 11 deletions src/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,38 @@
*/
/* global browser */

/**
* @typedef {Object} PlatformInfo
* @property {String} os
* @property {String} arch
*/
/**
* @typedef {Object} BrowserInfo
* @property {String} name
* @property {String} vendor
* @property {String} version
* @property {String} buildID
*/

/**
* @type Boolean
*/
var amLoaded = amLoaded || false;

if (!amLoaded) {
amLoaded = true;
browser.runtime.onMessage.addListener(async (message, sender) => {
let method = String(message.method);
let platformInfo = {
os: String(message.platformInfo.os),
arch: String(message.platformInfo.arch)
};
let browserInfo = {
name: String(message.browserInfo.name),
vendor: String(message.browserInfo.vendor),
version: String(message.browserInfo.version),
buildID: String(message.browserInfo.buildID)
};
switch (method) {
case "init": {
let result = {
Expand All @@ -31,29 +57,26 @@ if (!amLoaded) {
"saveAs"
]
}
if (document.documentElement.requestFullScreen || document.documentElement.mozRequestFullScreen) {
//result.enable.push("fullscreen");
}
if (document.querySelector(":focus")) {
result.enable.push("edit*");
}
return result;
} case "editCut": {
return document.execCommand("cut");
return {result: document.execCommand("cut")};
} case "editCopy": {
return document.execCommand("copy");
return {result: document.execCommand("copy")};
} case "editPaste": {
return document.execCommand("paste");
return {result: document.execCommand("paste")};
} case "editUndo": {
return document.execCommand("undo");
return {result: document.execCommand("undo")};
} case "editRedo": {
return document.execCommand("redo");
return {result: document.execCommand("redo")};
} case "editSelectAll": {
return document.execCommand("selectAll");
return {result: document.execCommand("selectAll")};
} case "editDelete": {
return document.execCommand("delete");
return {result: document.execCommand("delete")};
} case "print": {
return window.print();
return {result: window.print()};
} default: {
throw new Error(`Unsupported Function '${method}'`);
}
Expand Down

0 comments on commit c7f6d5a

Please sign in to comment.