How can I load an external JavaScript file using a bookmarklet? This would overcome the URL length limitations of IE and generally keep things cleaner.
2015 Update
Content security policy will prevent this from working in many sites now. For example, the code below won't work on Facebook.
2008 answer
Use a bookmarklet that creates a script tag which includes your external JS.
As a sample:
javascript:(function(){document.body.appendChild(document.createElement('script')).src='** your external file URL here **';})();
Firefox and perhaps others support multiline bookmarklets, no need for one liner. When you paste in the code it just replaces newlines with spaces.
javascript:
var q = document.createElement('script');
q.src = 'http://svnpenn.github.io/bm/yt.js';
document.body.appendChild(q);
void 0;
Example
If I can add method tested in FF & Chrome (for readibility split to multiple lines):
javascript:var r = new XMLHttpRequest();
r.open("GET", "https://...my.js", true);
r.onloadend = function (oEvent) {
new Function(r.responseText)();
/* now you can use your code */
};
r.send();
undefined
It is no longer recommended to do this as CSP on most website will make it fail. But if you still want to use it: 2022 example
(() => {
const main = () => {
// write your code here
alert($('body')[0].innerHTML)
}
const scriptEle = document.createElement('script')
scriptEle.onload = main
scriptEle.src = 'https://cdn.jsdelivr.net/npm/jquery#3.6.1/dist/jquery.min.js'
document.body.appendChild(scriptEle)
})();
I always prefer to use a popular open source project loadjs
it is cross browser tested and has more functionality/comfort features.
So the code will look like this:
loadjs=function(){function e(e,n){var t,r,i,c=[],o=(e=e.push?e:[e]).length,f=o;for(t=function(e,t){t.length&&c.push(e),--f||n(c)};o--;)r=e[o],(i=s[r])?t(r,i):(u[r]=u[r]||[]).push(t)}function n(e,n){if(e){var t=u[e];if(s[e]=n,t)for(;t.length;)t[0](e,n),t.splice(0,1)}}function t(e,n,r,i){var o,s,u=document,f=r.async,a=(r.numRetries||0)+1,h=r.before||c;i=i||0,/(^css!|\.css$)/.test(e)?(o=!0,(s=u.createElement("link")).rel="stylesheet",s.href=e.replace(/^css!/,"")):((s=u.createElement("script")).src=e,s.async=void 0===f||f),s.onload=s.onerror=s.onbeforeload=function(c){var u=c.type[0];if(o&&"hideFocus"in s)try{s.sheet.cssText.length||(u="e")}catch(e){u="e"}if("e"==u&&(i+=1)<a)return t(e,n,r,i);n(e,u,c.defaultPrevented)},!1!==h(e,s)&&u.head.appendChild(s)}function r(e,n,r){var i,c,o=(e=e.push?e:[e]).length,s=o,u=[];for(i=function(e,t,r){if("e"==t&&u.push(e),"b"==t){if(!r)return;u.push(e)}--o||n(u)},c=0;c<s;c++)t(e[c],i,r)}function i(e,t,i){var s,u;if(t&&t.trim&&(s=t),u=(s?i:t)||{},s){if(s in o)throw"LoadJS";o[s]=!0}r(e,function(e){e.length?(u.error||c)(e):(u.success||c)(),n(s,e)},u)}var c=function(){},o={},s={},u={};return i.ready=function(n,t){return e(n,function(e){e.length?(t.error||c)(e):(t.success||c)()}),i},i.done=function(e){n(e,[])},i.reset=function(){o={},s={},u={}},i.isDefined=function(e){return e in o},i}();
loadjs('//path/external/js', {
success: function () {
console.log('something to run after the script was loaded');
});
I have created this javascript to block vpn on my website.
I would like to run this code in php or make it run inside a php tag because it is to easy to bypass this code with plugins that block javascript.
I have try to write it inside a php tag but seems not working.
Thank you so much for your help, I'm not a PHP expert so it is hard for me to find the solution.
<script type="application/javascript">
function getIP(json) {
var rightnow = new Date();
var backthen = new Date(2017,07,31);
if (rightnow<backthen)
{
var r = (json.ip);
var s = (json.org);
var t = (json.country);
s = / (.+)/.exec(s)[1];//hostname
//------- ISP | FAI ---------//
var bannedhostname=[
"DigitalOcean, LLC",
"Digital Ocean, Inc." ,
]
//------- IP FOR TESTING THE SCRIPT ---------//
var bannedip=[
"0.0.0.0","45.32.149.219","138.197.142.88","159.203.21.83","138.197.142.88","104.131.124.76",
]
var handleip=bannedip.join("|");
var handlehostname=bannedhostname.join("|");
handleip=new RegExp(handleip, "i");
handlehostname=new RegExp(handlehostname, "i");
if (r.search(handleip)!=-1)
{
}
else if (s.search(handlehostname)!=-1)
{
alert("Your ISP : " + json.org + " seems to be a VPN or CLOUD HOSTING, deactivate it and check again");
}
}
else
{
alert("The VPN BLOCKER script used on this website is out of date, visit www.facebook.com/VPN-Blocker-1416592971752805 and download the update");
}
}
</script>
<script type="application/javascript"
src="http://ipinfo.io/?callback=getIP">
</script>
As per my understanding, You can't do this because in the end on browser level it will be parsed as JS and if any plugin disable JS or browser JS is disabled than JS will not work on the browser.
You can simply assign all the Js in a php variable and each that variable whenever you want to execute this js code.
I would like to write a JavaScript code processed with Mozilla Rhino that can do a simple HTTP GET request, which fetches a text string from a URL.
The problem is that, I couldn't find any support in Rhino to do any kind of HTTP requests. Besides, I don't have access to the Rhino instance itself, it's running via TopBraid Composer IDE for ontology modelling. I believe any idea about a simple library that I can import within my JavaScript file maybe a good solution.
Any help?
Thanks.
Okay, so it wasn't that difficult to figure it out. This one works via TopBraid Composer and without importing any JAVA libraries. Here's the answer in case anyone needs it later on.
var resourceURL = new java.net.URL(
'http://someurl');
var urlConnection = resourceURL.openConnection();
var inputStream = new java.io.InputStreamReader(urlConnection
.getInputStream());
var bufferedReader = new java.io.BufferedReader(inputStream);
var inputLine = bufferedReader.readLine();
bufferedReader.close();
var jsString = String(inputLine);
return jsString;
How can I run exe file from Mozilla Filrefox?
I tried this but it not working.
var oShell = new ActiveXObject("Shell.Application");
var comandtoRun = "C:\\Buziol Games\\Mario Forever\\Mario Forever.exe";
oShell.ShellExecute(comandtoRun,"","","open","1");
You can't run any system command from a web page. This was only possible with Internet Explorer under certain conditions, but fortunately it's not something you can do with modern browsers.
Perhaps your path isn't right. It could be produced by whitheSpaces.
You can solve it by quoting folder names wich contains whitespaces, like this.
var oShell = new ActiveXObject("Shell.Application");
var comandtoRun = "C:\\'Buziol Games'\\'Mario Forever'\\'Mario Forever.exe'";
oShell.ShellExecute(comandtoRun,"","","open","1");
If ActiveXObject not works on firefox, you can use window.open function.
window.open('file:///C:"Buziol Games"/"Mario Forever"/"Mario Forever.exe"');
I'm working on some code that needs to parse numerous files that contain fragments of HTML. It seems that jQuery would be very useful for this, but when I try to load jQuery into something like WScript or CScript, it throws an error because of jQuery's many references to the window object.
What practical way is there to use jQuery in code that runs without a browser?
Update: In response to the comments, I have successfully written JavaScript code to read the contents of files using new ActiveXObject('Scripting.FileSystemObject');. I know that ActiveX is evil, but this is just an internal project to get some data out of some files that contain HTML fragments and into a proper database.
Another Update: My code so far looks about like this:
var fileIo, here;
fileIo = new ActiveXObject('Scripting.FileSystemObject');
here = unescape(fileIo.GetParentFolderName(WScript.ScriptFullName) + "\\");
(function() {
var files, thisFile, thisFileName, thisFileText;
for (files = new Enumerator(fileIo.GetFolder(here).files); !files.atEnd(); files.moveNext()) {
thisFileName = files.item().Name;
thisFile = fileIo.OpenTextFile(here + thisFileName);
thisFileText = thisFile.ReadAll();
// I want to do something like this:
s = $(thisFileText).find('input#txtFoo').val();
}
})();
Update: I posted this question on the jQuery forums as well: http://forum.jquery.com/topic/how-to-use-jquery-without-a-browser#14737000003719577
Following along with your code, you could create an instance of IE using Windows Script Host, load your html file in to the instance, append jQuery dynamically to the loaded page, then script from that.
This works in IE8 with XP, but I'm aware of some security issues in Windows 7/IE9. IF you run into problems you could try lowering your security settings.
var fileIo, here, ie;
fileIo = new ActiveXObject('Scripting.FileSystemObject');
here = unescape(fileIo.GetParentFolderName(WScript.ScriptFullName) + "\\");
ie = new ActiveXObject("InternetExplorer.Application");
ie.visible = true
function loadDoc(src) {
var head, script;
ie.Navigate(src);
while(ie.busy){
WScript.sleep(100);
}
head = ie.document.getElementsByTagName("head")[0];
script = ie.document.createElement('script');
script.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js";
head.appendChild(script);
return ie.document.parentWindow;
}
(function() {
var files, thisFile, win;
for (files = new Enumerator(fileIo.GetFolder(here).files); !files.atEnd(); files.moveNext()) {
thisFile = files.item();
if(fileIo.GetExtensionName(thisFile)=="htm") {
win = loadDoc(thisFile);
// your jQuery reference = win.$
WScript.echo(thisFile + ": " + win.$('input#txtFoo').val());
}
}
})();
This is pretty easy to do in Node.js with the cheerio package. You can read in arbitrary HTML from whatever source you want, parse it with cheerio and then access the parsed elements using jQuery style selectors.