Regex in chrome.declarativeContent.PageStateMatcher - javascript

I need to add some Regex inside my chrome.declarativeContent.PageStateMatcher.
So far I have
var matcher = new chrome.declarativeContent.PageStateMatcher({
pageUrl: {
urlContains: "something.com/someRegex/something"
}
});
Essentially I want to have a regular expression to evaluate to "something.com/4-5 char string/somithing".
How would I do this? Is this possible to do with chrome.declarativeContent.PageStateMatcher?
Thanks

Instead of urlContains use urlMatches
This is what I'm using for both domain fedmich.com and fedche.com
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlMatches: '(fedmich|fedche)\.com' },
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
documentation is here https://developer.chrome.com/extensions/events#property-UrlFilter-hostEquals
The regular expressions use the RE2 syntax.
https://code.google.com/p/re2/wiki/Syntax

In https://developer.chrome.com/extensions/events#property-UrlFilter-hostEquals it describes urlMatches case.

Related

chrome declarativeNetRequest append matched url not working

I'm trying to append the matched domain from a declarativeNetRequest rule to the redirect extension page, but I can't seem to be able to get it to work. The redirect is working to my extension page but the matched URL isn't appended.
Here is my code snippet:
const page = chrome.runtime.getURL('/MyPage.html');
const RULES = [
{
'id': 1,
'priority': 2,
action: {type: 'redirect', redirect: {regexSubstitution: page + '#\\0', extensionPath: '/MyPage.html'}},
'condition': {
regexFilter: "\w*",
requestDomains: ["amazon.com"]
}
}]
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: RULES.map(r => r.id),
addRules: RULES,
});
Updated code snippet:
const page = chrome.runtime.getURL('/MyPage.html');
const RULES = [
{
id: 1,
priority: 2,
action: {type: 'redirect', redirect: {regexSubstitution: page + '#\\1' }},
condition: {
regexFilter: "https://([^/]+)",
requestDomains: ["amazon.com"]
}
},
];
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: RULES.map(r => r.id),
addRules: RULES,
});
Remove , extensionPath: '/MyPage.html' as you already have a substitution
Replace \w* with ://([^/]+) to capture dots and dashes in the domain name, also note that inside regexp strings you need to use an escaped backslash \\ not \ while there's no need to escape the forward slash /.
replace #\\0 with #\\1 to get the parenthesized group of the above regexp.

Chrome page action not open popup on click

I have this code to activate an extension only if a certain website is visited. I've noticed that the extension icon will be always clickable and will be not grey if the url isn't mathcing with the condition setted and when the desired website is visited and the url match, if the user click on the extension icon, the popup will not be opened. How I can fix?
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([
{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'www.example.com/video/*', schemes: ["https"] },
})
],
actions: [ new chrome.declarativeContent.ShowPageAction() ]
}
]);
});
});
chrome.pageAction.onClicked.addListener( () => {
chrome.windows.create({
url: chrome.runtime.getURL('popup.html'),
width: 500,
height: 295,
type: 'popup'
});
});
Your rule for hostEquals will never match anything because per the documentation it's compared against the host part of a URL e.g. simply www.example.com so it can't have / or *. Note that chrome.declarativeContent uses its own filtering system, it does not support any of the usual matching patterns used by content_scripts or webRequest.
Solution 1:
{ hostEquals: 'www.example.com', pathPrefix: '/video/', schemes: ['https'] }
Solution 2:
{ urlPrefix: 'https://www.example.com/video/' }

xml2js valueProcessor removing \t and \n

I have a problem with parsing an XML file.
I want to remove strings with characters like \t\n.
XML File: http://ftp.thinkimmo.com/home/immoanzeigen24/immo.xml
{
trim: true,
normalize: true,
attrValueProcessors: [cleanValue, name => name],
valueProcessors: [cleanValue, name => name]
}
cleanValue:
const cleanValue = value => {
return value.toString().trim().replace("\t","atest");
};
I tried cleaning it with a lot of regex I've found online - but value always stays like following:
"verwaltung_objekt": {
"objektadresse_freigeben": "0",
"verfuegbar_ab": "nachaasjkdhkjshadjkashdAbsprache",
"bisdatum": "2016-01-15",
"min_mietdauer": "\n\t\t\t\t",
"max_mietdauer": "\n\t\t\t\t",
}
This is a difficult one!
I'd suggest following a simple strategy and pre-processing the xml data before you parse it.
This should resolve your issue at least.
If you just do something like:
function trimXml(xml) {
return xml.replace(/>\s+</g, "><");
}
xml = trimXml(xml);
Then parse the trimmed xml data. You should see the output now looks like so:
"verwaltung_objekt": [
{
"objektadresse_freigeben": [
"1"
],
"abdatum": [
"2017-03-01"
],
"min_mietdauer": [
""
],
"max_mietdauer": [
""
]
}
],
Which is a bit more like what you want!

How can I build a compound query with SearchBox in searchkit?

I'm using searchkit to try to build a basic text search. I think the query I want to build is fairly simple. It needs to be structured like this:
{
"query":{
"bool":{
"must":[
{
"multi_match":{
"query":"test search",
"type":"phrase_prefix",
"fields":[
"field_1^5",
"field_2^4",
"field_3"
]
}
},
{
"term":
{
"field_id": "3"
}
}
],
"must_not":[
{
"term":
{
"status": "archived"
}
}
]
}
},
"size":6,
"highlight":{
"fields":{
"field_1":{},
"field_2":{},
"field_3":{}
}
}
}
I've tried using the prefixQueryFields attribute, which gave me something fairly close to what I wanted except it was using a BoolShould rather than a BoolMust, plus it was always including the default SimpleQueryString query. It looked something like this:
const prefixQueryFields = [
'field_1^5',
'field_2^4',
'field_3',
];
...
<SearchBox
searchOnChange={true}
prefixQueryFields={prefixQueryFields}
/>
I couldn't figure out the issues there easily and decided to go with the queryBuilder attribute in SearchBox. This is what I came up with:
_queryBuilder(queryString) {
const prefixQueryFields = [
'field_1^5',
'field_2^4',
'field_3',
];
return new ImmutableQuery()
.addQuery(BoolMust([
MultiMatchQuery(queryString, {
type: 'phase_prefix',
fields: prefixQueryFields,
})
]))
.addQuery(BoolMustNot([
TermQuery('status', 'archived'),
]));
}
...
<SearchBox
searchOnChange={true}
queryBuilder={this.queryBuilder}
/>
This query came out even more messed up, and I have no idea what to try next after checking the documentation and a cursory look at the source code.
(For the sake of brevity, I will not bother including the incorrect queries these two attempts created unless someone thinks that info will be useful.)
Figured it out. Using the QueryDSL structures wasn't working out very well, but apparently you can create the query with pure JSON, which worked great. Basically updated my query builder to return as so:
return {
bool: {
must: [
{
multi_match:{
query: queryString,
type: 'phrase_prefix',
fields: prefixQueryFields,
}
}
],
must_not: [
{
term: {
status: 'archived',
}
}
]
}
};

async ajax within background.js of chrome extension

this is my background.js
chrome.runtime.onInstalled.addListener(function() {
chrome.declarativeContent.onPageChanged.removeRules(undefined, function() {
chrome.declarativeContent.onPageChanged.addRules([{
conditions: [
// When a page contains a <video> tag...
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'www.youtube.com'}
})
],
// ... show the page action.
actions: [new chrome.declarativeContent.ShowPageAction() ]
}]);
});
});
where should I insert my ajax? and how to use jquery? like $.post in my background.js?

Categories

Resources