Unable to decode QRCode with qrcode-reader NodeJS - javascript

I am using the following code to decode qrcodes using the qrcode-reader library. I am able to decode simple qrcodes that I have generated from various sites. However, I am unable to decode more complex qrcodes like the attached and receive "Couldn't find enough alignment patterns". I am able to decode this qrcode with a free Android app. What do I need to do to decode this qrcode?
it.only('test QrCode', async () => {
await Promise.resolve();
const buffer = fs.readFileSync(`${__dirname }/test.png`);
const image = await Jimp.read(buffer);
const qrcode = new QrCode();
qrcode.callback = function(err, value) {
if (err) {
console.error(`err: ${err}`);
} else {
console.log(`value: ${JSON.stringify(value)}`);
}
};
qrcode.decode(image.bitmap);
});
Edit:
The contents of the qrcode is in a compressed format using jszip. This is what I was able to decode using a random Android qrcode reader app from the play store
PK
ŠiÞRØ)E è Ù certificate.json Tioê: ý+(Ÿ f Jùr „²ïa zª&‰ À¦Ž
Uÿû ·´Rß"E‘<s<Ë™3~×þð8“äMj•?µ ”‡¸òðp< óG+ÏEø`êFùÁ Ä'LRØÅ ©¡e? ?R– yš§ì;
< 2 ”3u㯬&O ¢r̉ wGìO<F ΃a0¿ßûâÆë4Ž "´ÊÏ©µ+ ˜Gê 1 fꦑÓK9KŸê…Š®ã—×u} HÆÙT ‹ "T –"!è¸7à$nD<ääýV·6""æLåññäS¿rËžP h Àß ˆŠi ð³,«€PA‚¶B h*©Ä°W¡j 8A¦O¥ 4†„ù—Æú°SE#x)ÿñ‚W<ÀŽÊ šÚ̧ * ß $ŽUq± „ÈêoƒfèFÎÐÍl¦ ,„]¦ o¨8‚ ·¾AMÄ:™yfÂ/>Ÿ¢—ª~5 ˜ O$çÌ‚ÄR‘rÍÞOö.Ð{ö ±¶KÙ o®Ýܼ6O˜ —ª h=ðXÂÎæ¾j­€c( ´ ¬FRŠ„{J ïWb œîULDM9 ÄwÁÛÎÄîŸÐ¿Ô XÀÿ ò©Ç»â”Î\ ÞFµUÃI–kKDÞÒ£ÑæsxCË X €' q™œ=œ „½"À¿ËO/Y:ZH ”hJ B^}º ¿¯¾ Ênwõ o>ŸÇ*’Z7É‘¾:žã›!½,‘Jþþ[U ç ¢ŒCY¸Ñ [¬ ^•ó ©ÖìLŸøÔCeØ(t¼ÿïj2­l¦‹a÷ 5ÁO*ú®ž QÀD$ ‡'rs—ϧã uA ÿ°ýŸúAå|à êÁø² “ b Šz´®ûŒÓð¿¿ †U1ÌŠyy ®4z—á÷‰Üðûnã®i·$£D ®³€8&â 8«EGE 9u6nÓ£CÚ ÏÎmc#Ûq{ß6 v»´Þ7bÏœáyp‚å˜ w1]E+½½3žòù™ÿÊW½·5xƒÂs¹S{êO·ërgÛë.K,4ç ólñº<V“`î%ro "k±~zõº¹z=b²XöŸÓ—£áÛ, §¡ûÂÚõ {® ]½Ô,Ÿ[ËY¡ TWæK# 3½t '6 .»U$·®{¸Fnk;
å\‹wžÜ·×u,JƒEd aµ1Z yîÐêy;KzFÐhºQ
|v
çi59®h¹Yï 7S§ž¶‹§Õ㢠¥ÕÜ3G‹ÒÒZøµpäå^C äc:éMKåç¨4›öÂck¥—É&‰ AIÚ³úÀ 4ƒiÐnÉ Oìí0,ZüÅ}z,ÏíÖÉy$‹åv<öÃãºÉ ¦;?E sd4'
; ‡NÍq¬Ð¬ ’¥áô"˜ 'c”ÈßPK
ŠiÞRØ)E è Ù certificate.jsonPK >

Related

Scrape JS site with NodeJS, Nightmare and Cheerio

I am trying to scrape a website to get the scores of each team. I am running into an issue where my script is returning null content. I cannot see where I am going wrong and looking for some help.
JS:
const Nightmare = require('nightmare')
const cheerio = require('cheerio');
const fs = require('fs');
const nightmare = Nightmare({ show: true })
const url = 'https://www.mscl.org/live/scorecard/ed7941919f69b0e11e800fef/mHcehsPR9S86T3zQv';
nightmare
.goto(url)
.wait('body')
.wait('div#summaryTab.tab-pane.fade.in.table-responsive.borderless.active')
.evaluate(() => document.querySelector('div.col-md-6').innerHTML)
.end()
.then(response => {
console.log(getData(response));
}).catch(err => {
console.log(err);
});
let getData = html => {
data = [];
const $ = cheerio.load(html);
$('div').each((i, elem) => {
if(i === 0 ){
console.log($(elem).find('nth-child(1)').html());
}
});
return data;
}
The html I am scraping is here.
https://pastebin.com/R6syWDwD
The line where the scores are: 30 and 32
<div class="col-md-6">
<b>40 Overs Match</b><br>
<b>MVCC Combined</b> won the toss and chose Batting<br>
<b>Umpires: </b>No umpires were selected<br>
<b>Date: </b> 3/24/2021, 5:00:00 PM<br>
<b>Ground: </b>Acton Field 1<br>
<b>Result: TBD</b><br>
<b>MoM: </b> <br>
<hr>
<p><b>MVCC COMBINED XI - 147/10</b> (<b>O:</b> 12.5 | <b>RR:</b> 11.45)</p>
<p><b>MVCC United XI - 23/1</b> (<b>O:</b> 2.0 | <b>RR:</b> 11.50)</p>
<hr>
</div>
When I run this it returns nothing. No errors are being displayed either. What am i missing?
The jQuery Docs for nth-child says
jQuery's implementation of :nth- selectors is strictly derived from the CSS specification
So you propably have to provide an element to your nth-child(1)-pseudo-selector, to tell jQuery from which element it should select the nth-child of. try this:
console.log($(elem).find('b:nth-child(1)').html());
alternatively, just try to prefix nth-child(1) with a colon -> :nth-child(1)
Edit:
I just realized you are using innerHTML on your selected div, which actually returns the contents of the div without the wrapping div itself. But in getData you try to select the div with $('div') which then is actually not found.

I can't decode a url encoded in unknow format on an ASP site

I'm scraping through a website and this website has a form the form is in the Arabic language so it's encoding everything (inputs) to let the form get work properly
MY problem is I'm scraping using JS and decodeUri/decodeUriComponent not working with the encoding output string that coming from the site.
even the browser says unable to decode value in console dev
the link example that I need to get it decoded:
http://app2.helwan.edu.eg/HelwanNat/Education/TermAlist.asp?x_level=2020%2D2019&z_level=LIKE&x_dep=%C7%E1%DA%E1%E6%E3+%2D%CA%DA%E1%ED%E3+%C7%D3%C7%D3%EC&z_dep=LIKE&x_st_name=%E3%CD%E3%CF&z_st_name=LIKE
This appears to be a windows-1256 encoding.
You can decode it in js like so:
function decode(string) {
var array = [...string.matchAll(/%(.{2})/g)].map((groups) => parseInt(groups[1], 16));
var decoder = new TextDecoder('windows-1256');
return decoder.decode(Uint8Array.from(array).buffer);
}
console.log(decode('%E3%CD%E3%CF'));
console.log('%C7%E1%DA%E1%E6%E3+%2D%CA%DA%E1%ED%E3+%C7%D3%C7%D3%EC'.split('+').map(decode));
Since TextEncoder() can only encode utf-8 strings, you will have to make a conversion table for windows-1256:
windows_1256 = [
'\u0000', '\u0001', '\u0002', '\u0003', '\u0004', '\u0005', '\u0006', '\u0007', '\u0008', '\u0009', '\u000A', '\u000B', '\u000C', '\u000D', '\u000E', '\u000F',
'\u0010', '\u0011', '\u0012', '\u0013', '\u0014', '\u0015', '\u0016', '\u0017', '\u0018', '\u0019', '\u001A', '\u001B', '\u001C', '\u001D', '\u001E', '\u001F',
'\u0020', '\u0021', '\u0022', '\u0023', '\u0024', '\u0025', '\u0026', '\u0027', '\u0028', '\u0029', '\u002A', '\u002B', '\u002C', '\u002D', '\u002E', '\u002F',
'\u0030', '\u0031', '\u0032', '\u0033', '\u0034', '\u0035', '\u0036', '\u0037', '\u0038', '\u0039', '\u003A', '\u003B', '\u003C', '\u003D', '\u003E', '\u003F',
'\u0040', '\u0041', '\u0042', '\u0043', '\u0044', '\u0045', '\u0046', '\u0047', '\u0048', '\u0049', '\u004A', '\u004B', '\u004C', '\u004D', '\u004E', '\u004F',
'\u0050', '\u0051', '\u0052', '\u0053', '\u0054', '\u0055', '\u0056', '\u0057', '\u0058', '\u0059', '\u005A', '\u005B', '\u005C', '\u005D', '\u005E', '\u005F',
'\u0060', '\u0061', '\u0062', '\u0063', '\u0064', '\u0065', '\u0066', '\u0067', '\u0068', '\u0069', '\u006A', '\u006B', '\u006C', '\u006D', '\u006E', '\u006F',
'\u0070', '\u0071', '\u0072', '\u0073', '\u0074', '\u0075', '\u0076', '\u0077', '\u0078', '\u0079', '\u007A', '\u007B', '\u007C', '\u007D', '\u007E', '\u007F',
'\u20AC', '\u067E', '\u201A', '\u0192', '\u201E', '\u2026', '\u2020', '\u2021', '\u02C6', '\u2030', '\u0679', '\u2039', '\u0152', '\u0686', '\u0698', '\u0688',
'\u06AF', '\u2018', '\u2019', '\u201C', '\u201D', '\u2022', '\u2013', '\u2014', '\u06A9', '\u2122', '\u0691', '\u203A', '\u0153', '\u200C', '\u200D', '\u06BA',
'\u00A0', '\u060C', '\u00A2', '\u00A3', '\u00A4', '\u00A5', '\u00A6', '\u00A7', '\u00A8', '\u00A9', '\u06BE', '\u00AB', '\u00AC', '\u00AD', '\u00AE', '\u00AF',
'\u00B0', '\u00B1', '\u00B2', '\u00B3', '\u00B4', '\u00B5', '\u00B6', '\u00B7', '\u00B8', '\u00B9', '\u061B', '\u00BB', '\u00BC', '\u00BD', '\u00BE', '\u061F',
'\u06C1', '\u0621', '\u0622', '\u0623', '\u0624', '\u0625', '\u0626', '\u0627', '\u0628', '\u0629', '\u062A', '\u062B', '\u062C', '\u062D', '\u062E', '\u062F',
'\u0630', '\u0631', '\u0632', '\u0633', '\u0634', '\u0635', '\u0636', '\u00D7', '\u0637', '\u0638', '\u0639', '\u063A', '\u0640', '\u0641', '\u0642', '\u0643',
'\u00E0', '\u0644', '\u00E2', '\u0645', '\u0646', '\u0647', '\u0648', '\u00E7', '\u00E8', '\u00E9', '\u00EA', '\u00EB', '\u0649', '\u064A', '\u00EE', '\u00EF',
'\u064B', '\u064C', '\u064D', '\u064E', '\u00F4', '\u064F', '\u0650', '\u00F7', '\u0651', '\u00F9', '\u0652', '\u00FB', '\u00FC', '\u200E', '\u200F', '\u06D2'
];
function encode(string){
return [...string].map(c => `%${windows_1256.indexOf(c).toString(16).toUpperCase()}`).join('');
}
console.log(encode('محمد'));
console.log(['العلوم', "-تعليم", "اساسى"].map(encode).join('+'));

CRC-16-IBM implementation (JS) not working

Background
I receive a buffer of bytes that has a CRC check at the end. According to the manual, this is the CRC-16-IBM algorithm with the polynomial 0xA001 ( reversed as seen in Wikipedia ).
Research
To implement this I translated code from several SO questions from Java to C# into JS but nothing seems to work and I have no idea why.
Code
Here is the implementation I am currently dealing with:
const crc16ibm = buffer => {
const table = [
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
];
let crc = 0x0000;
for( const byte of buffer ){
crc = crc >>> 8 ^ table[ ( crc ^ byte ) & 0xff ];
}
return crc;
};
module.exports = crc16ibm;
Here is a test that is supposed to work:
const hexStr = Buffer.from( "000000000000001C0D01050000001056E924222347455420444154414F524445520D0A01", "hex");
const expectedCrc = 0x00004990;
assert( crc16ibm( hexStr ), expectedCrc ); //NOPE!
What is wrong?
I have quite literally tried everything, but either the examples I am using to test my code are wrong, or I have no idea on what's going on. At this point I weight both equally.
Is the problem with the lookup table?
The manual provides a graphic with an image of a ( quite inefficient ) possible way to implement this, via repeated for loops instead of using a table:
But I would much rather use a table and find out what is actually wrong in my current algorithm...
There's nothing wrong with your table or code to compute the CRC described in the flow chart. There must be something wrong with your test vector.
I did this and it worked
const crc16ibm = buffer => {
const table = [
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
];
let crc = 0x0000;
for( const byte of buffer ){
crc = crc >>> 8 ^ table[ ( crc ^ byte ) & 0xff ];
}
return crc;
};
module.exports.crc16ibm = crc16ibm;
This is how yo can try it
/*
const crc16ibm= require("./crc16ibm").crc16ibm; console.log((crc16ibm(Buffer.from('0C010500000007676574696E666F01','hex'))).toString(16));
*/

Return Youtube video URL from keyword search with Javascript

Alright , so I'm making a bot in Discord for my server, and something I wanted to implement is a youtube command.
I've been searching all over and looking in the Youtube API and all I can find is their search for what seems like a browser
I'm using nodejs to run it off of my laptop, and my bot runs off of discord.jsI have a similar command that does a MAL and a Urban Dictionary search, but I've found nothing and have no idea how to do the same with youtube
I used to have a command for a python bot that was able to due this, and I've seen other Discord bots able to do it as well, so I know it's obviously possible
Basically what I'm saying is I need to be able to search for and return a youtube video URL (the first search result) from a string of search terms so the usage would look like
>>youtube Tunak Tunak Tun
Would return
https://www.youtube.com/watch?v=vTIIMJ9tUc8 , which is the first search result for that keyword(s)
EDIT:I've found the python command that would do this, but have nigh the skills nor the confidence to try to translate this to JavaScript
elif prefix and cmd=="youtube" and len(args) > 0:
try:
yword=args.replace(" ","_")
ydata= urlreq.urlopen("http://gdata.youtube.com/feeds/api/videos?vq="+yword+"&racy=include&orderby=relevance&max-results=1")
yread= str(ydata.read())
if "<openSearch:totalResults>0</openSearch:totalResults>" in yread:
room.message("I got nothin' for ya by the name of "+args)
else:
trash , yclean=yread.split("<media:player url='http://www.youtube.com/watch?v=",1)
yclean , trash=yclean.split("&",1)
room.message("http://http://www.youtube.com/watch?v="+yclean,True)
except:
room.message("Somethin ain't right")
EDIT2 (Apologies for length) : Alright! I've found something that's gotten me a lot closer!
https://www.npmjs.com/package/youtube-search
I've got a command in my bot now that goes something like this:
if (commandIs("yt" , message)){
search(args.join(' ').substring(4), opts, function(err, results) {
if(err) return console.log(err);
message.channel.sendMessage(results);
console.log(results);
});
}
So now when I enter >>yt Tunak Tunak Tun I get
[ { id: 'vTIIMJ9tUc8',
link: 'https://www.youtube.com/watch?v=vTIIMJ9tUc8',
kind: 'youtube#video',
publishedAt: '2014-03-21T07:00:01.000Z',
channelId: 'UC3MLnJtqc_phABBriLRhtgQ',
channelTitle: 'SonyMusicIndiaVEVO',
title: 'Daler Mehndi - Tunak Tunak Tun Video',
description: 'Presenting \'Tunak Tunak Tun\' music video sung by the talented Daler Mehndi Song Name - Tunak Tunak Tun Album - Tunak Tunak Tun Singer - Daler Mehndi ...',
thumbnails: { default: [Object], medium: [Object], high: [Object] } } ]
in the console and [object Object] in the discord channel.
http://i.imgur.com/Vorpn0f.png
So the problem now is I have the link in my reach, but I can not get it to return JUST the link, and I've no idea how to pull it out of that mess.
It sounds like your results object is a JSON string. This essentially means that it is a string representation of a javascript object. You can parse this into an object by using JSON.parse().
var objResults = JSON.parse(results);
console.log(objResults);
console.log(objResults.link);
EDIT
Failed to notice that your result is actually an array. You just need to access it like this: console.log(results[0].link). Shouldn't need to JSON.parse()
Okay, here's another approach that is working for me, using the google javascript API. Once again, the SO snippet doesn't run it, so I'll link you to the fiddle.
This method requires you to setup a google API key, then enable youtube API access.
I've removed my google API key from the fiddle, so you'll need to set that up. I can PM you mine if you want to test first though.
var apiKey = null //put your API key here
function search() {
var searchTerm = $('#txtSearch').val()
gapi.client.init({
'apiKey': apiKey,
'discoveryDocs': ['https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest']
}).then(function() {
return gapi.client.youtube.search.list({
q: searchTerm,
part: 'snippet'
});
}).then(function(response) {
var searchResult = response.result;
$('#search-results').append(JSON.stringify(searchResult, null, 4))
console.log(searchResult.items[0])
var firstVideo = searchResult.items[0]
firstVideo.url = `https://youtube.com/watch?v=${firstVideo.id.videoId}`
$('#first-video').text(firstVideo.url).attr('href', firstVideo.url)
$('#first-video-title').text(firstVideo.snippet.title)
$('#first-video-description').text(firstVideo.snippet.description)
});
}
$('#btnSearch').on('click', function() {
$('#first-video-title').text("")
if (!apiKey) {
$('#first-video-title').text("You need to set an apiKey!")
return;
}
gapi.load('client', search)
});
#search-results { white-space: pre; font-family: monospace; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src='https://apis.google.com/js/api.js'></script>
<div id="container">
<input id="txtSearch" type="text" />
<button id="btnSearch">
Search!
</button>
<br />
<p id='first-video-title'> </p>
<p id='first-video-description'></p>
<a target="_blank" id="first-video"></a>
<div id='search-results'>
</div>
</div>

Looking up Address field on Custom Entity in CRM 2011

I have an entity called Customer Site from which I store Site information. From this site I create devices (another custom entity). I have created relevent address fields to those of the address entity in my customer site entity and I am trying to (based on an account) fill my site fields with those of a selected address from the account.
I referred to this article for doing so, http://xrmexpertz.com/2012/01/24/lookup-address-for-custom-entities-in-crm-2011/
I've altered the javascript and xml provided in the link to reflect my entities and my web resource.
My problem is that when i click the button that should execute the JavaScript it does nothing, and if I try to save a site without a account it sends me and error.
this is my java script.
function CustomLookup() {
'use strict';
var aoItems = getFieldValue("kez_SiteInfo_AccountId");
if (aoItems == null) {
alert(“Account is not Selected”);
return;
}
var _object = openStdDlg(“ / sfa / quotes / dlg_lookupaddress.aspx ? headerForm = 1 & parentType = 1 & parentId = ” + aoItems[0].id + “ & willCall = 0″, “LookupAddress”, 500, 330, true);
if (object) {
setFieldValue(“kez_address1_name”, object.Address.Name);
setFieldValue(“kez_address1_line1″, object.Address.Line1);
setFieldValue(“kez_address1_line2″, object.Address.Line2);
setFieldValue(“kez_address1_line3″, object.Address.Line3);
setFieldValue(“kez_address1_city”, object.Address.City);
setFieldValue(“kez_address1_province”, object.Address.StateOrProvince);
setFieldValue(“kez_address1_postalcode”, object.Address.PostalCode);
setFieldValue(“kez_address1_country”, object.Address.Country);
}
}
function setFieldValue(fieldName, fieldValue) {
Xrm.Page.getAttribute(fieldName).setValue(fieldValue);
}
This is the error is gives
Microsoft Dynamics CRM Error Report Contents
<CrmScriptErrorReport>
<ReportVersion>1.0</ReportVersion>
<ScriptErrorDetails>
<Message>Uncaught SyntaxError: Unexpected token ILLEGAL</Message>
<Line>7</Line>
<URL>/%7B634962800260003236%7D/WebResources/kez_GetLocation</URL>
<PageURL>/userdefined/edit.aspx?_gridType=10018&etc=10018&id=%7b1F8E02F0-766C-E211-934E- 00155D018211%7d&pagemode=iframe&preloadcache=1360684076423&rskey=69426415</PageURL>
<Function></Function>
<CallStack>
</CallStack>
</ScriptErrorDetails>
<ClientInformation>
<BrowserUserAgent>Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.30 (KHTML, like Gecko) Chrome/26.0.1403.0 Safari/537.30</BrowserUserAgent>
<BrowserLanguage>undefined</BrowserLanguage>
<SystemLanguage>undefined</SystemLanguage>
<UserLanguage>undefined</UserLanguage>
<ScreenResolution>1366x768</ScreenResolution>
<ClientName>Web</ClientName>
<ClientTime>2013-02-12T10:47:58</ClientTime>
</ClientInformation>
<ServerInformation>
<OrgLanguage>1033</OrgLanguage>
<OrgCulture>1033</OrgCulture>
<UserLanguage>1033</UserLanguage>
<UserCulture>1033</UserCulture>
<OrgID>{BC278BC1-EEEA-4D24-B5C6-F0720B343A1F}</OrgID>
<UserID>{D76EEA89-D760-E211-921F-00155D018211}</UserID>
<CRMVersion>5.0.9690.3236</CRMVersion>
</ServerInformation>
</CrmScriptErrorReport>
I ended up solving the problem, for one the source i listed uses quotes not recognized by CRM. Next I had to use XRM.getAttribute instead of getFieldValue.
The final and working code is below:
function CustomLookup() {
'use strict';
var aoItems = Xrm.Page.getAttribute('kez_siteinfo_accountid').getValue();
if (aoItems == null) {
alert("Account is not Selected");
return;
}
var _object = openStdDlg("/sfa/quotes/dlg_lookupaddress.aspx?headerForm=1&parentType=1&parentId=" + aoItems[0].id + "&willCall=0", "LookupAddress", 500, 330, true);
if (object) {
setFieldValue("kez_address1_name", object.Address.Name);
setFieldValue("kez_address1_line1", object.Address.Line1);
setFieldValue("kez_address1_line2", object.Address.Line2);
setFieldValue("kez_address1_line3", object.Address.Line3);
setFieldValue("kez_address1_city", object.Address.City);
setFieldValue("kez_address1_province", object.Address.StateOrProvince);
setFieldValue("kez_address1_postalcode", object.Address.PostalCode);
setFieldValue("kez_address1_country", object.Address.Country);
}
}
function setFieldValue(fieldName, fieldValue) {
Xrm.Page.getAttribute(fieldName).setValue(fieldValue);
}

Categories

Resources