0

I currently have a Chrome Extention, that runs a series of jQuery at the end of the document, to replace links containing a string through the inject script. While this works on the website https://en.wikipedia.org/wiki/The_Sun_(United_Kingdom), this does not work on google.com (https://www.google.co.uk/#q=the+sun).

Inject Script

var arr = [
 'twitter.com/thesun',
 'facebook.com/thesun/',
 'https://www.facebook.com/thesun/',
 'thesun.co.uk',
 'https://www.thesun.co.uk/',
 'www.thesun.co.uk',
 'thesun.co.uk'
 ];
$.each(arr, function (index, value) {
$('a[href*="'+value+'"]').each(function() {
    $(this).text('ECLIPSE THE SUN');
    $(this).css("color","red");
    $(this).css({background:"black"});
    $(this).prop("href", "http://www.contrast.org/hillsborough/");
});
});

Manifest.json

{
"name": "CHANGE THIS : Extension boilerplate",
"version": "0.0.1",
"manifest_version": 2,
"description": "This extension was created with the awesome extensionizr.com",
"homepage_url": "http://extensionizr.com",
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"default_locale": "en",
"background": {
"page": "src/bg/background.html",
"persistent": true
},
"options_page": "src/options_custom/index.html",
"browser_action": {
"default_icon": "icons/icon19.png",
"default_title": "browser action demo",
"default_popup": "src/browser_action/browser_action.html"
},
"permissions": [
"notifications"
],
"content_scripts": [
{
  "all_frames": true,
  "matches": [
    "<all_urls>"
  ],
  "js": [
    "js/jquery/jquery.min.js",
    "src/inject/inject.js"
  ],
  "run_at": "document_end"
}]}
dpcleitao
  • 1
  • 1
  • [Possible relevant question](http://stackoverflow.com/questions/824349/modify-the-url-without-reloading-the-page), it seems to me that google is not fully reloading the page, hence not firing an DOMContentLoaded event again, not sure though. – JohannesB Nov 19 '16 at 22:57
  • Please [edit] the question to be on-topic: include a **complete** [mcve] that duplicates the problem. Including a *manifest.json*, some of the background *and* content scripts. Questions seeking debugging help ("**why isn't this code working?**") must include: ►the desired behavior, ►a specific problem or error *and* ►the shortest code necessary to reproduce it **in the question itself**. Questions without a clear problem statement are not useful to other readers. See: "**How to create a [mcve]**", [What topics can I ask about here?](http://stackoverflow.com/help/on-topic), and [ask]. – Makyen Nov 20 '16 at 01:16
  • @Makyen thank you for your comment, I updated the code and added example sites that create the issue. Please let me know if you need any other information. – dpcleitao Nov 20 '16 at 07:16
  • BTW: If you are going to use the same selector multiple times, then use the selector once and assign it to a variable, then use the variable. Doing so is much better than performing redundant walks through the DOM. In addition, jQuery permits performing multiple actions so you could chain them together without the need to perform the selection again. Even better would be to make a function which performed the actions. Given that it is multiple similar actions, it would be easy to have one function which you pass arguments of the URL and changes that are different (i.e. the old and new `href`). – Makyen Nov 20 '16 at 07:38
  • Given that you are actually performing only 3 different actions (`.text`,`.css`, and `.prop`) and do not need to be concerned about compatibility with old versions of IE (or browsers other than Chrome), you should seriously consider using straight JavaScript rather than loading jQuery on *every single webpage*. You are loading 85KiB of *minimized* jQuery code to save the small amount of additional code required to write this in straight JavaScript. – Makyen Nov 20 '16 at 07:54
  • @Makyen I thought I required jQuery for the selector, but I will investigate writing it in pure javascript. I removed the extra code and boiled it down to a function so it should be much more manageable code. However, it still won't run perform the action properly on google search pages. – dpcleitao Nov 20 '16 at 11:37

0 Answers0