chrome declarativeNetRequest append matched url not working - javascript

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.

Related

Vue-router: get rid of trailing slash in the url

Setup: vue 2.6.14 & vue-router, my router config:
{
mode: 'history',
base: document.location.pathname,
routes: [
{
path: '/',
name: 'main',
component: Main,
props: (route) => {
return {
id: route.query.id,
};
},
pathToRegexpOptions: {
strict: true,
},
},
],
}
Problem: I have urls like this
local.host/page/xxx_111.html?id=111
The router base is /page/xxx_111.html path. At some point I would like to change the url via programmatic navigation to just
local.host/page/xxx_111.html
in other words, to get rid of query params, but whatever I do, I always get
local.host/page/xxx_111.html/
with trailing slash :( How to solve this?
Solved by making router's path: '/*([\\w\\-]+_?[\\d]+.html)' which matches any url of type local.host/<ANYTHING>/xxx_111.html and calls like this.$router.push({ query: {} }) to get rid of url param w/o adding trailing slash to the url.

replacing path using route object in nuxt

I want to go to this url:
myApp.com/search-page?name%5Bquery%5D=value
and the following code works well for it when I'm on the home page myApp.com:
this.$router.push({
path: "search-page",
query: { name: { query: `${this.value}` } }
});
But if I'm on a different page that has a path already like:
myApp.com/movies/id
when I use the same code with $router.push it takes me to this url:
myApp.com/movies/search-page?name%5Bquery%5D=value
As you can see there is an extra movies/ in the URL which we don't want! How can I make it so no matter what my current path (URL) is, my $router.push method takes me to:
myApp.com/search-page?name%5Bquery%5D=value
You are using path in $router.push. If you are in a nested page you also need to change the path value.
Instead use name so you don't have to specify path levels
this.$router.push({
name: "search-page",
query: { name: { query: `${this.value}` } }
});
In nuxt, the route names are basically the folder name generated to lower string and hyphens. So if you have folder like this and you want to access movies id route
pages/
--| movies
-----| _id.vue
You can access it by
this.$router.push({
name: "movies-id",
params: {
id: 1
}
});

Telegraf: unable to use Markdown style in reply with inlineButtons

The reply allowed html style with inline button but unfortunately it is not possible to use markdown style with inline button.
const inlineButtons = () => {
const inlineLinks = [
{
title: 'Google',
link: 'https://www.google.com/',
},
{
title: 'DuckDuckGo.com',
link: 'https://www.duckduckgo.com/',
},
];
const buttonLinks = inlineLinks.map(({ title, link }) =>
Markup.markdown().urlButton(title, link),
);
return Extra.markup(m => m.inlineKeyboard(buttonLinks, { columns: 1 }));
};
// Reply message
ctx.reply(
`<b>show_inline</b> *${show_inline}*`,
show_inline ? inlineButtons()
);
With the current code there is no style with in the message
There's Extra.markdown() and Extra.HTML()
Both ctx.reply() and ctx.replyWithHTML() works, the key point is the Extra.< Something >.markup
Try not mix replyWithHTML() with Extra.markdown() // Doesn't make sense
ANS:
ctx.replyWithHTML(
"<b>show_inline</b>",
Extra.HTML().markup(m =>
m.inlineKeyboard([
m.callbackButton("Single", "single"),
m.callbackButton("Range", "range")
])
)
);
Got my idea from https://github.com/telegraf/telegraf/issues/443
Edit:
For markdown, a single _ is invalid
<b>show_inline\</b> *${show_inline}*
Use Escape \\:
<b>show\\_inline</b> *${show_inline}*
Markup doesn't have a function called markdown()
(I use TS to check the functions they have)
I don't think you can style the inline keyboard text

Codemirror Simple Mode - Regex lookbehind and lookahead does not work

I just implemented a simple mode to recognize latex code.
Here is an code example that could be highlighted:
\documentclass{test}
There should be everything blue, except of the 'test' what should be purple. This is how my implementation looks like atm:
const CodeMirror = require('codemirror')
CodeMirror.defineSimpleMode("simplemode", {
start: [
{
regex: /(?<=\{).+?(?=\})/,
token: 'argument'
},
{
regex: /%.*/,
token: 'comment'
},
/*{
regex: /\\.*{.*}/,
token: 'tag'
},*/
{
regex: /\$.*\$/,
token: 'math'
}
],
meta: {
dontIndentStates: [],
lineComment: '%'
}
})
I removed the tag part, because I thought it would overlap with the tag. Anyways, even if all regexes match perfect in regex testers for javascript, the lookahead and lookbehind do not work.
Is there any workaround, fix, mistake?

Block URL with a specific word somewhere in the subdomain

I am trying to make a Chrome extension that blocks URLs with a specific word in the subdomain, but not other URLs in that domain. For example, let's say I want to block all tumblr blogs with the word "cola" in the subdomain.
It should be able to block this page: http://coca-cola.tumblr.com/.
I have tried to use this url match: urls:["*://*cola*.tumblr.com/*"], but it is not working. And I cannot think of any other combinations that might work. Can somebody point me in the right direction?
This is my full background.js:
chrome.webRequest.onBeforeRequest.addListener(
function() {
return {cancel: true };
},
{
urls:["*://*cola*.tumblr.com/*"] // This is the part I'm messing up.
},
["blocking"]
);
Your code fails because *://*cola*.tumblr.com/* is not a valid match pattern. Wildcards can only be used in the path component of an URL, or at the start of a host name.
If you want to block an URL whose subdomain contains some keyword, you need to match the whole domain, and use JavaScript to check whether the subdomain contains the profane word.
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var hostname = details.url.split('/', 3)[2];
return {
cancel: hostname.indexOf('cola') >= 0
};
},
{
urls:["*://*.tumblr.com/*"]
},
["blocking"]
);
Or using the chrome.declarativeWebRequest API (omitted chrome.runtime.onInstalled event for brevity):
chrome.declarativeWebRequest.onRequest.addRules({
id: 'some rule id',
conditions: [
new chrome.declarativeWebRequest.RequestMatcher({
url: {
hostContains: 'cola',
hostSuffix: '.tumblr.com'
}
})
],
actions: [
new chrome.declarativeWebRequest.CancelRequest()
]
});

Categories

Resources