user3674312
user3674312

Reputation: 33

Edit source css with C#

I was just wondering is it possible to edit a websites source clientside using C#. Like say I wanted to change webkit-user-select from none to inherited.

body {
background: #000;
color: #333;
margin: 0;
padding: 0;
-webkit-user-select: none;
}

I was wanting to add the option to the pageloadcomplete section, Just don't know where to begin. Never done that.

Upvotes: 0

Views: 64

Answers (1)

Jon Adams
Jon Adams

Reputation: 25147

It sounds like you want to temporarily modify the CSS file of (I assume the same project as your C# web site?) so it renders differently, but you want to do it from C# some how?

If so, the answer is you can, but I don't recommend it. But you can't do it in the ASPX WebForms page request, because the link in the CSS file that the ASPX request links to is a separate HTTP request completely.

Instead, you would have to write an HTTP handler and run all your CSS requests through it, manually load the CSS file, parse it, and somehow determining how and when to modify the response. Way too much work; I don't recommend it.

I think the simpler solution would be to have an extra CSS class in the file that doesn't normally apply to the element you want it to, then render client-side JavaScript in your ASPX page so that when the page loads (in the client) the JavaScript can add the CSS class to the element you need it to so different CSS applies.

There are lots of ways to write JavaScript to add/remove classes, both with libraries or in direct code. Google search for how to do that, there are a lot of basic tutorials on altering CSS classes on DOM elements via JavaScript.

IF the assumptions above are wrong, and you want to modify the CSS of another site you don't control, the answer is again, technically yes, but an even more emphatic "it's not worth it." You'd have to write a whole C# application to load the web site and all its resources, intercept the CSS file before being loaded into whatever renderer you're using, write code to parse the CSS and adjust it, etc. There's no reason to go to all that effort.

It would be a little more reasonable to write a browser plugin to do it, but again, that's a ton of work too.

Upvotes: 3

Related Questions