Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Network Reading #7

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7,862 changes: 7,862 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
},
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
],
"background": {
Expand Down
15 changes: 15 additions & 0 deletions src/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,18 @@ import GatewayRequests from './gateway/requests'

let RequestListener = new GatewayRequests()
RequestListener.listen()


import NetworkFirefox from './network/firefox.network'
let netListener = new NetworkFirefox()
netListener.subscribe('*://*/kcsapi/*', data => {
let responseObj = null
if (data.body.indexOf('svdata=') === 0)
data.body = data.body.substr(7)
if (data.body)
responseObj = JSON.parse(data.body)
if (responseObj)
console.log('DATA', data.request.requestBody.formData, responseObj.api_data)
else
console.log('DATA', data.request.requestBody.formData, data.body)
})
68 changes: 68 additions & 0 deletions src/service/network/firefox.network.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import FirefoxRequest from './firefox.request'

class FirefoxNetwork {

constructor () {
this.requests = {}
}

subscribe (urlPattern, callback) {
// onBeforeRequest
browser.webRequest.onBeforeRequest.addListener(this.onBeforeRequest.bind(this, callback), {
urls: [ urlPattern ]
}, [ 'blocking', 'requestBody' ])
//
browser.webRequest.onBeforeSendHeaders.addListener(this.onBeforeSendHeaders.bind(this), {
urls: [ urlPattern ]
}, [ 'requestHeaders' ])
// onCompleted
browser.webRequest.onCompleted.addListener(this.onCompleted.bind(this, callback), {
urls: [ urlPattern ]
}, [ 'responseHeaders' ])
}

// Before request is sent, add to tracking so we get all data
onBeforeRequest (callback, details) {
// console.log('onBeforeRequest', details.requestBody);
// Track this request for data
let newRequest = new FirefoxRequest()
newRequest.updateRequest(details)
newRequest.onDone(callback)
newRequest.stream()
// Add to list for later referece on complete
this.requests[ details.requestId ] = newRequest
}

onBeforeSendHeaders (details) {
// console.log('onBeforeSendHeaders', details.requestBody);
// If request is not being tracked, do nothing
let trackedRequest = this.requests[ details.requestId ]
if (!trackedRequest) return

// Update request data, because `onBeforeSendHeaders` contains request headers
trackedRequest.updateRequest({ requestHeaders: details.requestHeaders })
}

// On request complete, return all useful data to callback
onCompleted (callback, details) {
// If request is not being tracked, do nothing
let trackedRequest = this.requests[ details.requestId ]
if (!trackedRequest) return

// Update response details
trackedRequest.updateResponse(details)

// Build full request data
/*callback({
request: trackedRequest.getRequest(),
response: trackedRequest.getResponse(),
body: trackedRequest.getBody()
})*/
// Remove the request from tracked list
// delete this.requests[ details.requestId ]
}

}

export default FirefoxNetwork
65 changes: 65 additions & 0 deletions src/service/network/firefox.request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

class FirefoxRequest {

constructor () {
this.request = {}
this.response = {}
this.body = ''
this.filter = null
this.decoder = new TextDecoder("utf-8")
this.encoder = new TextEncoder()
this.callback = null
}

updateRequest (details) {
this.request = Object.assign({}, this.request, details)
}

updateResponse (details) {
this.response = Object.assign({}, this.response, details)
}

onDone (callback) {
this.callback = callback
}

stream () {
// Get the stream for this request ID
this.filter = browser.webRequest.filterResponseData(this.request.requestId);

// When a chunk of data is received
this.filter.ondata = event => {
// Decode the response data
let responseChunk = this.decoder.decode(event.data, { stream: true })
// Forward to the browser
this.filter.write(this.encoder.encode(responseChunk))
// Append the chunk to the cumulative request body
this.body += responseChunk
}

// When the transfer has stopped
this.filter.onstop = event => {
if (this.callback) this.callback({
request: this.getRequest(),
response: this.getResponse(),
body: this.getBody()
})
this.filter.disconnect()
}
}

getRequest () {
return this.request
}

getResponse () {
return this.response
}

getBody () {
return this.body
}

}

export default FirefoxRequest
Empty file removed src/service/network/index.js
Empty file.
29 changes: 0 additions & 29 deletions src/service/network/network.firefox.js

This file was deleted.