0

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.

2
  • add the option to the pageloadcomplete section ... in CSS ? Wrong way. Commented Jan 9, 2015 at 2:14
  • I want to edit the css in c# not css. The website does not allow highlighting of text. Was just wondering if it is possible to modify the client side to allow. Sort of like chromes inspect element. Commented Jan 9, 2015 at 2:18

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

For the browser plugin route there are plugins that work as a "framework" that lets you write tools on top of them, for example on firefox Greasemonky will let you write javascript that executes and modifes any web page you want and what the OP wants done could easily be done through that.
Thank you this was the information I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.