1

I'm running into issues with an existing userscript that works few months ago but not anymore.

// ==UserScript==
// @namespace       http://www.mysite.com
// @author          Vadorequest
// @license         GNU GPL v3
// @include         mysite.com/index.php (I had to remove the http:// because of SO security)
// @include         www.mysite.com/index.php
// @include         mysite.com/*
// @include         www.mysite.com/*
// @version         1.6
// @require         https://code.jquery.com/jquery-1.11.0.min.js
// ==/UserScript==

// Inject a custom script from one of my servers, allow me to change the source code without changing the userscript itself. Use jQuery.

My old way to use jQuery was to copy/paste jQuery script iself inside my user script, at the time I didn't find any other solution. But it's not what I should do.

The biggest problem now is that the script is loaded in all pages, in facebook, google, etc. Not only on the targeted website. Before, it worked. so I don't really get it. Maybe something changed in chrome. I guess so.

I'm currently reading Manually adding a Userscript to Google Chrome at the same time I'm writing this thread, it looks like things changed few months ago.

But it's confusing, should I use the manifest.json?

Anyway, I'll explain how my script works because it's a bit tricky:

  1. Userscript contains jQuery and create a DOM element in the head that import my custom script.
  2. My custom script is imported and run as if it was imported by the original webpage, not in some weird sub frame part.

Why? It allows me to dev quite quickly, because I don't need to change the userscript itself, just the script injected. My changes are applied on a simple save & upload, much better than distribute the userscript to all users. (Because it's not a public userscript and I won't add it on userscript.com or whatever)

I had issues at the time to make it work with jQuery, and later with jQuery.ui, I need both. (I wrote it 4 years ago)

So, is there a better efficient way to do this now? I guess I need to be updated on this, if you have any advice or good tutorial, don't hesitate! I'll try the way with the manifest.json.

Community
  • 1
  • 1
Vadorequest
  • 16,593
  • 24
  • 118
  • 215
  • 2
    The best way to use jQuery in a cross-browser script is shown in [this answer](http://stackoverflow.com/a/12751531/331508). – Brock Adams Apr 12 '14 at 11:08
  • I'm actually not sure this is still valid. My `@require` and `@include` don't seem to have any effect anymore. – Vadorequest Apr 12 '14 at 13:55
  • 1
    See [no effect, global](http://stackoverflow.com/a/14250187/331508), [no effect Chrome](http://stackoverflow.com/q/16403175/331508), and [jQuery conflicts](http://stackoverflow.com/a/12255424/331508) -- which also applies in Chrome+Tampermonkey. ... Also note that on Chrome, `@require` has no effect unless you are using Tampermonkey, which you should. ... Upvote early and often. – Brock Adams Apr 12 '14 at 19:58
  • 1
    My script works now using `TamperMonkey`, it fails if I use it in Chrome directly. Well, that's a solution. – Vadorequest Apr 12 '14 at 20:27
  • If it works in TM and not plain Chrome, and **you are not using `GM_` functions**, then use the technique from my first comment and it will work in both places. – Brock Adams Apr 12 '14 at 20:40
  • You mean http://stackoverflow.com/questions/2246901/how-can-i-use-jquery-in-greasemonkey-scripts-in-google-chrome/12751531#12751531 ? I don't think so! Because basically if I do that my script is imported in all webpages, the `@include` rule is not respected, that was one of my first issues. – Vadorequest Apr 12 '14 at 20:47
  • 1
    Yes, that answer (specifically the full example script). And no, it respects `@include`, `@match`, etc. perfectly. If you are having `@include` issues, it's nothing to do with that technique. However, since you said your script now works with TM, just use TM! It's really much better than a plain Chrome userscript. – Brock Adams Apr 12 '14 at 20:51
  • I read somewhere that Chrome doesn't use `@include`, but that was something posted in 2012, maybe it does now but I'm sure that was included everywhere. Anyway, let's use TaperMonkey! Thanks. – Vadorequest Apr 12 '14 at 20:55

0 Answers0