开发者

In chrome with a greasemonkey extension, how can I modify an `<a...>` construct to strip out the onclick= attribute?

I want to modify an internal webpage to strip away some of the onclick behavior of certain links.

The internal webpage has a bunch of links like:

<a href="/slm/detail/ar/3116370" onclick="rallyPorthole.showDetail('/ar/view.sp','3116370','pj/b');return false;">foo de fa fa</a>

How can I do an extension to Chrome so it does the following:

for link in all_links:
    if link's href attribute matches '/slm/det开发者_开发问答ail/ar/...':
        remove the onclick attribute


After finding this script, the following code can be put in a file ending in .user.js and installed in Firefox or Chrome.

// ==UserScript==
// @name          Rally Onclick Nuke
// @namespace     http://diveintogreasemonkey.org/download/
// @description   Nukes the "onclick" attribute from user story links so you can CTRL click a link and have it open in a new tab
// @include       https://*rally.sp
// ==/UserScript==

var links = document.getElementsByTagName("a");
for (i = 0; i < links.length; i++) {
  var node = links[i];
    var link = node.getAttribute("href");
    if (link && link.indexOf("slm/detail/ar/") > -1 ) {
        if (node.getAttribute("onclick")) {
          node.removeAttribute("onclick");
        }
    }
} 


Instead of document.getElementByTagName("a") you can also use document.links which you can read about here.

So to modify Ross Rogers' code:

var node, links = document.links;
for (var i = 0; node = links[i]; i++) {
  if (node.indexOf("slm/detail/ar/") > -1 ) {
      if (node.getAttribute("onclick")) {
        node.removeAttribute("onclick");
      }
  }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜