Skip to content
Advertisement

User input always surrounded by double quotations in concatenated URL

I am developing a Chrome extension that, among other things, allows you to right-click selected text and search for that selected text in a new tab after it’s tested against some regular expression patterns.

In the below JavaScript code, the selected text is made into a string, then encoded as a component of a URI, then it is supposed to remove any double quotations from the beginning or end of the variable, but the double quotations are inexorably still in the URL of the newly created tab. I can’t figure out what is causing the quotations to show up in the end result in the first place, let alone why they are there even after the .replace method is used. I need there to be no quotations at all and only pass in the selected text into the URL. Please help!

var EncodedSelectedText = encodeURIComponent(JSON.stringify(info.selectionText));
var NoQuotes = EncodedSelectedText.replace(/^"|"$/g, '');

if (RITMRegExPattern.test(info.selectionText)) {
   chrome.tabs.create({
        url: 'https://redacted-site-name.com/sc_req_item_list.do? 
                 sysparm_query=numberLIKE'+NoQuotes
   });
}else if . . .

Resulting URL of the newly created Chrome tab looks like this:

https://redacted-site-name.com/sc_req_item_list.do? 
                 sysparm_query=numberLIKE"abc1234567"

but needs to look like this (without the quotations surrounding abc1234567):

https://redacted-site-name.com/sc_req_item_list.do? 
                 sysparm_query=numberLIKEabc1234567

Advertisement

Answer

Remove JSON.stringify and reload your extension. – wOxxOm

This fixed it. Thank you wOxxOm.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement