Hook function without direct reference - javascript

I try at the moment to hook a Javascript function in a WebGame.
The problem is that this function isn't available in the global DOM. The only way that I get it working, is to set a breakpoint in the browser debugger, override the function in the console and let it run again. Unfortunately the whole JS-Code is Obfuscated and loaded via a webpack lib.
My question is now, is there any way to find that function from outside or to automate the "breakpoint way"? I want to load my hook without to set manual a breakpoint.
I tried to find the function with that: Recursively search for a value in global variables and its properties but without success.

The Solution was to use greasemonkey and a simple python flask server.
The greasemonkey script prevent that the js file get load, instead its load the script from the flask server, that download the js and modify it.

Related

javascript implementing problem in creating a ms teams app

I am new at creating Microsoft Apps and i managed to build an app (group tab) and install it in Teams.
How can i attach an external javascript file to the Tab.js?
import './MyFunction.js'; //get's ignored
How can i generally use javascript code in the Tab.js , because it wont even accept the script tag without returning an error?
Compile with Script - ErrorMessage
(The only thing i found out was that you need the javascript client sdk, but i cannot find examples or how/where to you use it in the Tab Script)
To attach one javascript file in another javascript in your example you need to export Myfunction in Myfunction.js and import Myfunction from './Myfunction.js' in Tab.js. For second question you cannot use script tag in Tab.js, in return() function you need to write html code and in render() function you need write functions you want to call.

How to use external ajax response in Chrome Plugin

I have a existing file ajax,js in my website that makes an ajax request and creates a global JSON object, searchResult using that response. Now I am creating a Chrome plugin, that requires this JSON Object inside it. I have a content script for the plugin viz. plugin.js and I want to include the object inside plugin.js file.
When I try to log window.searchResult from within plugin.js, it shows as undefined.
but when I use browser console, it shows the value as expected.
Please help me with this.
Problem
Chrome content scripts and the page's own scripts live in isolated worlds.
Content scripts execute in a special environment called an isolated world. They have access to the DOM of the page they are injected into, but not to any JavaScript variables or functions created by the page.
Your code works in the console, since you execute it by default in the page's context. To see what the extension sees, you need to switch it.
Solution 1a
First, the generic solution (works even if you don't control the webpage)
There is a way around this, by injecting some code directly into the page's "world" (or properly called, context).
After injecting the code, your page-level script needs to communicate with the content script to pass the data. It's possible with custom DOM events (as, as you remember, DOM is shared). The page-level script sends an event with the data in the event's details.
Alternatively, you can just attach the data to some DOM node, say, an invisible <div>.
Solution 1b
Since you said it's your page, you can skip the inject-into-the-page step and have a listener ready in the page's own code.
The content script sends a custom event to request the data, and the page answers passes the data back as described in 1a.
Solution 2
In theory, you don't even need a content script.
You can use the "externally_connectable" mechanism to speak with the page directly.
Note though that the page has to initiate the conversation.

Is there a way to run JavaScript code via a shortcut within Firebug?

Is there a way (extension or something build-in) in Firebug to create a shortcut that runs a piece of JavaScript (e.g. a function)?
For instance, I want to delete the local storage and do a reload each time I press Ctrl+y.
You currently (Firebug 2.0.1) cannot bind a keyboard shortcut to JavaScript code you entered within Firebug.
Though you at least have the possibility to save aliases for URLs containing scripts as described in "How to inject JavaScript snippet via Firebug on page load?". This at least allows you to store specific functionality and call it again later via the include() command.
There are also some feature requests targetting easy code snippet execution. You may want to create a new request for allowing to reference these snippets via a shortcut.

How do I use Firebase in an external Javascript file

This is probably a very simple issue, but I've been trying to use Firebase in an external javascript file that is being used with an HTML file and can't get it to work properly. I am planning to use this file for many other similar pages, so I'd rather keep it in an external document. Specifically, my code is:
$(function() {
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.src= 'https://cdn.firebase.com/v0/firebase.js';
head.appendChild(script);
var Database = new Firebase('https://myfirebase.firebaseIO.com/');
...
but when I try to run it, it says that the Firebase object/keyword is undefined. I know that the script is being correctly appended to the HTML page because I've checked the HTML on the page after running the code.
I have also read somewhere that you might need to have a personal server to run Firebase, but frankly I don't really know what that means - in any case, I use Mac OSX and run all of my HTML and Javascript in Chrome.
Thank you very much!
The problem is that using document.createElement does not force the script to be loaded and rendered before your inclusive script is invoked (it's being invoked now). There are no guarantees by this method on when the script you include will get invoked.
Additionally, you are loading the script onDomReady by putting it inside $(function() {...}); you would want to insert it into the header immediately, not wait for the entire document to load.
The simplest answer is to just put Firebase into the head of the html page; you haven't really explained your limitations here, but I assume this isn't an option for you. If it is, KISS.
Another simple answer is to utilize jQuery, since you obviously have it available.
$.getScript('https://cdn.firebase.com/v0/firebase.js', function() {
// now I can use Firebase
});
You can also accomplish this with other methods (wait until Firebase is defined using a setInterval; utilize other script retrieval methods besides document.createElement--try googling "load scripts dynamically via javascript load order"), but I think this covers your needs sufficiently.

Get path of lazy loaded JS file from within the file?

If I'm loading a script using something like
document.getElementsByTagName("head")[0].appendChild(scriptElement);
…what code can I put inside of that loaded JS file to get the URL of itself?
In other words: I use dom injection to load http://foo.com/foo.js. From within foo.js, how do I get the URL http://foo.com/foo.js?
If you have included the scriptElement object in your dom, then you should know the "scriptElement.src" - so inside foo.js you should know the source:
alert(scriptElement.src);
I've found a solution that works for me here:
javascript - get node reference to script tag that calls a function
The answer that requires you to throw an error in the loaded file, catch it, and then pass it to the global function in the loading page did the trick. (It doesn't work in IE, but for my current project that is not a concern.)

Categories