Chrome Extension: getBackgroundPage vs sendMessage. Which method is better? - google-chrome-extension

Question in chrome.runtime.getBackgroundPage(...) and chrome.runtime.sendMessage(...).
The first allows you to get a link to the window background page, the second - to send a message to those who have subscribed to the event.
I feel that the second method is more correct to use, but I don’t have proof of that. On the contrary, I understand that sending messages and receiving a response is a serialization / deserialization of request and response in json, when, when we receive a background page, we get a link to it and it should theoretically be less expensive.
What is the right approach?
Any thoughts?

Related

Response to the Source from Zap with Webhook Trigger

When I have a zap of more than 1 step, and the trigger is Webhooks by Zapier with Catch Hook. When I send the response to the webhook, it provides back to the source this kind of payload response:
{"id": "548299ef-bb3a-4769-885a-fb001407c049", "request_id": "5f197542-b118-4fe9-9b45-f1e10b4ac710", "attempt": "5f197542-b118-4fe9-9b45-f1e10b4ac710", "status": "success"}
Is there a way that the connection of the Zap is kept alive and rather than providing the response up, it provides the response of the last step of the Zap?
Short answer: no, it's not possible. You'll always get the json payload you posted when you send a webhook (unless you disable it, but then you just get nothing).
Long answer: No, because the zap execution happens separately from the webhook ingestion. Think of this operation less like a webserver (where the caller is waiting for a response) and more asynchronously (where the caller asks that a thing be done, the server confirms it will do it, and then the thing happens eventually).
What you want to use here is Make, as they support webhook responses.
Check out https://www.make.com/en/help/tools/webhooks
No affiliation, I just assumed Zapier could do this (and am slightly annoyed they can't).

Microsoft graph, batch request's nextLink

I'm currently implementing a sync queue service to sync a webapp's customers to Outlook's contacts.
I'm using the Graph API for the job. The creation and updating of contacts is done using graph's batch request.
There's a part in the docs about the response that I don't fully understand and pretty much ignored. I just want to make sure my implementation is correct.
In addition to the responses property, there might be a nextLink
property in the batch response. This allows Microsoft Graph to return
a batch response as soon as any of the individual requests has
completed. To ensure that all individual responses have been received,
continue to follow the nextLink as long as it exists.
I was wondering about the following:
when does nextLink show up? I've tried sending different requests but never received it. It's not really clear from the docs but my guess is that it appears when for some reason some of the requests in the batch did not complete in time?
Would the pending requests show up as errors in the response or would they just be missing from it?
Will the nextLink be in form of #odata.nextLink like in pagination requests? It does not specify that in the docs.
How should I handle it when/if it does appear? Can I safely ignore it and just count on the next invocation of service (every 15mins) to retry and sync the pending requests?
The paging mechanism mostly applies when you are querying Graph for data.
The nextLink shows up if whatever query you had as part of one of your batch requests requires pagination (just as if you ran the request directly). For example this request as part of your batch job would cause one to appear, provided the targeted user has more than 10 folders:
{
"id":"1",
"method":"GET",
"url":"users/user#domain.tld/mailFolders"
}
The response shows up as normal (with the first page of data included in the response body, along with the nextLink to get to the next page).
Correct. In the above example, the nextLink shows up like this:
"#odata.nextLink":"https://graph.microsoft.com/beta/users/user#domain.tld/mailFolders?$skip=10
You will need to follow the nextLink to get the rest of the data.

Caching response for API.ai Node.js webhook

I have a webhook designed in Node.js for API.ai that interacts with multiple API's to gather information and give response to user.
Since, I am interacting with multiple API's the response time taken is more than 5 secs which is causing the API.ai request to timeout.
To overcome this, I am trying to implement caching into the node.js webhook which saves the response from API's until a certain amount of time. This will remove timeout until the max-age header time is reached.
Edit: What is the best node module that I can use to cache the API responses for subsequest requests.
Note: I am using request node module for http requests but it doesnt seem to provide a way to cache the response.
All of the answers given are reasonable for tackling the cache problem on the request side. But since you specified API.AI and Actions, you might also be able to, or need to, store information while the conversation is in progress. You can do this using an API.AI context.
It may even be that if you limit it to just one remote call for each response from the user, you might be able to fit it in the timeframe.
For example, if you were having a conversation about movie times and ticket ordering, the conversation may go something like:
User: "I want to see a movie."
[You use an API to lookup the nearest theater, store the theater's location in a context and reply] "Your nearest theater is the Mall Megaplex. Are you interested in one there?"
User: "Sure"
[You now already have the theater, so you query for what it is playing with another API call and store it in a context] "There are seven different movies playing, including Star Wars and Jaws. Do those sound interesting?"
User: "No"
[You already have the data in the context, so you don't need another call.] "How about Rocky or..."
In this way you're making the same number of calls (generally), but storing the user's results in the session as you go as opposed to collecting all the information for the user, or all the possible results, and then narrowing them.
Finally decided to use the below module:
https://www.npmjs.com/package/memory-cache
This served my scenario better. Might try using Redis soon when i get some time.

Sending POST requests to Google Action/API.AI or sending responses which take more than 5s

My fulfillment needs to do a lot of processing after receiving a certain request from Google Action/API.AI and the default response timeout is 5s.
https://developers.google.com/actions/components/fulfillment#nodejs
Is there any way I could send a delayed response or send a POST request after the results are ready?
The short answer is no - you must respond within 5 seconds, and there is no way to send a notification back through the Assistant at this time.
The slightly longer answer is that we know notifications are coming - but we don't know if there will be an API for them. There have been rumors about other ways that may be coming that allow us to work around the 5 second limit.
The even longer answer is that, if you are using Action Transactions (ie - allowing the user to purchase or reserve something) you can send updates after the fact. However, Transactions are still in developer preview and don't work on all surfaces (they don't work on Google Home at all, for example).

Determining frame URL for outgoing requests, using WebRequest API

I'm using the WebRequest API to modify requests that get sent by Chrome. In order to know how to rewrite the request I would like to know what's the frame URL that caused the outgoing request. I see I can get frame IDs and tab IDs, with which I could send a message to the content script to find out the URI. But since messaging is always asynchronous there seems to be no way to ensure that I get that information before the request gets sent.
This is for a testing tool, not something for regular users, so I wouldn't mind incurring some added latency. Does anyone know if there is there another way to do this? I tried using setTimeout but it's blocked by content security policy. Using referrer doesn't quite cut it because it's not set on HTTP requests coming from an HTTPS frame.
I am not sure if i fully understand what are you trying to accomplish, but here is what i think.
Scenario
Main frame google, sub frames facebook and twitter, and you want to modify any requests from the facebook frame !
If that's the case then here is what i am going to try;
1.Register the onCompleted event listener, which will be used to retrieve the completed requests info, i.e. url and frame id, and store them in an array.
2.Register the onBeforeSendHeaders event listener, which will be used to retrieve the request info and compare it against the one you stored in the previous step, if it passed you can modify the headers
So the code will go like this
onCompleted ({store the info -i.e. url and frame id- in an array},...)
onBeforeSendHeaders ({compare the frame id that made the request with the one stored before if they match modify the header},...)
Difference between your approach and the one i listed
In your approach you used asynchronous messages to retrieve info about the frame after the requests get sent, in my approach you will have those info ready for you with no need to do any farther messaging, so whenever a request happens you can use them immediately.
Hope you will find this helpful, good-luck.

Resources