1

I have found a javascript function that inverts colors on webpage:

String javascript = "javascript: (function (){var newSS, styles = '* { background-color: black ! important; color: green !important; }a:link, a:link * { color: green !important; text-style: underline; }a:visited, a:visited * { color: #7f0000 !important; }a:hover, a:hover * { color: red !important; }';var elemHead = document.getElementsByTagName(\"head\")[0];var elemCSS = document.getElementById(\"darkenCSS\");if (elemCSS){elemHead.removeChild(elemCSS);}else{newSS = document.createElement('link');newSS.rel = 'stylesheet';newSS.href = 'data:text/css,' + escape(styles);newSS.id = \"darkenCSS\";elemHead.appendChild(newSS);}})();";

Is it possible to run this automatically? By that I mean load www.google.co.uk and apply this javascript function.4

Hope that makes sense, I don't know much about javascript.

CLARIFICATION:

I want to know if this javascript function can be appended to a URL at all. Something like http://www.google.com/?Javascript_blah_blah_blah

FURTHER CLARIFICATION:

I am making a basic web browser in Android. I want to invert colours on the webpage. I have made a button, that executes this javascript on the page. This works. But needs the user to press the button each time.

I want to make a switch to make permanently inverted.

So I need to browse to the url input and have it invert the colours automatically.

Hope this helps

9
  • You would probably have to create an extension for the browser you're using that would inject this script on every page. Commented Sep 13, 2011 at 20:48
  • So, you want your browser to automatically run this and invert the colors of every page you visit? You can probably build a Chrome or FireFox extension to do this. That's about all I can offer, though. Commented Sep 13, 2011 at 20:49
  • The automatic part is hard.. You can't exactly force another site to run js. You could build an extension in Chrome, or run it manually from the address bar or console... Commented Sep 13, 2011 at 20:49
  • You can save it as a bookmarklet which can probably be put on your browser's toolbar, making it a one-click operation. Commented Sep 13, 2011 at 20:52
  • 1
    If you are going to make a browser, you can just manipulate on the document's source code - simply append this chunk of code at the end of the page within <script></script> tags and it will be executed. Put the quoted JS code directly, or create a function from it and then use window.addEventListener / window.setTimeout. Commented Sep 13, 2011 at 21:21

4 Answers 4

3

Yes, paste the string without quotes into the address bar. Be sure that the psuedo-protocol javascript: is at the beginning


addition by rlemon

You first need to modify your script to unescape the escaped quotes, then add a new bookmark to your addressbar. Edit the bookmark and change the location to

javascript:%20(function%20(){var%20newSS,%20styles%20=%20'*%20{%20background-color:%20black%20!%20important;%20color:%20green%20!important;%20}a:link,%20a:link%20*%20{%20color:%20green%20!important;%20text-style:%20underline;%20}a:visited,%20a:visited%20*%20{%20color:%20#7f0000%20!important;%20}a:hover,%20a:hover%20*%20{%20color:%20red%20!important;%20}';var%20elemHead%20=%20document.getElementsByTagName("head")[0];var%20elemCSS%20=%20document.getElementById("darkenCSS");if%20(elemCSS){elemHead.removeChild(elemCSS);}else{newSS%20=%20document.createElement('link');newSS.rel%20=%20'stylesheet';newSS.href%20=%20'data:text/css,'%20+%20escape(styles);newSS.id%20=%20"darkenCSS";elemHead.appendChild(newSS);}})();

There you have it!

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

4 Comments

That's not really automatic though.
@patrick, yes it does. The code provided needs to be modified. but it works. Tested in Chrome 13 and FF 6.0.1
Sorry I haven't made myself clear .... I am running this in an Android WebView. I want to know if it is possible to have a url and append this javascript function to run. Something like google.com/?JavaScript...sd..d.d.d.. etc
@rlemon: I meant pasting javascript in the address bar doesn't work in FF anymore. It does in Chrome, I was wrong about that one so I updated my comment.
1

No, you can't "just run" javascript appended to a URL. This is security breach (will be). But you can use

  • bookmarklets ('URL' that begins with javascript:),
  • browser plugins (Chrome/FF/Safari - all can do this) or
  • you can look for plugin that can run userscripts. This is something like Greasemonkey.

Also you can try Fluid (Site Specific Browser, MacOS X only)... I think you get an idea.

BTW if you want/need to write bookmarlet you can run on specific site and want something to start - check this article: http://coding.smashingmagazine.com/2010/05/23/make-your-own-bookmarklets-with-jquery/

1 Comment

You are welcome. And if you want to run javascript within your custom browser - you can implement userscripting idea. This way you can run any script on every page loaded in your browser. Not sure it can help you but take a look at GreaseKit (it's userscripting "plugin" for Safari/WebKit) - code.google.com/p/greasekit
1

The easiest way if you are using Firefox (it is said the add-on works in Chrome and Opera, but I never tried it) is to install Greasemonkey and create a userscript for the domain(s)/URLs you want - www.google.co.uk/*, www.google.com/* etc.

However things like this are better done with Stylish extension, but you can use only CSS there, no JavaScript. But in what you presented, the following CSS fragment should work like a charm:

* { background-color: black ! important; color: green !important; }a:link, a:link * { color: green !important; text-style: underline; }a:visited, a:visited * { color: #7f0000 !important; }a:hover, a:hover * { color: red !important; }

There are lots of userscripts and userstyles available on the web.

4 Comments

how about on Android Webkit. This question needs some serious updates for clarification.
Indeed :) Huh, can't tell about Android Webview.
The Android WebView seems to handle URL's pretty similar to Chrome. I would settle for knowing if it is at all possible to append this javascript function to a URL? Regardless of the browser
I don't believe it would be possible for security reasons to append JS to URL. See here for further discussion. And generally speaking, whatever you append to URL will be set over the network as a part of URL; only the part after "#" will not, but it will be used as an anchor identifier within the page.
0

You could use Chrome + http://defunkt.io/dotjs/ to achieve that. That's exactly the purpose of this extension: automatically run some JS scripts on specific websites.

It's OSX only for now but I guess there are some other extensions doing that as well.

Comments

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.