Capture and Remove URL segment in custom JS - GTM - javascript

I need a custom JS variable for GTM that excludes everything from "/p/" until the end or the next "/", what happens first.
The url after the /p/ can be anything, any length, and have nothing afterwards.
Before:
hostname/category/brand/some-snicker-model/p/NIBQ5448001
hostname/campaign/some-snicker-model/p/AB434222/?device=type
After:
hostname/category/brand/some-snicker-model
hostname/campaign/some-snicker-model/?device=type

Something like that should works:
/\/p\/.*?(?=(\/|\?|$))/g
const data = ["hostname/category/brand/some-snicker-model/p/NIBQ5448001", "hostname/campaign/some-snicker-model/p/AB434222/?device=type"]
const regex = /\/p\/.*?(?=(\/|\?|$))/g;
data.forEach(url => {
console.log(url.replace(regex, ''))
})
// hostname/category/brand/some-snicker-model
// hostname/campaign/some-snicker-model/?device=type
Another better alternative would be to use URL(), as it allows to separate the url according to the origin, the path name, the search value, etc.
let data = [
'https://www.hostname.com/category/brand/some-snicker-model/p/NIBQ5448001',
'https://www.hostname.com/campaign/some-snicker-model/p/AB434222/?device=type',
'https://www.hostname.com/category/brand/some-snicker-model/p/NIBQ5448001?device=mobile'
];
let pattern = /\/p\/.*?(?=(\/|$))/g;
data.forEach(element => {
let url = new URL(element);
let new_url = url.origin + url.pathname.replace(pattern, '') + url.search
console.log(new_url)
});

Related

I need to allocate a url to very student name in Javascript

The name list is supposedly as below:
Rose : 35621548
Jack : 32658495
Lita : 63259547
Seth : 27956431
Cathy: 75821456
Given you have a variable as StudentCode that contains the list above (I think const will do! Like:
const StudentCode = {
[Jack]: [32658495],
[Rose]: [35621548],
[Lita]: [63259547],
[Seth]: [27956431],
[Cathy]:[75821456],
};
)
So here are the questions:
1st: Ho can I define them in URL below:
https://www.mylist.com/student=?StudentCode
So the link for example for Jack will be:
https://www.mylist.com/student=?32658495
The URL is imaginary. Don't click on it please.
2nd: By the way the overall list is above 800 people and I'm planning to save an external .js file to be called within the current code. So tell me about that too. Thanks a million
Given
const StudentCode = {
"Jack": "32658495",
"Rose": "35621548",
"Lita": "63259547",
"Seth": "27956431",
"Cathy": "75821456",
};
You can construct urls like:
const urls = Object.values(StudentCode).map((c) => `https://www.mylist.com?student=${c}`)
// urls: ['https://www.mylist.com?student=32658495', 'https://www.mylist.com?student=35621548', 'https://www.mylist.com?student=63259547', 'https://www.mylist.com?student=27956431', 'https://www.mylist.com?student=75821456']
To get the url for a specific student simply do:
const url = `https://www.mylist.com?student=${StudentCode["Jack"]}`
// url: 'https://www.mylist.com?student=32658495'
Not sure I understand your second question - 800 is a rather low number so will not be any performance issues with it if that is what you are asking?
The properties of the object (after the trailing comma is removed) can be looped through using a for-in loop, (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in)
This gives references to each key of the array and the value held in that key can be referenced using objectName[key], Thus you will loop through your object using something like:
for (key in StudentCode) {
keyString = key; // e.g = "Jack"
keyValue = StudentCode[key]; // e.g. = 32658495
// build the urls and links
}
to build the urls, string template literals will simplify the process (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) allowing you to substitute values in your string. e.g.:
url = `https://www.mylist.com/student=?${StudentCode[key]`}
Note the use of back ticks and ${} for the substitutions.
Lastly, to build active links, create an element and sets its innerHTML property to markup built using further string template literals:
let link = `<a href=${url}>${keyValue}</a>`
These steps are combined in the working snippet here:
const StudentCode = {
Jack: 32658495,
Rose: 35621548,
Lita: 63259547,
Seth: 27956431,
Cathy: 75821456,
};
const studentLinks = [];
for (key in StudentCode) {
let url = `https://www.mylist.com/student=?${StudentCode[key]}`;
console.log(url);
studentLinks.push(`<a href href="url">${key}</a>`)
}
let output= document.createElement('div');
output.innerHTML = studentLinks.join("<br>");
document.body.appendChild(output);

I want to fetch loginID and password from below code

const fs = require('fs');
// var fileRefer=new Array();
var fileRefer = fs.readFileSync('D:\\NgageAuto\\LoginID\\Creds.txt').toString().split("\n");
for(i in fileRefer) {
console.log(fileRefer[i]);
}
Ouput:- Date: 2021-11-08 16:56:42 LoginID: pvgA1245 Password: Root#123
it's one of the example which is in file i want LoginID value i.e "pvgA1245 and password value i.e Root#123
Please , help me how can i make it!!!
there are hundreds of solutions to this problem, some having advantages in different scenarios then others.
Here are some for using split() or using a regex match. Each either using destructuring or just "normally assign" the variables. Regex has the advantage, that it can be used more flexible for example if sometimes the line schema is different and Password is missing. But if you can be sure that the schema is always the same or just wanna skip lines that don't have all values, split() is totally fine.
You would just add the relevant code snippet inside your for loop
let i = "Date: 2021-11-08 16:56:42 LoginID: pvgA1245 Password: Root#123";
// split and destructure
const [ , , , , id, ,pw,] = i.split(" ");
console.log(id, pw);
// split and normally assign
const a = i.split(" ");
const id2 = a[4];
const pw2 = a[6];
console.log(id2, pw2);
// regex and destructure
const [, id3, pw3] = i.match(/(?:LoginID: ([^\s]*)) ?(?:Password: ([^\s]*))/);
console.log(id3, pw3);
// regex and normally assign
const m = i.match(/(?:LoginID: ([^\s]*)) ?(?:Password: ([^\s]*))/);
const id4 = m[1];
const pw4 = m[2];
console.log(id4, pw4);

How to get a param from the url?

I have a url like this:
http://localhost:3000/#/firstregistration?panel=4?codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262
I need to get the param called "codice" from the url of this page and use it in a query. I tried with this code:
render() {
const params = new URLSearchParams(this.props.location.search);
const codiceHash = params.get('codice');
console.log(params.get('codice'))
return (
<div className={styles}>
<div className="notification">
<h2>Prima Registrazione eseguita con successo</h2>
</div>
{this.saveEsegue(email, transactionHash , blockHash, now, "FR", codiceHash)}
</div>
)
}
But what I get back from the console.log is null.
What am i doing wrong?
Your URL is invalid. You cannot have # and then later two ? in it.
Your ?codice shoould be &codice
Here is one way to get at codice
const invalidHref = "http://localhost:3000/#/firstregistration?panel=4?codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262&somethingelse"
const codice = invalidHref.split("codice=")[1].split("&")[0];
console.log(codice)
Here is how it would have worked on a valid URL
const params = new URLSearchParams("http://localhost:3000/#/firstregistration?panel=4&codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262")
const codice = params.get("codice")
console.log(codice)
The parameters string isn't correct in the URL, but to get the string from what you've provided I'd use RegEx.
This way it doesn't matter where the codice parameter is in the URL (ie you can add more parameters without breaking it. RegEx will just pick it out.)
const url = "http://localhost:3000/#/firstregistration?panel=4?codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262"; // window.location.href;
const codice = url.match(/(codice=)([a-zA-Z0-9]*)/)[2];
console.log(codice) // prints fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262
I suggest you to use the module querystring to achieve that, this is one of the top used for this purpose.
Example:
console.log(this.props.location.search);
//=> '?foo=bar'
const parsed = queryString.parse(location.search);
console.log(parsed);
//=> {foo: 'bar'}
Since you only want the one parameter and you know which values it can hold I would use regex.
var r = /codice=([a-z0-9]+)&/g
var matches = r.exec('http://localhost:3000/#/firstregistration?panel=4?codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262')
console.log(matches[1])
>> fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262
The code snippet will return
codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262
change url_string to window.location.href to grab the current URL of the page
var url_string = "http://localhost:3000/#/firstregistration?panel=4?codice=fea023b0cb134b845d49a789a9149ab4321574fe093a5fceac1083959e26d262"; //window.location.href
var b = url_string.substring(url_string.indexOf("?codice=") + 1);
console.log(b);

Cheerio Not Parsing HTML Correctly

I've got an array of rows that I've parsed out of a table from html, stored in a list. Each of the rows in the list is a string that looks (something) like this:
["<td headers="DOCUMENT" class="t14data"><a target="6690-Exhibit-C-20190611-1" href="http://www.fara.gov/docs/6690-Exhibit-C-20190611-1.pdf" class="doj-analytics-processed"><span style="color:blue">Click Here </span></a></td><td headers="REGISTRATIONNUMBER" class="t14data">6690</td><td headers="REGISTRANTNAME" class="t14data">SKDKnickerbocker LLC</td><td headers="DOCUMENTTYPE" class="t14data">Exhibit C</td><td headers="STAMPED/RECEIVEDDATE" class="t14data">06/11/2019</td>","<td headers="DOCUMENT" class="t14data"><a target="5334-Supplemental-Statement-20190611-30" href="http://www.fara.gov/docs/5334-Supplemental-Statement-20190611-30.pdf" class="doj-analytics-processed"><span style="color:blue">Click Here </span></a></td><td headers="REGISTRATIONNUMBER" class="t14data">5334</td><td headers="REGISTRANTNAME" class="t14data">Commonwealth of Dominica Maritime Registry, Inc.</td><td headers="DOCUMENTTYPE" class="t14data">Supplemental Statement</td><td headers="STAMPED/RECEIVEDDATE" class="t14data">06/11/2019</td>"]
The code is pulled from the page with the following page.evaluate function using puppeteer.
I'd like to then parse this code with cheerio, which I find to be simpler and more understandable. However, when I pass each of the strings of html into cheerio, it fails to parse them correctly. Here's the current function I'm using:
let data = res.map((tr) => {
let $ = cheerio.load(tr);
const link = $("a").attr("href");
const number = $("td[headers='REGISTRATIONNUMBER']").text();
const name = $("td[headers='REGISTRANTNAME']").text();
const type = $("td[headers='DOCUMENTTYPE']").text();
const date = $("td[headers='STAMPED/RECEIVEDDATE']").text();
return { link, number, name, type, date };
});
For some reason, only the "a" tag is working correctly for each row. Meaning, the "link" variable is correctly defined, but none of the other ones are. When I use $("*") to return a list of what should be all of the td's, it returns an unusual node list:
What am I doing wrong, and how can I gain access to the td's with the various headers, and their text content? Thanks!
It usually looks more like this:
let data = res.map((i, tr) => {
const link = $(tr).find("a").attr("href");
const number = $(tr).find("td[headers='REGISTRATIONNUMBER']").text();
const name = $(tr).find("td[headers='REGISTRANTNAME']").text();
const type = $(tr).find("td[headers='DOCUMENTTYPE']").text();
const date = $(tr).find("td[headers='STAMPED/RECEIVEDDATE']").text();
return { link, number, name, type, date };
}).get();
Keep in mind that cheerio map has the arguments reversed from js map.
I found the solution. I'm simply returning the full html through puppeteer instead of trying to get individual rows, and then using the above suggestion (from #pguardiario) to parse the text:
const res = await page.evaluate(() => {
return document.body.innerHTML;
});
let $ = cheerio.load(res);
let trs = $(".t14Standard tbody tr.highlight-row");
let data = trs.map((i, tr) => {
const link = $(tr).find("a").attr("href");
const number = $(tr).find("td[headers='REGISTRATIONNUMBER']").text();
const registrant = $(tr).find("td[headers='REGISTRANTNAME']").text();
const type = $(tr).find("td[headers='DOCUMENTTYPE']").text();
const date = moment($(tr).find("td[headers='STAMPED/RECEIVEDDATE']").text()).valueOf().toString();
return { link, number, registrant, type, date };
});

Writing a query parser in javascript

I'm trying to write a parser that supports the following type of query clauses
from: A person
at: a specific company
location: The person's location
So a sample query would be like -
from:Alpha at:Procter And Gamble location:US
How do i write this generic parser in javascript. Also, I was considering including AND operators inside queries like
from:Alpha AND at:Procter And Gamble AND location:US
However, this would conflict with the criteria value in any of the fields (Procter And Gamble)
Use a character like ";" instead of AND and then call theses functions:
var query = 'from:Alpha;at:Procter And Gamble;location:US';
var result = query.split(';').map(v => v.split(':'));
console.log(result);
And then you'll have an array of pairs, which array[0] = prop name and array[1] = prop value
var query = 'from:Alpha;at:Procter And Gamble;location:US';
var result = query.split(';').map(v => v.split(':'));
console.log(result);
Asuming your query will always look like this from: at: location:
You can do this:
const regex = /from:\s*(.*?)\s*at:\s*(.*?)\s*location:\s*(.*)\s*/
const queryToObj = query => {
const [,from,at,location] = regex.exec(query)
return {from,at,location}
}
console.log(queryToObj("from:Alpha at Betaat: Procter And Gamble location: US"))
However, adding a terminator allow you to mix order and lowering some keywords:
const regex = /(\w+):\s*(.*?)\s*;/g
const queryToObj = query => {
const obj = {}
let temp
while(temp = regex.exec(query)){
let [,key,value] = temp
obj[key] = value
}
return obj
}
console.log(queryToObj("from:Alpha at Beta;at:Procter And Gamble;location:US;"))
console.log(queryToObj("at:Procter And Gamble;location:US;from:Alpha at Beta;"))
console.log(queryToObj("from:Alpha at Beta;"))

Categories

Resources