Using a page's functions in a Chrome extension - javascript

I have a Chrome extension which is injecting some code into a web page (I know that part's working), which is trying to call a function that is part of the original web page. The function works fine from the console, but gives an error when the extension calls it. I assume this is some sort of security feature, but is there a way to get around it? Please help!
PS. It's just for one website, if that helps

You can't do it with content scripts. Chrome extensions content scripts live in isolated worlds, meaning they share DOM, but have separate JavaScript sandboxes. Read more here:
The workaround to this is to inject a <script> into the DOM of the page, but that code won't have privileges to call any chrome.* APIs.

Related

using importScript in chrome extension web page scripts

In my chrome extension, I'm using a v3 manifest. It starts a background script which starts the extension's page (extension.html) in the browser. The page also has a service script (extension.js) which handles things happening on that page. Extension.js is started from the last line of the HTML body. All of this seems to be working fine.
I've tried to encapsulate the complicated portions of the code into relevant function blocks (and seperate files), which are then called as functions from the main code stream. These functions get pulled into the background.js using importScripts('function.js'); - and this works great.
In the page's service script (extension.js), attempting to do the same importScripts('function.js'); results in Uncaught ReferenceError: importScripts is not defined,
Shouldn't this functionality work the same in both the background.js and extension.js scripts?
I've tried the various wrappers and permutations found in other similar questions. Since these are not really 'worker' scripts, those suggestions simply result in the importScript not being called.
Thanks!

Can a browser's dev console continue executing JavaSript after a new page loads?

I'm trying to automate some online work through JavaScript and the Firefox (or Chrome) dev console. The work is mostly inputting the same (or similar) data on the same exact pages for many many people.
Example:
unique id
date 1 and 2
some more numbers
I wrote a very simple script that runs in the console and enters the data just fine.
The Problem
My script stops execution whenever it requires the page to reload or it loads another page. I cannot find any information on how to continue executing a script after a page has loaded.
My Limitations
I'm basically limited to what's on FireFox, Chrome, or Edge. Unfortunately, I cannot download any programs or tools that would make the automation any easier right now. Otherwise, I would just use Selenium and Python.
What I've Tried
First I tried to use the script that I describe above (simple DOM manipulation)
Then I tried to use the Selenium browser add-on, but I had to enter a starting URL for it to run. Selenium was not able to get past the login page of our system which is the only static URL that I can use as a starting point.
I then tried to use the Firefox Browser Console (different from the dev console) because the documentation seemed to suggest that I can use JavaScript on the entire browser (not just one tab). Unfortunately, I cannot find any helpful information on how to use the browser console for DOM manipulation. Everything that I search for points to how you create a browser extension, add-on, or how to use JavaScript on your own website.
What I Want To Do
I want to create a script that runs in a dev console. The script should take all of the data either from a separate page or an array then enter the data on each page for each person. I'll also have it prompt the user to verify the data before submission.
What I'm Looking For
What I'm hoping to get from this question is at least one three things.
An answer to the question's title.
Being directed to documentation or some other solution that can solve any of the above problems.
Being told if this is impossible and why by those who have more experience than me (I don't understand if the problem is just a lack of knowledge or limitations on the tools themselves.)
I think you can create a chrome extension and put your code in the background service worker. or use workers read this link

Google Chrome console run a command in every page of a particular website

I am visiting a website which I think has a bug. And I have tried running a javascript function (defined by them), after running the function the site works smoothly in that page. After I navigate to a different page, I again have to run the function. So I want it to run automatically after the page loads. I am using google chrome. So, are there any chrome extensions or any built in thing that can do this. Any help will be appreciated.
Thanks in advance.
I think tampermonkey is what you are looking for
https://chrome.google.com/webstore/detail/tampermonkey/dhdgffkkebhmkfjojejmpbldmpobfkfo
Another solution is to write an extension that injects code in the page's context.
It's important, because normally code in the extension is isolated from the page.
Tampermonkey route is probably better though in your case.

Access Settings in Start Script of Safari Extension

I'm working on a Safari extension now, but I haven't been able to find a way to access any of the extension's settings from within the start script. I can access them just fine from the end script by getting them from messages to the global.html file. However, when I try that in the start script, I never seem to get a response back from global.html. Does anyone know a solution to this? Or am I just overlooking something?
When you use a start script, you are injecting it and accessing that page's DOM. Rather than making calls against the SafariExtension class, you'll want to make them against the SafariContentExtension class. This is a relatively minor difference, but it makes all the difference when you are accessing extension functionality from an injected script rather than a global page.
EDIT: Since answering this question a couple of days ago, I've begun working on adding a custom settings page to my own Safari extension. It appears that you can only access the baseURI variable of your extension using the SafariContentExtension class. Apparently Apple won't give that class the same access as the full SafariExtension one.

Can we insert javascript into any webpage loaded in the browser

I am looking into methods to inject javascript into any webpage loaded in the browser, so that I can traverse through the page's DOM. I use JQUERY for my scripting needs.
Method should work in all browsers.
I tried using IFRAME and adding some html into it, but I cant. Please suggest some ways.
Try using Greasemonkey: http://www.greasespot.net/. You can use it to execute custom scripts on page load for any website you want. You can find some basic tutorials here: http://wiki.greasespot.net/Tutorials.
I suggest to create a page with two iframes one to navigate to the designated website and other to get DOM Objects.
in the first one navigate to the site and then select its HTML and
append it in the body of the second Iframe.
iframe2.contentWindow.document.body.innerHTML = iframe1.contentWindow.document.body.innerHTML
then traverse the DOM Objects inside the second Iframe with your custom functions
There are a couple of approaches to solve this problem.
Using BookMarklet
You can create a simple bookmarklet which injects jQuery on the page and you can open Dev Console in your favorite browser and try out your DOM inspection using jQuery or whatever you want to try out.
Use Requestly Chrome Extension
You can use Requestly Script Rule to insert scripts on any webpage. Since your post mentions that you need jQuery, Requestly provides an option to include jQuery as well.
So with a simple click, you can write jQuery supported code without worrying about how jQuery will come in the page. Check these screenshots for reference :-
Script Rule Selection
Sample Script Supported by jQuery
Advantages
A Couple of advantages with using Requestly
You can share the script with other Users using the Requestly Share feature
jQuery is by default supported
After creating the rule, you can simply disable it once you are not using it.
Requestly is available on Firefox as well as Chrome so you can run your script cross-browser.
PS: This may be an older post but answering here because the question is still relevant.
Disclaimer: I am the founder of Requestly So you blame me if you don't like something.
You could create a bookmarklet (see http://en.wikipedia.org/wiki/Bookmarklet) which in turn can add a node to the page, with the src pointing to where your own javascript is located. Onde the script node gets added it will run.
You can find more details on http://www.johnvey.com/features/deliciousdirector/ under "how does it work?". This way you can have a bookmark in your bookmarks bar which, when click, will add your script to any page you happen to be on.
I'm using for Chrome TamperMonkey to add custom scripts for a specific web page which is as well awesome and I can really recommend it.
If I understand correctly, you want to execute a javascript code in any websites you are using in any browser. That means you have to go browser by browser different applications. In chrome supports extension same as firefox, edge, safari browsers supports add-ons. You can add the relevant extension to do that. I am using a scripting extension, that can run the given script whenever I open the page with same URL given in the script. Likewise you have to find different applications for different browsers.
You can't run Javascript on arbitrary Web pages that you do not control the content of. It would be a huge security hole if that were not true.
Think about it: you could run Javascript and wait for someone to log on to their internet banking and then do something with the characters input.
Take a look at jquery JSON and Wikipedia's JSON page.
Alternatively you can simply add a <script> tag to the document:
$("head").append('<script src="..." type="text/javascript"></script>');
This will load the javascript file.

Categories