I am trying to unblock a blocked URL using chrome.webrequest api,I went through the api documentation and implemented.It is blocking the url ,but after it ,when i run the unblock part of the code ,it is not unblocking the url.Below,is the code for the unblocking part.Is there a way ?
chrome.webRequest.onBeforeRequest.addListener(
function(details) { return { } },
{urls: ["url to be unblocked "]},
["requestBody"]);
Related
What I want to do
I'm trying to intercept a third party website's fetch events and modify its request body in a Chrome extension. Modifying the request body is not allowed by the chrome.webRequest.onBeforeRequest event handler. But it looks like regular service workers do have the ability to listen for and modify fetch events and manually respond to the request using my own response, which means I should be able to intercept the request, modify the body, send the modified request to the API, and then respond to the original request with my modified request's response.
The problem
It looks like neither of these event handlers ever get triggered, despite plenty of fetch events being triggered by the website, as I can see in the Network panel.
// background.js
self.onfetch = (event) => console.log(event.request); // never shows
// or
self.addEventListener("fetch", (event) => {
console.log(event.request); // never shows
});
I can verify that the service worker is running by seeing other console.logs appearing in the service worker console, both top-level logs as well as logs triggered by the "install" event
// background.js
console.log(self); // works
self.addEventListener("install", (event) => {
console.log(event); // works
});
Hypothesis
Do the fetch event handlers not get triggered because extension service workers are not allowed access to these for security reasons? That would make sense, I just haven't seen this documented anywhere explicitly so it would be good to know if this is indeed a platform limitation or if I'm doing something wrong.
Alternate solutions?
If this is indeed a limitation of the extensions platform, is there any way other way I can use a Chrome extension to modify request bodies on a third party website?
So I am writing an application in Node.js and Electron, and I am trying to login through Google on the same session, then get another URL. I have the session working and the login to Google working, but when I login to Google, I want it to switch and load another URL. The current idea I have is something like this:
win.loadURL('https://accounts.google.com/').then(() => {
});
setInterval(() => {
while (!win.webContents.getURL().includes("myaccount.google")) {
if (win.webContents.getURL().includes("myaccount.google")) {
break;
}
}
clearInterval();
}, 100);
win.loadURL('http://' + url);
I just don't know what else to do, I know this is fairly spaghetti but I've tried so many things and nothing seems to work correctly. I feel like I shouldn't even be doing a while loop at all because it seems to freeze my browser (understandably).
Listen for the event 'did-navigate'. I would like to but I can't test this for you at this time.
Also: electron webview navigation event
I made a nodeJS script to automate a few actions on a website - which is not mine!
To have a bit more control over what is going on, I would like to listen to the events on the website's socket.io stream.
Works in NODE so far:
Logging into the website and receiving their cookies as a string for further requests
Sending requests with the cookies from the login (do the actual actions)
Open a websocket connection and listen to the public (!) events
Doesn't work in NODE yet:
Read "private" events that are only being sent to a specific user (me)
I inspected a XHR request that is happening in chrome when clicking a specific button on this website. After this request has been sent, the websocket connection on chrome emits events about the status of my action. Of course, these events are only being sent to the user who performed this action.
Doing the exact same request in node (with the cookies from the website login) gives the right response (success), but the socket stream i opened before, only shows some public events - nothing about my actions.
As seen here, it logs in, displays the website's cookies, opens a socket stream. Then it sends a XHR POST request with the displayed cookies in the headers. The response says "success", but the socket.io events popping up once a second are only the public ones (userCount).
http://i.imgur.com/ZUrA2el.jpg
After sending the request, there should be events like "step_calc" popping up, displaying the status of my action.
My script
After receiving the website's login cookies as a string, I am running this:
const io = require('socket.io-client');
const request = require('request');
main()
function main() {
var socket = io(socketURL, {});
socket.on('connect', function () {
setTimeout(function(){
performAction(); // Send XHR to server
console.log(" > Sending XHR request...")
}, 1500)
});
socket.on('step_calc', function (data) { // Personal event about my action
console.log(" >>> Event = step_calc: " + data)
});
socket.on('login_time', function (data) { // Personal event being displayed every few seconds IF LOGGED IN (chrome) console.log(" >>> Event = step_calc: " + data)
});
socket.on('userCount', function (data) { // Public event
console.log(" >>> Event = userCount: " + data)
});
socket.on('disconnect', function () {
console.log(" > [Disconnected]");
});
}
1500ms after being connected to the socket, it would send the XHR request that should make the server emit information to the socket - performAction().
When I check the chrome console:
step_calc follows to a successfull XHR request (account specific)
login_time is being displayed every 2 seconds, but only if i am logged in (account specific)
userCount is being displayed all the time - to everybody
I checked the socket.io-client's API guide and found out about socketIDs. But it only says, how to get this id after connecting to the server...
https://github.com/socketio/socket.io-client/blob/master/docs/API.md#socket
... and yes ... when opening the website, the first thing chrome does, is send a GET request to the website, with data like this:
EIO=3&transport=polling&t=1493058868222-0
The response contains some kind of "sid".
{"sid":"g_mqoOS__________bHb","upgrades":["websocket"],"pingInterval":25000,"pingTimeout":60000}
Well...
Now that I have gathered all of this information, how can I use it?
How can I make the socket connection be "connected" to the cookies that I got from the login (which I am using to send requests to the website)?
I really hope that my question is kind of understandable. Any help is appreciated, I have already put a lot of time into trying to make it work by myself.
Thanks a lot!
Edit:
I tried to add the same cookies from the handshake in chrome. One weird thing is, that the first XHR it does when i open the website (which seems to be the handshake), already contains a cookie named "io", which is then replaced by a new one. If I check the chrome console>application>cookies, I can't see this cookie at all. Where does it come from?
Left side: The request under the XHR tab on chrome
Right side: This is being displayed under the Websocket tab
http://i.imgur.com/VkRouQf.jpg
Are those two different requests or is it the same one in some way?
Does this information help somehow help to solve my problem?
From what I can see, you're not passing the session cookies to the socket.io-client constructor, which would probably mean that the socket connection isn't being authenticated.
Try this:
var socket = io(socketURL, {
extraHeaders: {
Cookie : '...'
}
});
Documented here.
I am developing my own implementation of DevMode for GWT-like framework. This DevMode is to
work like GWT's old DevMode, which is going to be deprecated, since GWT teavm doesn't want to
support browser plugins. Another reason is NPAPI was removed from latest versions of Google Chrome.
So my DevMode implementation has its own Google Chrome extension, which is written completely in
JavaScript and communicates with server through WebSocket.
The main goal of DevMode is to enable debugging of Java code in Java IDE. It runs Java code
in JVM, and when JVM attempts to execute JavaScript code, it transfers it remotely to browser
extension, which executes JavaScript and sends result back. However, thing are slightly more
difficult in practice, as JVM can pass callback to JavaScript, so there is bi-directional
message flow between JVM and browser extension, in any depth of nesting. Consider the following
ping-pong:
public class PingPong {
public native void ping(int count) /*-{
console.log("Before ping %d", count);
this.pong(count - 1);
console.log("After ping %d", count);
-}*/;
public void pong(int count) {
System.out.println("Before pong " + count);
ping(count - 1);
System.out.println("After pong " + count);
}
}
When ping calls pong it should wait until JVM receives message, handles it and sends response
back. Furthermore, JVM calls ping again and so forth. However, WebSocket API is asynchronous.
Also, we can't just block until JVM responds, as JVM may call JavaScript again.
So I use remote debugging protocol in my extension.
When extension receives a command to call a JavaScript method,
it sends the Runtime.callFunctionOn command to debugger API. When Java passes callback to
extension, it creates the following proxy function:
function() {
var params = [];
for (var i = 0; i < arguments.length; ++i) {
params.push(arguments[i]);
}
return sendMessage({
type : "invoke-method",
methodName : methodName,
args : params
});
}
where sendMessage is:
function sendMessage(message) {
sentMessage = message;
while (sentMessage != null) {
debugger;
}
return receivedMessage;
}
Further, extension listens to the Debugger.paused notification. When a notification comes,
extension sends message to JVM and waits for response. After response is received,
extension sets receivedMessage, clears sentMessage and sends Debugger.resume to debugger.
Unfortunately, I have two major problems. First, I when call
element.addEventListener("click", callback)
from JVM, and then click the element, Debugger.paused comes as expected.
But when I read sentMessage through RDP, I get objectId different from that I passed to
addEventListener. Are objects ids persistent in Google Chrome debugger API?
Second problem is speed. RDP roundtrip takes 10-20 milliseconds on each call. Is there another
way to block execution of script using Google Chrome extension APIs? Also, I want not only
to block, but to enter a nested event loop, since JVM callback may call JavaScript again.
I'm writing a Chrome background extension with firebase. What I've noticed is that when I hibernate my computer (Win7) while Chrome is open, the connection with firebase disconnects (as would be expected). However, when I return from hibernate it does not automatically reconnect and my .on() events are no longer firing.
I've already looked through the API documentation and this StackOverflow question about detecting disconnects: Detect if Firebase connection is lost/regained
Here's some pseudocode that I'm using to test the connection:
var myFirebase = new Firebase('https://my.firebaseio.com/'); //replace
setInterval(function() {
myFirebase.child('.info/connected').on('value', function (snap) {
if (snap.val() === true) {
console.log('Connected: ' + (new Date()).toString());
}
else {
console.log('Disconnected: ' + (new Date()).toString());
}
});
}, 5000);
This works as expected when running the javascript through a webpage - it connects, then disconnects when I hibernate, then reconnects soon after coming back from hibernation.
However, when it runs as a background page as part of a google extension, it disconnects and never reconnects.
Is there a way to force a reconnect and/or a reason why this isn't working as expected?
Do your permissions look something like this?
"content_security_policy": "script-src 'self' https://static.firebase.com https://myfirebase.firebaseio.com https://s-ord-nss-1.firebaseio.com https://s-ord-nss-2.firebaseio.com; object-src 'self'"
And are you getting any permission errors in the log?
(Just a hunch--no idea if this could be related to connection logic, but I've had problems with permissions in the past.)