Skip to content

Commit

Permalink
Initial WebExtension setup
Browse files Browse the repository at this point in the history
  • Loading branch information
bhollis committed May 5, 2018
1 parent fa655c8 commit 52e1546
Show file tree
Hide file tree
Showing 11 changed files with 560 additions and 133 deletions.
95 changes: 0 additions & 95 deletions .eslintrc

This file was deleted.

4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
*.xpi
.tern-port
/build
/main.js
/node_modules

11 changes: 0 additions & 11 deletions .jpmignore

This file was deleted.

File renamed without changes
88 changes: 88 additions & 0 deletions lib/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const jsonRequestIds = new Set<string>();

interface RequestDetails {
requestId: string,
url: string,
method: string,
frameId: number,
parentFrameId: number,
tabId: number,
type: browser.webRequest.ResourceType,
timeStamp: number,
originUrl: string,
statusLine: string,
responseHeaders?: browser.webRequest.HttpHeaders,
statusCode: number,
}

function listener(details: RequestDetails) {
console.log("JSONView listener", details);
if (!jsonRequestIds.has(details.requestId)) {
console.log("JSONView this isn't JSON");
return;
}

const requestId = details.requestId;
const filter = browser.webRequest.filterResponseData(details.requestId);

// TODO: figure out encoding I guess
const enc = new TextDecoder("utf-8");
let content = "";

function disconnect() {
console.log("JSONView disconnect filter");
filter.disconnect();
jsonRequestIds.delete(requestId);
}

filter.onstart = (event: Event) => {
console.log("JSONView started", event, details);

if (jsonRequestIds.has(requestId)) {
console.log("JSONView YEAH start");
} else {
disconnect();
}
};

filter.ondata = (event: Event & {
data: ArrayBuffer
}) => {
console.log(event.data, event);
filter.write(event.data);
if (jsonRequestIds.has(requestId)) {
console.log("JSONView YEAH data");
}
content = content + enc.decode(event.data);
};

filter.onstop = (event: Event) => {
console.log("JSONView finished", event, details);
console.log("CONTENT", content);
disconnect();
};
}

function detectJSON(event: RequestDetails) {
console.log("JSONView headers", event);
if (!event.responseHeaders) {
return;
}
for (const header of event.responseHeaders) {
if (header.name === "Content-Type" && header.value && header.value.includes("application/json")) {
console.log("JSONView found some JSON!");
jsonRequestIds.add(event.requestId);
listener(event);
}
}

return { responseHeaders: event.responseHeaders };
}

// Listen for onHeaderReceived for the target page.
// Set "blocking" and "responseHeaders".
browser.webRequest.onHeadersReceived.addListener(
detectJSON,
{ urls: ["https://jsonview.com/*"], types: ["main_frame"] },
["blocking", "responseHeaders"]
);
File renamed without changes.
26 changes: 26 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"manifest_version": 2,
"name": "JSONView",
"version": "2.0.0",
"description": "View JSON documents in the browser.",
"author": "Benjamin Hollis",
"homepage_url": "https://jsonview.com/",
"icons": {
"32": "icon.png",
"64": "icon64.png",
"256": "icon256.png"
},
"background": {
"scripts": ["lib/main.js"]
},
"permissions": [
"<all_urls>",
"webRequest",
"webRequestBlocking"
],
"applications": {
"gecko": {
"id": "[email protected]"
}
}
}
38 changes: 11 additions & 27 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,40 +1,17 @@
{
"id": "[email protected]",
"version": "1.2.4",
"version": "2.0.0",
"name": "jsonview",
"title": "JSONView",
"description": "View JSON documents in the browser.",
"homepage": "https://jsonview.com/",
"author": "Benjamin Hollis",
"repository": "https://github.com/bhollis/jsonview",
"license": "MIT",
"main": "lib/main.js",
"permissions": {
"multiprocess": true
"scripts": {
"start": "tsc && rollup build/main.js --format iife --name 'main' --file main.js",
"lint": "tslint -p tsconfig.json"
},
"preferences": [
{
"name": "jsonHttpAccept",
"type": "bool",
"value": false,
"title": "Include \"application/json\" in the HTTP Accept header for requests",
"description": "Only enable this if you need it - this can cause problems with some websites."
},
{
"name": "alternateContentTypes",
"type": "string",
"value": "",
"title": "Alternate JSON content types",
"description": "Other content types that should be treated as JSON, separated by commas."
},
{
"name": "useNativeJsonView",
"type": "bool",
"value": false,
"title": "Use built-in Firefox JSON viewer",
"description": "Firefox now includes a built-in JSON viewer you can use instead of JSONView."
}
],
"contributors": [
"Gabriel Barros",
"Quoc-Viet Nguyen"
Expand Down Expand Up @@ -122,5 +99,12 @@
"zh-CN": {
"description": "在浏览器中查看JSON 文件."
}
},
"devDependencies": {
"rollup": "^0.58.2",
"tslint": "^5.10.0",
"tslint-eslint-rules": "^5.1.0",
"typescript": "^2.8.3",
"web-ext-types": "^2.0.1"
}
}
19 changes: 19 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"outDir": "./build/",
"sourceMap": true,
"strictNullChecks": true,
"noImplicitAny": true,
"module": "ESNext",
"target": "es6",
"moduleResolution": "node",
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowJs": true,
"checkJs": true,
"typeRoots": ["node_modules/@types", "node_modules/web-ext-types"]
},
"include": [
"./lib/main.*"
]
}
Loading

0 comments on commit 52e1546

Please sign in to comment.