I've read Match all URLs except certain URLs in Chrome Extension but its accepted solution is only applicable to content scripts within the manifest file.
In
background.js
I have the following working code:
chrome.webRequest.onBeforeSendHeaders.addListener(handler, {urls: ["<all_urls>"] }, ["blocking", "requestHeaders"]);
Question arises because I need it to apply to all urls except one ( *://*.facebook.com/* ).
How to create this exception?
If the only solution is regex could you please guide me on how to properly write it?
The filters you can to supply when calling addListener for webRequest events, i.e. the RequestFilter type, use match patterns (where the docs say this). Match patterns do not have the capability to do what you desire: exclude specific URLs, while allowing all others.
You will have to perform the exclusion in your webRequest handler by checking the url property of the details Object which is passed to your listener. Doing so can be something like:
function listenForWebrequest(details){
if(/^[^:]*:(?:\/\/)?(?:[^\/]*\.)?facebook.com\/.*$/.test(details.url)) {
//The URL matched our exclusion criteria
return;
} //else
//Do your normal event processing here
}
chrome.webRequest.onBeforeSendHeaders.addListener(listenForWebrequest, {
urls: ["<all_urls>"]
}, ["blocking", "requestHeaders"]);
Related
In Postman, I can create a set of common tests that run after every endpoint in the collection/folder Tests tab, like so:
pm.test("status code is 200", function () {
pm.response.to.have.status(200);
});
But how should I do this for my schema validation on the response object? Each endpoint has a different expected schema. So I have something like this on each individual endpoint:
const schema = { type: 'array', items: ... }
pm.test('response has correct schema', function () {
const {data} = pm.response.json();
pm.expect(tv4.validate(data, schema)).to.be.true;
});
I can't extract this up to the collection level because each schema is different.
Now I find that I want to tweak that test a little bit, but I'll have to copy-and-paste it into 50 endpoints.
What's the recommended pattern here for sharing a test across endpoints?
I had the same issue some years ago, and I found two ways to solve this:
Create a folder for each structure object
You can group all your test cases that share the same structure into a new folder and create a test case for this group. The issue with this is that you will be repeating the requests in other folders. (For this solution, you will need to put your "tests cases" into the folder level)
Create a validation using regular expressions (recommended)
Specify in the name of each request a set of rules (these rules will indicate what kind of structure or call they might have). Then you create a validation in the first parent folder for each type of variation (using regex). (You will need to create documentation and some if statements in your parent folder)
E.g.: [POST] CRLTHM Create a group of homes
Where each initial is meaning to:
CR: Response must be 201
LT: The response must be a list of items
HM: The type of the response must be a home object
And the regex conditional must be something like this (this is an example, please try to make your regex accurate):
if(/CRLTHM\s/.test(pm.info.requestName))
(In this image, NA is referring just to Not Authenticated)
I need to make a request for a CSS file.
I know which folder on the server my file will be in.
What I don't know is the exact file name. It will be titled of the form theme.bundle.xxxxxxxxxxxxxx.css where xxxxxxxxxxxxxx is a series of random characters and numbers generated at build time.
My question is, is it possible to make an HTTP request with a regex to get the name of the matching file(s)? I don't need help constructing the regex itself, but rather how to utilize one in combination with an HTTP request.
I can't find any information related to the usage of regular expressions to construct an HTTP request, or if this is even possible.
Short answer: Not possible, unless you have access to customize your server. You tagged this question as an "angular" question. From an Angular standpoint - Angular can't make this happen.
Longer answer: Totally possible! But this ends up being more of a backend question, not an Angular question. You didn't specify which backend you have, so I'll use a Node/Express server as an example. Part of building a server is setting up routing and API endpoints. Consider this code that responds with a particular file whenever the server receives a GET request to /images/background
app.get('/images/background', function(req, res) {
res.sendFile('public/img/background.png')
})
For your situation, you would need to set up an endpoint with similar logic to this:
app.get('/getMyCssFile', function(req, res) {
// Use NodeJS fs module to loop over files in /testfolder and read the file names
let matchingFile;
fs.readdirSync(testFolder).forEach(file => {
console.log(file);
// Perform REGEX matching here, if filename matches, then save this file name
if (matches) {
matchingFile = file;
}
})
if (matchingFile) {
res.sendFile(file)
} else {
// handle sending error - no matching file found
}
})
On your Angular frontend, you'd just need to request /getMyCssFile, and your server will respond with the matching file.
According to my understandings, you should not post to get data.
For example, I'm on a project and we are posting to get data.
For example, this following.
{
"zipCOde":"85022",
"city":"PHOENIX"
"country":"US"
"products":[
{
"sku":"abc-21",
"qty":2
},
{
"sku":"def-13",
"qty":2
}
]
}
Does it make sense to post? How could this be done without posting? There could be 1 or more products.
Actually there is a SEARCH method in HTTP, but sadly it is for webdav. https://msdn.microsoft.com/en-us/library/aa143053(v=exchg.65).aspx So if you want to send a request body with the request, then you can try with that.
POSTing is okay if you have a complex search. Complex search is relative, by me it means, that you have different logical operators in your query.
The current one is not that complex, and you can put the non-hierarchical components into the query string of the URI. An example with additional line breaks:
GET /products/?
zipCOde=85022&
city=PHOENIX&
country=US&
filters[0]['sku']=abc-21&
filters[0]['qty']=2&
filters[1]['sku']=def-13&
filters[1]['qty']=2
You can choose a different serialization format and encode it as URI component if you want.
GET /products/?filter={"zipCOde":"85022","city":"PHOENIX","country":"US","products":[{"sku":"abc-21","qty":2},{"sku":"def-13","qty":2}]}
One potential option is to JSON.serialize your object and send it as a query string parameter on the GET.
I would like to define different middleware depending on how the path looks.
The thing is that the path may wary; I'd like to for instance support the following paths:
/chat/auth/test
/chat/auth/add
/chrome/auth/test
/chrome/add
Every time auth is in the path I would like the auth middleware to be called, and for chat and chrome I want their respective middlewares to be called.
app.js:
// Middleware authentication
var chatAuthenticate = require('./server/middleware/chat-authenticate');
app.use('/chat', chatAuthenticate(addon));
var authAuthentication = require('./server/middleware/auth-authenticate');
app.use('/auth', authAuthentication());
I understand that I can just add multiple entries to app.js for every possible combination, like /chat/auth and /chrome/auth and it wouldn't increase the complexity at all, but I'm just curious if it's possible to solve this with wildcards or regex :)
app.use(/auth/, authAuthentication);
This will call authAuthentication for every request that contains auth anywhere. A few things to consider:
It is a Regular Expression, so instead of quotes you just need /…/ as delimiters
Note that if you have multiple middlewares that match a route (e.g. another one with /chat/ as RegEx, and you call /auth/chat or /chat/auth, both middlewares will be called. Because of that it is important to consider the order of your app.use() calls.
authAuthentication is required to expect (request, response, next) parameters. If you directly call the function as in your example, the function is expected to return a function that will take the three express middleware parameters.
It is common practice to place all require calls at the top of your script
Make sure you fully read and understood Express Routing. It is awesome.
You can use wildcards (at least in express 4):
app.use('/chat/*', chatMiddleware);
Which would apply that middleware first to any request starting with '/chat/'. Then use the next one for the next level which would only apply to '/chat/auth/*'...
app.use('/chat/auth/*', function(req, res, next) {
//.... middleware logic here
//.... assuming we don't reject the call, call next() when done
next();
));
I think this should be pretty straight forward but if I have a url(s) with a request URIs such as:
/en/my-hometown/92-winston
/en/my-hometown/92-winston/backyard
and I want to set up a GA view which only includes this page and any subpages, then does the following Filter work in Analytics?
Will that basically filter against and URI which specifically contains 92-winston or do I need to wrap it in a fancy regex? I'm not that great with RegEx.
Thanks! Apologies in advance if this is ridiculously easy.
You may want to try this regex, if you know exactly that the URI will start with "/en/my-hometown/92-winston":
^\/en\/my\-hometown\/92\-winston.*
This captures URI strings that start exactly with "/en/my-hometown/92-winston" and also include anything that may come after that, for example
/en/my-hometown/92-winston
/en/my-hometown/92-winston/backyard
/en/my-hometown/92-winston/some_other_page
Just beware that if you have more than one "Include" filter in GA, then you need to make sure you aren't actually excluding more data, as multiple include filters are "and"ed. Always test your new filters in your test view as well.