简体   繁体   English

从Applescript发送HTML到Javascript时,如何正确转义双引号?

[英]How do I properly escape double-quotes when sending HTML from Applescript into Javascript?

I am working on an Applescript script which executes some Javascript to add some HTML to a webpage I am viewing on my local machine. 我正在研究一个Applescript脚本,该脚本执行一些Javascript将一些HTML添加到要在本地计算机上查看的网页上。 Applescript is reading the HTML file into a string and automatically escapes all the double quotes in the HTML like this: \\" Applescript正在将HTML文件读取为字符串,并自动将HTML中的所有双引号转义,如下所示:\\“

But Javascript doesn't like the string with all the escaped double-quotes in it: 但是Javascript不喜欢其中包含所有转义双引号的字符串:

set programHTML to "\\"Hi kid!\\"" --This works! 将programHTML设置为“ \\” Hi kid!\\“”-这有效!

set programHTML to "\\"Hi \\"kid!\\"" --This doesn't! 将programHTML设置为“ \\” Hi \\“ kid!\\”“” -这不是!

I feel like I must be missing something very basic here. 我觉得这里一定缺少一些非常基本的东西。 I scanned and skimmed many posts but have not seen anything addressing this specific problem. 我扫描并浏览了许多帖子,但没有发现任何解决此特定问题的方法。

[Adding this after what would have been a helpful comment had I been a bit more explicit]: [如果我更明确一些,在有帮助的评论之后再添加]:

Nothing with more than the quotes on the end works when it gets to Javascript. 当到达Javascript时,最后没有引号。 This does not work: 这不起作用:

set programHTML to "\\"Howdy, \\"kid!\\", is your \\"Mom\\" home?\\"" 将programHTML设置为“ \\” Howdy,“孩子!\\”,您的“妈妈”是家吗?

I should perhaps mention that this string is going into a line which looks something like this: 我也许应该提一下,该字符串进入了看起来像这样的行:

execute tab 1 of front window javascript "codeDIV.innerHTML = " & programHTML & ";" 执行前窗javascript“ codeDIV.innerHTML =”&programHTML&“;”的标签1

Double Quotes must always be used in pair . 双引号必须始终成对使用 that's why your second method does not works. 这就是为什么第二种方法不起作用的原因。

set programHTML to "\\"Hi kid!\\"" --This works! 将programHTML设置为“ \\” Hi kid!\\“”-这有效!

-- Because it results in "Hi kid" - 2 double quotes -因为它导致"Hi kid" -2双引号

set programHTML to "\\"Hi \\"kid!\\"" --This doesn't! 将programHTML设置为“ \\” Hi \\“ kid!\\”“” -这不是!

Because it results in "Hi "kid!" - 3 double quotes 因为它导致"Hi "kid!" -3双引号

Change the outer quotes to single quotes so they don't interfere with the inner double quotes. 将外部引号更改为单引号,以免干扰内部双引号。 If you do this, you don't need to worry about escaping the double quotes at all. 如果这样做,您根本不必担心转义双引号。

//using the single quote in the beginning and end
console.log('"Howdy, "kid!", is your "mom" home?"');

reads as "Howdy, "kid!", is your "mom" home?" 读为“你好,孩子!”,你的“妈妈”在家吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM