Javascript函数调用中的多行字符串(Firefox user.js)



我有一个user.js文件,它为firefox的新选项卡页面设置了user_prefs()。当我在单行上使用转义引号进行函数调用时,它可以正常工作,但是当参数是多行字符串时,它就不起作用了。任何想法我如何能有这个工作作为一个多行字符串(可读性)?

:


user_pref("browser.newtabpage.pinned", "[{"url":"https://amazon.com/","label":"amazon","baseDomain":"amazon.com"},{"url":"https://mail.google.com/mail/u/0/#inbox","label":"mail"},{"url":"https://www.facebook.com/","label":"facebook","baseDomain":"facebook.com"},{"url":"https://twitter.com/","label":"twitter","baseDomain":"twitter.com"},{"url":"https://www.instagram.com/","label":"instagram","baseDomain":"instagram.com"},{"url":"https://www.reddit.com/","label":"reddit","baseDomain":"reddit.com"},{"url":"https://www.youtube.com/","label":"youtube","baseDomain":"youtube.com"},{"url":"https://keep.google.com/","label":"keep.google","baseDomain":"keep.google.com"},{"url":"https://app.plex.tv/desktop/#!/","label":"app.plex","baseDomain":"app.plex.tv"},{"url":"https://www.phoronix.com/scan.php?page=home","baseDomain":"phoronix.com"},{"url":"http://192.168.50.2","label":"nas","baseDomain":"192.168.50.2"},{"url":"http://192.168.50.2:8080","label":"app","baseDomain":"192.168.50.2"},{"url":"https://www.protondb.com/","label":"proton","baseDomain":"protondb.com"}]");

行不通:

user_pref("browser.newtabpage.pinned",
`[
{
"url": "https://amazon.com/",
"label": "amazon",
"baseDomain": "amazon.com"
},
{
"url": "https://mail.google.com/mail/u/0/#inbox",
"label": "mail"
},
{
"url": "https://www.facebook.com/",
"label": "facebook",
"baseDomain": "facebook.com"
},
{
"url": "https://twitter.com/",
"label": "twitter",
"baseDomain": "twitter.com"
},
{
"url": "https://www.instagram.com/",
"label": "instagram",
"baseDomain": "instagram.com"
},
{
"url": "https://www.reddit.com/",
"label": "reddit",
"baseDomain": "reddit.com"
},
{
"url": "https://www.youtube.com/",
"label": "youtube",
"baseDomain": "youtube.com"
},
{
"url": "https://keep.google.com/",
"label": "keep.google",
"baseDomain": "keep.google.com"
},
{
"url": "https://app.plex.tv/desktop/#!/",
"label": "app.plex",
"baseDomain": "app.plex.tv"
},
{
"url": "https://www.phoronix.com/scan.php?page=home",
"baseDomain": "phoronix.com"
},
{
"url": "http://192.168.50.2",
"label": "nas",
"baseDomain": "192.168.50.2"
},
{
"url": "http://192.168.50.2:8080",
"label": "app",
"baseDomain": "192.168.50.2"
},
{
"url": "https://www.protondb.com/",
"label": "proton",
"baseDomain": "protondb.com"
},
{
"url": "http://lichess.org",
"label": "lichess",
"baseDomain": "lichess.org"
},
{
"url": "https://app.ynab.com",
"label": "ynab",
"baseDomain": "app.ynab.com"
}
]`);

似乎user.js使用限制器解析器,所以我猜模板字符串只是不工作在那里

尝试使用带有换行转义的基本字符串,如

user_pref('browser.newtabpage.pinned',
'[                                       
{                                    
"url": "https://amazon.com/",    
"label": "amazon",               
"baseDomain": "amazon.com"       
},                                   
{                                    
"url": "https://app.ynab.com",   
"label": "ynab",                 
"baseDomain": "app.ynab.com"     
}                                    
');

(注意我使用' string '来避免"逃)

使用JSON.parse将其转换为对象(甚至将其写为对象),然后使用JSON.stringify将其转换回它期望的字符串

user_pref(
"browser.newtabpage.pinned",
JSON.stringify(
{ blah: 999 }
)
)

相关内容

  • 没有找到相关文章

最新更新