0

I am trying to use sub_filter to replace pieces of this code in a .js file.

SYNO.SDS.AzureSSOUtils = function() {
    var a = 600
      , c = 500
      , e = (screen.width / 2) - c / 2
      , d = (screen.height / 2) - a / 2
      , b = String.format("height={0},width={1},left={2},top={3}", a, c, e, d);
    return {
        login: function(l, i) {
            var f = (function(m) {
                l(i, m)
            }
            ).createDelegate(this);
            var g = _S("dsm_https_port");
            var k = Ext.urlEncode({
                action: "signin",
                method: "azure_oidc",
                origin: location.origin
            });
            var j = location.hostname;
            var h = "https://" + j + ":" + g + "/webman/index.cgi?" + k;
            window.addEventListener("message", f);
            window.open(h, "OIDC", b)
        },
        logout: function() {
            var g = Ext.urlEncode({
                asso: "true"
            });
            var f = "webman/logout.cgi?" + g;
            window.open(f, "OIDC", b)
        }
    }

I have managed to use sub_filter to replace simple fragments

For example all of these examples work just fine and replace what i expect:

sub_filter              'dsm_https_port' '443';
sub_filter              "dsm_https_port" "443";
sub_filter              'SYNO.SDS.AzureSSOUtils' 'PleaseWorkDamnYou'

What I really want to do is one of these two replacements:

sub_filter 'var g = _S("dsm_https_port");' 'var g = 443;';
or
sub filter 'var h = "https://" + j + ":" + g + "/webman/index.cgi?" + k;' 'var h = "https://" + j + + "/webman/index.cgi?" + k;'; 

However neither of these seem to do anything. I have tried escaping every space, quote and semi colon by placing \ in front of them - while that didn't error it didn't make a difference.

What am I doing wrong? I assume this is because the text to be replaced has ambiguous characters in it but i am not sure.

for reference, yes i have made sure the following is enabled in my location section (which is what most articles advise, first all of this was need to be able to edit the small fragments that are working:

        gzip                    off;    
        proxy_set_header        Accept-Encoding         "";
        sub_filter_once         off;
        sub_filter_types        *;
8
  • Can you describe what is your actual problem you are trying to solve? To me, it looks you are trying to use sub_filter when you actually need to define variables outside the scope of JavaScript file. Commented Sep 14, 2020 at 6:03
  • 1
    Alex Balcanquall you need to escape the slashes like for example: "https:\/\/" Commented Sep 14, 2020 at 7:05
  • @TeroKilkanen i can't edit the javascript directly, i want to use the sub_filter to edit the .js file as it is downloaded to the client; assume the .js is uneditable. Commented Sep 14, 2020 at 17:09
  • @LukasRäpple thanks i will try that again, do you have a complete list of what does and doesn't need to be escaped? I tried escaping the quotes and spaces in this and couldn't get it to work. sub_filter 'var g = _S("dsm_https_port")' 'var g = 443' also one example i saw only escaped the find string and not the replacement string. Do i need to escape both sides of the sub_filter parameters? Commented Sep 14, 2020 at 17:12
  • How do you exactly use this JS file? Commented Sep 14, 2020 at 17:51

1 Answer 1

1

Ok, this is entirely my mistake (comes from not being a coder).

I was using chrome's developer tools to look at the desktop.js file the browser downloaded using the preview function in the network tab of chrome developer tools. It makes the code pretty. It never occurred to me it would insert spaces where it felt was good.

I downloaded the desktop.js file locally raw with no formatting and it turned out, that the text I wanted to replace was var g=_S("dsm_https_port") not var g = _S("dsm_https_port");

and var h="https://"+j+":"+g+"/webman/index.cgi?"+k not var h = "https://" + j + ":" + g + "/webman/index.cgi?" + k

once I fixed the whitespace issue and used escape chars everything worked as expected (i tried escaping before, but the whitespace issue was the root issue that blocked me).

Lesson / Answer = don't look at prettified code, look at raw file as returned by server to the browser.

For completeness of answer, this was the final sub_filter that worked:

sub_filter 'var g=_S(\"dsm_https_port\")' 'var g=443';

Thanks for everyone's comments, that was helpful.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .