0

I have an extension which consists essentially of this (using jQuery):

$('a[href*="dailymail"]').css({'background':'#333'})

and it works just fine on regular links.

It doesn't work on Google search results, however, because the href part of the link is actually a google redirect URL, and the actual URL is in a data-href attribute.

My problem is that

$('a[data-href*="dailymail"]').css({'background':'#333'}) 

doesn't work. Do I have a problem with matching the attribute/value, or with Google's search results page, or what?

TIA for any thoughts.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
AmbroseChapel
  • 11,957
  • 7
  • 46
  • 68

1 Answers1

1

Likely your problem is when the code executes.

Google page loads its search results dynamically (for example, for Instant purposes). If your code executes in a content script, that only guarantees that it executes after static DOM was parsed.

$(selector).css() operates by adding inline styles to elements that are matched by the time the code runs. So, results (that are added later) are unaffected.


The most obvious solution is to simply inject CSS, not JS code:

"content_scripts" : [{
  "matches": ["*://*/*"],
  "css": ["dailymail.css"]
}]
a[href*="dailymail"], a[data-href*="dailymail"] {
  background: #333;
}

This should be specific enough to apply; if not, try adding !important.

If you want to control this programmatically, consider adding a <style> element to the page.

Alternatively still, you can watch the page for newly added links.

Xan
  • 74,770
  • 16
  • 179
  • 206