Skip to content

Commit

Permalink
Add support for Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
tadajam committed May 31, 2019
1 parent 3b7bdaf commit 350deac
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 43 deletions.
74 changes: 40 additions & 34 deletions extension_scripts/background.ts
Expand Up @@ -45,6 +45,13 @@ interface Request {
data: any;
}

enum Platform {
PLATFORM_CHROME,
PLATFORM_FIREFOX,
PLATFORM_EDGE,
PLATFORM_OPERA
}

class Background {

private version = 1;
Expand All @@ -69,43 +76,18 @@ class Background {
}

assignEventHandlers(): void {
chrome.tabs.onUpdated.addListener((tabId: number, changeInfo: chrome.tabs.TabChangeInfo, tab: chrome.tabs.Tab) => {
if ('status' in changeInfo && changeInfo.status === 'complete') {
if (tab.url) {
this.existsVault().then(exists => {
if (exists) {
chrome.tabs.executeScript(tabId, {file : 'extension_scripts/contentscript.js'}, error => {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message);
}
});
}
});
}
}
});

chrome.runtime.onConnect.addListener((port: chrome.runtime.Port) => {
if (port.name === ContentScriptMessage.PortName) {
port.onMessage.addListener((message: any, connectedPort: chrome.runtime.Port) => {
switch (message.type) {
case ContentScriptMessage.InPageContent:
chrome.runtime.getPackageDirectoryEntry((directoryEntry: DirectoryEntry) => {
directoryEntry.getDirectory('extension_scripts', {create: false} , (targetDirectory: DirectoryEntry) => {
targetDirectory.getFile('inpage.js', {create: false}, (inpageFile: FileEntry) => {
inpageFile.file(file => {
const reader = new FileReader();
reader.onload = () => {
connectedPort.postMessage({
type: message.type,
script: reader.result
});
};
reader.readAsText(file);
});
this.readAsTextInpage()
.then(script => {
connectedPort.postMessage({
type: message.type,
script: script
});
});
});
break;

case ContentScriptMessage.InPageInit:
Expand Down Expand Up @@ -159,9 +141,34 @@ class Background {
}
});

chrome.runtime.onSuspend.addListener(() => {
chrome.browserAction.setBadgeText({text: ''});
});
if (this.getPlatform() === Platform.PLATFORM_CHROME || this.getPlatform() === Platform.PLATFORM_OPERA) {
chrome.runtime.onSuspend.addListener(() => {
chrome.browserAction.setBadgeText({text: ''});
});
}
}

async readAsTextInpage(): Promise<string> {
const inpage = await fetch(chrome.runtime.getURL('extension_scripts/inpage.js'), { method: 'GET' });
return await inpage.text();
}

private getPlatform(): Platform {
const ua = navigator.userAgent;
if (ua.search('Firefox') !== -1) {
return Platform.PLATFORM_FIREFOX;
} else {
// if (window && window.chrome && window.chrome.ipcRenderer) {
// return Platform.PLATFORM_BRAVE;
// } else
if (ua.search('Edge') !== -1) {
return Platform.PLATFORM_EDGE;
} else if (ua.search('OPR') !== -1) {
return Platform.PLATFORM_OPERA;
} else {
return Platform.PLATFORM_CHROME;
}
}
}

async send(tx: string): Promise<any> {
Expand Down Expand Up @@ -191,7 +198,6 @@ class Background {
private popup(): void {
chrome.windows.create({
url: 'index.html',
focused : true,
type: 'popup',
width: 375,
height: 636
Expand Down
1 change: 0 additions & 1 deletion extension_scripts/contentscript.ts
Expand Up @@ -31,7 +31,6 @@ class ContentScript {

window.addEventListener('message', (event: MessageEvent) => {
if (event.origin === window.location.origin && event.data.action) {
// if ((event.origin === window.location.origin || window.location.origin === 'file://') && event.data.action) {
const data = this.isAlive ? event.data.message : {error: 'Extension context invalidated'};
if (this.isAlive) {
this.port.postMessage({type: this.getRequestType(event.data.action), id: event.data.id, origin: event.origin, data: data});
Expand Down
25 changes: 17 additions & 8 deletions src/manifest.json
@@ -1,13 +1,26 @@
{
"name": "Mpurse",
"manifest_version": 2,
"description": "Chrome extension for Monaparty",
"version": "0.0.3",
"description": "Extension for Monaparty",
"version": "0.1.0",
"icons": {
"16": "assets/mpchain16.png",
"48": "assets/mpchain48.png",
"128": "assets/mpchain128.png"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": [
"extension_scripts/contentscript.js"
],
"run_at": "document_start",
"all_frames": true
}
],
"background": {
"scripts": ["extension_scripts/background.js"],
"persistent": false
Expand All @@ -21,10 +34,6 @@
},
"permissions": [
"storage",
"activeTab",
"file://*/*",
"http://*/*",
"https://*/*"
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
"activeTab"
]
}

0 comments on commit 350deac

Please sign in to comment.