
想做一个 Javascript 书签,将当前阅读的微信标题提取出来,当我使用以下这段 js 代码时是可以正常运行的
Javascript:(function(){ code = (document.documentElement.outerHTML); title = code.match(/msg_title = ".*"/g); window.open('omnifocus:///add?note='+encodeURIComponent(window.location)+'&name= '+title+'&cOntext= Reading Lists'+'&estimate=05 mins&autosave=true'); })(); 但是一旦加上一个 replace 之后(如下),这个 js 书签点击就没有反应了,这可能是什么原因?
Javascript:(function(){ code = (document.documentElement.outerHTML); title = code.match(/msg_title = ".*"/g); title = title.replace(/msg_title = |"/,''); window.open('omnifocus:///add?note='+encodeURIComponent(window.location)+'&name= '+title+'&cOntext= Reading Lists'+'&estimate=05 mins&autosave=true'); })(); 1 pandacat 2018-08-07 22:34:59 +08:00 via Android 是不是”问题? |
2 paloalto 2018-08-07 22:38:42 +08:00 if (title) { title = title.replace(/msg_title = |"/,''); } 估计是可能有 title 取不到的情况 |
3 JamesHopbourn OP @paloalto 但是我是在微信文章页面做的测试,是取得到 title 的 |
4 JamesHopbourn OP @pandacat 我试着仅仅 replace msg_title 也没有生效 |
5 8qwe24657913 2018-08-08 00:51:50 +08:00 String.prototype.match() 要么返回 null,要么返回一个数组。即使一定能取到,你也要用 title[0].replace() 而不是 title.replace() |
6 airdge 2018-08-08 08:22:04 +08:00 code.match 返回的是数组 title = title[0].replace(/msg_title = |"/,''); |
7 airdge 2018-08-08 08:29:35 +08:00 code = (document.documentElement.outerHTML); var re = /msg_title = "(.*)"/g; var title = re.exec(code) [1]; |
8 zcxj138 2018-08-08 08:59:33 +08:00 那个双引号有没有转义? |
9 JamesHopbourn OP |