Userscripts Support

Discussion in 'Feature Requests (AdGuard for Windows)' started by Boo Berry, Jul 16, 2013.

  1. Boo Berry

    Boo Berry Moderator + Beta Tester Moderator

    Joined:
    May 30, 2012
    Messages:
    4,058
    Right, so with userscripts being on the way to being supported, I've come up with a sample script that forces YouTube to use HTTPS instead of HTTP...

    Code:
    // ==UserScript== 
    // @name        YouTube HTTPS 
    // @version     2.0
    // @author		A Test Script
    // @description	Forces HTTPS on YouTube
    // @include		http*://*.youtube.com/*
    // @grant		GM_xmlhttpRequest
    // @grant		GM_addStyle
    // @browsers	chrome.exe;firefox.exe
    // @run-at		document-start
    // @priority	9001
    // ==/UserScript== 
      
    // Redirects you from HTTP to HTTPS 
    if (location.href.match(/http:/) ){ 
        var newUrl = location.href.replace("http://","https://"); 
          
        if(top.location==location.href){ 
            window.location.href = newUrl; 
            window.navigate(newUrl); 
            self.location=newUrl; 
            top.location=newUrl; 
        } 
    } 
      
    if (location.href.match(/redirect\?q=/) ){ 
        function redirectThrough(){ 
            top.location = $('#baseDiv p').eq(0).children('a').eq(0).html(); 
        } 
          
        doJQuery(redirectThrough); 
    }
    As you can see, a basic userscript. However, I've added a custom tag @browsers that specifies the processes that should use this userscript separated by a semicolon. I'm thinking when userscripts are fully supported, a UI feature will be needed to edit/customize the userscript, especially for specifying browsers against the browsers list in Adguard. :p

    Also the assistant would probably have to intercept user.js files on websites to install them (even if the assistant is hidden/not shown) or some type of custom function for that.
     
    Last edited by a moderator: Oct 23, 2013
  2. avatar

    avatar Administrator Staff Member Administrator

    Joined:
    Oct 26, 2010
    Messages:
    13,143
    We were thinking about @browsers tag too, it is important in our case.

    What for intercepting .user.js files -- maybe. First interface will be as simple as possible:)
     
  3. Boo Berry

    Boo Berry Moderator + Beta Tester Moderator

    Joined:
    May 30, 2012
    Messages:
    4,058
    As long as you can toggle scripts on and off, along with some way to edit scripts (or what GM does, add a default program to edit scripts like Notepad) I'll be fine with that. :) The browser tag might need a UI function to add/remove browsers that are available in Adguard (enabled browsers) to the script though.
     
  4. halcyonboy

    halcyonboy New Member

    Joined:
    Nov 11, 2013
    Messages:
    3
    Hi there,

    So i've been happily using this method to get Youtube Center running within IE now for the last month or so, using IE11. But now in the last week or so for some reason its just stopped working completely. I can't seem to see the script in any of the other browsers i have running either, as if the script is no longer being injected. This is quite frustrating as it was running perfectly, and now isn't. Can anyone offer any insight into this? Why would the filter injection just stop working?
     
  5. Boo Berry

    Boo Berry Moderator + Beta Tester Moderator

    Joined:
    May 30, 2012
    Messages:
    4,058
    Yeah, the injected was changed in 5.8 in preparation of supporting userscripts officially in a future version. Avatar, is it still possible to inject the userscript in 5.8?
     
    Last edited by a moderator: Nov 11, 2013
  6. avatar

    avatar Administrator Staff Member Administrator

    Joined:
    Oct 26, 2010
    Messages:
    13,143
    Adguard now supports javascript-injection rules out of the box (#%# prefix rules).

    Youtube control center rule:
    Code:
    youtube.com#%#window.addEventListener('DOMContentLoaded', function() { var script = document.createElement("script"); script.setAttribute("type", "text/javascript"); script.setAttribute("src", "https://userscripts.org/scripts/source/114002.user.js"); var p = (document.body || document.head || document.documentElement); p.appendChild(script); }, false);
    
    Beta-version with full userscripts support coming soon (week or two).
     
  7. Boo Berry

    Boo Berry Moderator + Beta Tester Moderator

    Joined:
    May 30, 2012
    Messages:
    4,058
    Nice! I notice there's a delay before the script kicks in, I assume this won't be an 'issue' with the official support? A week or two? Man, you guys are quick!
     
  8. avatar

    avatar Administrator Staff Member Administrator

    Joined:
    Oct 26, 2010
    Messages:
    13,143
    Unfortunately we are not so quick :)

    Looks like we need one more week to prepare beta-version.
     
  9. Boo Berry

    Boo Berry Moderator + Beta Tester Moderator

    Joined:
    May 30, 2012
    Messages:
    4,058
    That's fine with me, I can wait patiently. :D
     
  10. rickyoh

    rickyoh Beta Tester

    Joined:
    Feb 3, 2012
    Messages:
    565
    can't wait to try it out, exciting times ahead.
     
  11. Boo Berry

    Boo Berry Moderator + Beta Tester Moderator

    Joined:
    May 30, 2012
    Messages:
    4,058
    To anyone curious about the status of userscripts support, it's in the 5.9 beta currently being tested by us beta testers. It works near-perfectly too! But keep in mind there may be some scripts that won't work in Internet Explorer. Fortunately YouTube Center works for all browsers I've tried thus far!

    You guys will love 5.9 when it's released! ;)
     
  12. Boo Berry

    Boo Berry Moderator + Beta Tester Moderator

    Joined:
    May 30, 2012
    Messages:
    4,058
    Is there a list of what Greasemonkey functions Adguard supports?

    I might one found one more Greasemonkey function that Adguard may have to eventually support; the GM_registerMenuCommand (aka User Script Commands menu items) function to add support for custom dialogs/options/menu items for specific running user scripts. An example of this being needed is the YousableTubeFix userscript - you can't customize or change the scripts settings without support for this.

    Maybe a Script Settings button can be added to the Assistant (or perhaps a custom script button) to invoke said options/menus?

    Also, I'm beginning to see the need to specify which browser(s) can use user scripts (extensions) and which ones shouldn't.

    Thoughts?
     
    Last edited by a moderator: Feb 27, 2014
  13. avatar

    avatar Administrator Staff Member Administrator

    Joined:
    Oct 26, 2010
    Messages:
    13,143
    I should find some time to write documentation:)

    The list of the functions supported:
    GM_setValue
    GM_getValue
    GM_getResourceText
    GM_getResourceURL
    GM_xmlhttpRequest
    GM_addStyle
    GM_log (just a safe version of console.log)

    GM_registerMenuCommand is an empty function for now.

    What for the browsers support tag. We will definitely add it in Adguard 6.
     
  14. Boo Berry

    Boo Berry Moderator + Beta Tester Moderator

    Joined:
    May 30, 2012
    Messages:
    4,058
    Ah, excellent! :D
     
  15. GunGunGun

    GunGunGun New Member

    Joined:
    Jul 7, 2014
    Messages:
    8
    Hi avatar, do you think you can emulate GM_CopyToClipboard with ZeroClipboard, ZeroClipboard uses flash to change clipboard, if browser turn off click to play then I click possible, right ?
     
  16. avatar

    avatar Administrator Staff Member Administrator

    Joined:
    Oct 26, 2010
    Messages:
    13,143
    Not sure, ZeroClipboard is rather "heavy" library.

    What do you think about this implementation:
    Code:
    function GM_CopyToClipboard(text) {
      window.prompt("Copy to clipboard: Ctrl+C, Enter", text);
    }
    
    It's ugly but it works:)
     
  17. GunGunGun

    GunGunGun New Member

    Joined:
    Jul 7, 2014
    Messages:
    8
    Your method seem really nice, and after some tries with ZeroClipboard I think it will not work, that is sad :(
     
  18. avatar

    avatar Administrator Staff Member Administrator

    Joined:
    Oct 26, 2010
    Messages:
    13,143
    I've added a task for devs to add support for GM_CopyToClipboard.
    We'll do it in the patch planned for 5.10.
     
  19. GunGunGun

    GunGunGun New Member

    Joined:
    Jul 7, 2014
    Messages:
    8
    Sorry avatar, I typed wrong the copy to clipboard name, it should be GM_setClipboard
     
  20. avatar

    avatar Administrator Staff Member Administrator

    Joined:
    Oct 26, 2010
    Messages:
    13,143
    Thank you! Patch will be ready soon.