Add your javascript to any page (using proxy or something else) - javascript

What I want to do is be able to see how it will look any website with my javascript attached . I want to create something like selectorgadget working on any website.
The first idea is to use a proxy (eg privoxy) and define a regular expression changing in html: </head> to <script src="http://myserver.com/my_javascript.js"></script></head>. It works, but in such case, I need to set proxy in my browser. And it is not cool because It visible only for me.
Another idea is to write a website that will download page under the hood and show it. How it will work:
Go to page eg.: http://myserver.com/
My index.php will download eg.: www.google.com using http client or curl, replace page source (add my <script> before </head>) and return page to user. It works, but when page contains relative images I'm not able to see it. Rewriting image src, and other script src is not so simple.
Because I want to add only javascript, I thought that you can use iframes. But there is a restriction to the same domain.
Any ideas on how to do it? Maybe some tool? Something with cross-domain iframe?

You can configure Apache web server to work in reverse proxy mode and use your first approach. Hence it will work very similar to your second case, but all replacement Apache server will do for you.

I'll throw Greasemonkey into the list of suggestions. Also note that FireBug allows you to run arbitrary JavaScript on a page, plus a lot more useful stuff for developing & debugging web apps.
(Although from your short description, I agree with awoodland that bookmarklets are probably the best solution.)

Bookmarklets let you do exactly this without any kludges.

if you are using Chrome, you can develop a simple extension and use the content scripts.

You can use the following project which is a node based proxy that can allow you to include any arbitrary JS or CSS in all pages
https://github.com/amitamb/plugin-proxy
I am the developer of the project.

Related

Change href of anchor tag through html/css

I have a webpage(say http://www.example1.com) which contains an anchor tag that points to a different website(say http://www.example2.com). I have to test it on testing servers (for which the urls are www.example1-test.com and example2-test.com) before publishing it. When the code is on example1-test.com then the link should point to example2-test.com and when it is on example1.com then it should point to example2.com.
But I cannot use JavaScript or manually change code while switching the servers. I can only use html and css. Is there a way to do this?
I know this is a weird question and css is used for styling and not to write logic but I cannot use JavaScript for this unless there is a way to enable JavaScript in the browser through some html tags.(say some meta tags,etc.)
I can put in two separate link pointing to respective example1.com and example2.com and do a show/hide depending on the environment but then the question is where do I put these conditions (cannot use JavaScript).
For testing I used to change my hosts file and redirect live domains to my test IPs on the internal network. I follow links and my browser thinks I'm hitting example2.com but actually my hosts file is pointing to the IP of example2-test.com and I see new functionality. When both go live I know visitors experience exactly what I've just tested but using live DNS.
Nowadays URLs in my applications are generated using routes and environments. I can specifically tell my framework that in development environments I expect URLs to be generated with XYZ base but behave differently in production.
Another option is Greasemonkey scripts. It is a browser plugin which executes scripts on command to manipulate pages, navigate, automate and more.
I can't think of a client-side way to achieve what you're looking for without the use of Javascript or browser plugins.

Can Javascript files be hosted other than website they need to point to?

this may seem like a strange question and I could have tested this first, but did not want to go through the hassle of setting it up only to find it doesn't work.
I have set up a JQuery based lightbox effect. The web page itself may be hosted at for example:
mysite.com
But I am not allowed to actually have access to the server for mysite.com (I know this sounds strange but it is what it is). However, I do have access to the server of a completely different website that belongs to the same organization. For example:
theirsite.com
So on a page where I want the lightbox effect to happen (mysite.com/lightbox.html) I can set up all of the tags and so forth on the page. But then in the head of this page, point to all of the JS and CSS files that are hosted on the theirsite.com server, so it looks like this:
So my question is - will this work? Or do I have to host all of the JS and CSS files on the same server (mysite.com) as the page itself that will display the lightbox itself?
Thanks.
Yes, in fact jQuery hosts a copy of itself on a CDN available with this script inclusion tag:
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
CSS files can be included in a similar fashion, instead of a relative path use the absolute path (starting with http:// typically, or the double forward slash for shorthand).
Here's a pretty awesome CDN that hosts a ton of stuff.
It will work fine, this is a very common practive and is encoraged in some situations.
For example, you should use the jQuery or google addresses for the jQuery libray, that way it can be cached on the client machine and used for several sites.
To verify, right click this page and view source, you'll see:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="//cdn.sstatic.net/Js/stub-uc.en.js?v=840de979f241"></script>

Chrome, firefox, or opera preload changes

Is there any way to "edit" a "server side" javascript file in one of the mentioned browsers that will save the js edits on the client side and replace the server side scripts?
Basically I want to edit the javascripts on the server. Obviously I can't save them on the server so they need to be saved on the client side(my computer) and the browser needs to load my scripts instead.
It shouldn't be hard to do at all but I've not been able to find any way to accomplish this.
Edit:
I want to modify the javascript's from a site I do not own or have write access too. e.g.,
Html page uses some javascript page on server. I want to modify this javascript file(the actual file).
I can download and save the javascript file BUT the html page will always use the one on the server because that is what is in the script tag. I need to modify the script tag of the html page to point to the local javascript file BEFORE the html page's scripts are executed(else the javascript from the server will be used).
here, for example, is a script tag from SE:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
It uses a non-local javascript file. I need to replace this line with my own line before any javascript is executed. It would like like:
<script type="text/javascript" src="file://C:/temp/myjquery.min.js"></script>
or whatever. (this way, I can modify the jquery file and have it execute my own version of the one on the server)
I, could, ofcourse, download the html file and modify it BUT then php code may not work among other things. (for example, relative links will be broke)
this is usually very easy in Opera: Just view source, edit what you want and use the special "Tools > Advanced > Reload from cache" command instead of a normal reload. Voila, you'll be running the site with your modified scripts..
(There are some exceptions, related to specific no-caching techniques some sites use it won't work 100% for all files - but it certainly should work for anything served from googleapis.com)
I think what you're looking for is something like LiveReload
It allows you to edit css files and have the browser apply the changes without refreshing the browser.
The windows version is in alpha right now but the Mac version works quite well for CSS.
I don't know if it does Javascript but I think it might.
You could also try the Chrome DevTools. It's a chrome extension that does just what you want with javascript and css.
No problem, you want to use bookmark-lets for this. Indeed it is easy, just remember to use an anonymous autoexecuting function: javascript:(function(){ //commands })();
In the sane good old day's one could even place this javascript directly into your addresses, but nowaday's some browser-builders (like firefox we coders USED to trust in the old day's) are being a 'good boy' and listen to facebook's 'demands' to kill normal standard functionality in favor of their lack on comprehending closures... But alas..
Ofcourse you could also create a bookmark to fix firefox's insanity, again reclaiming power to the user :)
Every time you visit the site, you click your bookmarklet. Done.
One can even make it 'memory resistant' for as long als you are on the same page (if you really want to). Naturally power is with the user/visitor AS IT SHOULD BE, not with the webmaster (who already publicly shared whatever info).
You might also look into greasemonky on firefox and comparable solutions.
Good luck
Build a string on the server side to write all your javascript code on the server side.

Include a text or XML file via the script tag?

For a little context I'm working on a site for a client and it has to run completely offline. It's just a set of html/css/js files that you run locally. The computers it will be used on are office computers and quite locked down so I can't even use java. Luckily the project isn't overly complicated and I've accomplished most of my goals with this limited platform. The issue I'm having is I want to create some easy to change files to load the data from. Right now I have all the data loading through script tags that point to js files that can be manually edited, however I've tried to make the javascript as simple and straight forward as I can but it's still not looking very friendly to someone who hasn't programmed before.
What I would like to do is include an xml file or text file in the HTML using a script tag or something similar and then use JS to read the contents but every time I try this it doesn't actually load the file. Here's a few things I've tried:
<script type="text/xml" src="myxml.xml"></script>
<script type="text/plain" src="myxml.xml"></script>
I've tried using XMLHttpRequest but most of these attempts end in the same result.. can't do a cross-site request. Even though I'm using a url "myxml.xml" and they're in the same folder, chrome is still convinced this is a XSS attempt. So I'm starting to run out of ideas. Can anyone think of any clever way to achieve this?
IF you're goal is to just run your web-app, even offline and you do not care about cross-browser compatibility, you can consider to convert you're application to Packaged App.
It will work only in google-chrome browser but setting the right permissions, you should not have problem with cross-site requests. At this point, you could download the xml content through a noraml XMLHttpRequest.

Stylesheets for content?

Forgive my ignorance since this seems like its something I should know by now.
I know I could make a stylesheet that will allow me to make changes in my CSS throughout several pages that use the CSS. I also know that you can make an external javascript file that could contain functions you want to reuse. But lets say I had pure HTML content (lets pretend a bunch of buttons or links) that I wanted replicated on several pages. Is there anything similar to a stylesheet in that regard? This would allow you to update the buttons/links all at once.
Try server-side includes.
The most frequent use of SSI is to include the contents of one or more files into a web page on a web server. For example, a web page containing a daily quote could include the quote by placing the following code into the file of the web page:
You could also use PHP, if your host allows it. Just change the name of the page from .html to .php and reference the header:
<?php include "header.php" ?>
Both of these require you to change the file's extension, so you might also want to use mod_rewrite to let users still access it via the .html name. Again, if your host supports it.
The question isn't that stupid, as there in fact is nothing native in HTML to do this.
If supported by your server, Server Side Includes are your best option. If you have PHP, you can also do a <?php include "footer.html"; ?>
All other server side languages have a similar construct.
Depends... I know Dreamweaver has some rather advanced support for templates. You can delve into the manual of your WYSIWYG HTML editor and get acquainted to how it can help you with repeatable content items. Otherwise, as Simon hinted, you should consider learning some server side technology (scripting language such as PHP is an easy choice), write your repeatable HTML and let the scripts output that whenever and wherever you need. Good luck!
It seems you're not using some server side technology like ASP.NET which has user controls on which you could place those.
An alternative would be to use Server Side Includes like:
<!--#include virtual="header.html"-->
Grz, Kris.
You can try using the CSS content property, but the content is inserted after/before the target. http://www.w3schools.com/Css/pr_gen_content.asp
EDIT
You can also try storing your content in XML documents and using JavaScript to load the XML sheets. Each sheet can store your button content, input content, etc. All you have to do is parse the XML and render the content as HTML elements.
While SSI seems like the best idea I believe, if memory serves me well, that if you're using IIS you're going to have to adjust some settings on the server to work get SSI with the html file extention.
While SimpleCoder's idea doesn't seem like the best idea it is an interesting one. Building on that idea maybe json data instead of xml would be best. I'd play around with this just for the fun of it.
If neither SSI or PHP is available, you could do it with javascript only:
Load the page into a hidden IFRAME, then grab it (with innerHTML)
- and move it to where you need it..
Unless you don't care about SEO, I would advise against using javascript for this purpose.
It's possible, but such a technique could prevent search engines from properly indexing your site.

Categories