Can you, without a browser extension, one off inject JavaScript into a page to run before anything else does? - google-chrome

I need to be able to inject javascript into my page to run before anything else does.
My use case is collecting debugging events from event listeners that start firing when my application begins booting.
Using a content_script with a Chrome extension is one way, however I want to be able to support cases that don't use a Chrome extension.
So far I have been using a proxy server that rewrites the index.html, replacing <head> with <head><script>console.log('hi')</script> but that solution is inaccessible and impractical.
Is there a way to inject javascript into a page so the next time you refresh it that code runs before anything else does?
Perhaps manually editing the html in the sources panel (does that persist)?

Related

Close a game in Godot

I'm creating a web game using Godot.
For close the game, i tried to use `get_tree().quit()`.
If I use it on the IDE, it works. When i tried it on my server (after exported the project) it doesn't work.
I'm sure that Exporting setting are okay.
How can I close the game?
And, how can I add an hypertext link (similar to html `` tag)?
Thanks for your answer and sorry for my bad English
Exit the game
On the web, using
get_tree().quit()
Should work. That is, it should stop the runtime. The game will not continue running. It does not close the browser tab. In fact, browsers have restriction on scripts closing tabs.
Note: Make sure you are using Godot 3.2.3 or newer (see #39604). I tried it, it works.
Making a link
You can a LinkButton, which is a button that looks like an hyperlink. And you want to connect its "pressed" signal to a script where you use OS.shell_open, for example:
OS.shell_open("https://example.com")
Note: This result in a new tab in web exports. On the desktop it opens the default browser.
Navigating the browser
Since you ask about closing the game, and about making a link, I'll venture to guess that what you actually want is to navigate (leaving the game and going to another page), you can accomplish that with JavaScript.eval, for example:
JavaScript.eval("window.location.href='https://example.com'")
Note: This can only work on a web export.
Detecting Web Build
You can use OS.get_name to identify the platform.
For example, you can do this:
if OS.get_name() == "HTML5":
JavaScript.eval("window.location.href='https://example.com'")
else:
OS.shell_open("https://example.com")
Which will navigate the browser if this is a web build, but if it isn't, it will try to open the default browser.

Google Chrome - Extension vs App

I want to modify chrome start page, change background and maybe logo.
I want to include jQuery in users local storage and load it while users page loads ( search page )
I never done anything with chrome so I want some way where to start. Do I go reading with apps or extensions?
As a rule of thumb: Choose an app if you want to develop an (independent) application that can stand on its own. If you want to interact with the browser (e.g. modify a web page), build an extension.
In your case, you definitely need an extension.
Change start page - use chrome_url_overrides in the manifest file to override newtab.
Locally store jQuery: Although it's possible to store jQuery in local storage, you're probably fine with packaging jQuery with your extension.
To add it to your user's "search page", you have to use a content script. By default, content scripts run in an execution environment that is different from the page (the document's DOM is shared though). Usually, this behavior is desired. If you really want to expose the jQuery library to the scripts in the page, take a look at this answer.
If you really want to load some script from a remote location and use it as a content script, read Chrome extension adding external javascript to current page's html.
I'm not sure what you mean by "change background and maybe logo". If you're referring to the browser's appearance, the only option to do that is by creating a theme. This must be a separate extension.

Communicate between isolated worlds (extension js and webpage js) on chrome extension

As you may know, js files on chrome extensions and pages cannot directly access each other and they run on isolated worlds.
However, I want to access some of the functions on a page and call those functions from the plugin.
And I do not want to make my own version of those functions.
I'm wondering if this is possible... Would appreciate all the answers.
EDIT: The functions are on the background page. Its a browser action extension.
more info: Basically, I have a context menu which creates a tab to submit url to a page. however, re-opening the page makes many tabs and opening so many pages takes time. So I am willing to call the javascript function on the page directly from the extension if one instance of the page is open already. And I have access to the page permission-wise.
TL;DR: Need a way to call a javascript function on a page (without copying it locally) from an extension.
Assuming you know the tab ID of the page whose function you want to call, you can use chrome.tabs.update(tabId, {url: 'javascript:functionNameHere()'}); from your extension page. This works because javascript: URLs are mapped to script execution in the main (i.e. page, not isolated) world.

Local WebSite: run default browser

I need to create a brochure-cd from a website I did. All resources are html, images and xml, so i don't have any problem at all in accessing file system. My question is: once I open my index.html page in browser, how can I set the params of the window (eg: show addressbar, show statusbar, etc)
Thanks
(If you have an alternative idea of how run an html based brochure on CD just let me know)
EDIT: Specs changed, I've just knewn I must write xml and upload file, too. Any idea? I'm considering Adobe Air, but would be amazing if I can compile a whole .NET website into an exe..... Anything similar?
The only way you can set these is when you open the page, not after it's already present. So, you will probably have to have a start page, which then opens your new page, with the parameters you want.
Be warned though, if you have any JavaScript, many browsers won't allow scripts to run locally, or will present a nasty warning message before it will execute.
You may look into having a small desktop application on the CD that launches an instance of a browser inside of itself, assuming you can run the browser on every machine this will be run on. This will probably give you the look you're after.
I think you can't. You can only set params of a window you create. So you'll have to run a script on load that creates a new window and then closes the initial window.
There is however no way that this script is executed without warnings, if at all.
An alternative would be to develop an executable that is able to display the page, but that might not be easy, especially when you want to be browser and platform independent.

Running a greasemonkey script on a non-HTML page

I have the following fairly basic greasemonkey script:
var newloc = location.href.replace(/^(.*)-xyz-(.*)$/i, "$1$2");
if (newloc != location.href)
location.href = newloc;
That is, it basically strips out "-xyz-" from the URL and loads the page again. So if you navigate to "www.example.com/a-xyz-b/" it'll reload the page at "www.example.com/ab/".
Now, the script work fine if the page is an HTML page. But if I open a .jpg file or something that's not HTML then the script does not run at all.
Is this just a limitation of greasemonkey? That it only works if the page is actually text/html? What is an alternative way this functionality could be done?
Yes, Greasemoney fires on the DOMContentLoaded event, which doesn't seem to trigger on media objects (no DOM).
Get around this by firing on the parent/referrer pages and changing the links there.
Or, if the file names are on the local machine, use a text editor or batch job to rename/rewrite the links/names.
If neither of these workarounds is viable, post the specific details of how you are feeding these URLS to FireFox (name the browser in use if it's not FF).