Chrome扩展程序可替换所有链接,并略微修改自己的版本

时间:2016-05-01 01:26:39

标签: javascript jquery regex google-chrome google-chrome-extension

所以我是一个写Chrome扩展程序的新人。到目前为止,我有一些非常简单的js作为我的content_script.js:

var links = document.getElementsByTagName("a");
for(var i=0;i<links.length;i++){
    links[i].href = links[i].href.replace((www\.)?imgur.com\/gallery\/, i\.imgur\.com\/);
}

和我的manifest.json:

{
  "manifest_version": 2,

  "name": "Degallerify",
  "description": "This extension will convert all instances of imgur.com/gallery/ links to direct links",
  "version": "1.0",

  "browser_action": {
   "default_icon": "icon.png"
  },
  "permissions": [
   "http://*/*","https://*/*"
   ],
   "content_scripts": [
  {
    "matches": [
      "http://*/*","https://*/*"
    ],
    "js": ["content_script.js"]
  }
]
}

前提很简单,只需用直接图片链接替换所有imgur.com/gallery/链接即可。我通过在线正则表达式比较检查了正则表达式,它完全匹配。

但是,当我加载扩展程序时,它没有做任何事情。

This is an example of a page with gallery links

当我加载扩展程序并刷新页面时,所有链接都保留了图库链接。我的正则表达式是不正确的?我是否需要为JS做一些不同的事情?

谢谢!

1 个答案:

答案 0 :(得分:1)

$status = mysql_query("SELECT * FROM status WHERE user='$username' ORDER BY date DESC"); if(mysql_num_rows($status) > 0){ while($row = mysql_fetch_array($status)) echo "<table width='520'>"; echo "<tr>"; echo "<td width='53' height='57' valign='top' rowspan='3'><img src='../includes/images/profile/$image' height='50px' width='50px' style='border: 1px solid #000'></td>"; echo "<td width='411' valign='top'><font color='#CCCCCC'>". $row['sentby'] . "</font><br><font size='1px' color='#CCCCCC'>". $row['date'] . "</font></td>"; echo "</tr>"; echo "<tr>"; echo "<td></td>"; echo "</tr>"; echo "<tr>"; echo "<td>"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td colspan='2' valign='top'><font color='#CCCCCC'>". $row['status'] . "</font></td>"; echo "</tr>"; echo "</table>"; echo "</p>"; } } else { echo "<p>This user has not yet updated their status.</p>"; } 未与替换字符串连接,替换字符串未被引号括起; i.不需要转义。尝试将/与替换字符串连接起来,删除i.个字符,并用引号括起来。

/