-1

I have written javascript that I am currently excecuting in the devtools of chrome (the console section). Is there any way to do that in javascript without me having to open the page, open the console, type it in, etc. I would be doing this from an external page. If this is confusing here is an example:

mypage.com

<script>
function myFunc(){return document.getElementById("hi")};
</script>

targetpage.com

<p id="hi">Hello world</p>

In this case, how can I run myFunc on targetpage.com from mypage.com?

thesecretmaster
  • 1,950
  • 1
  • 27
  • 39
  • 1
    possible duplicate of [Loading cross domain html page with jQuery AJAX](http://stackoverflow.com/questions/15005500/loading-cross-domain-html-page-with-jquery-ajax) – Phil Sep 17 '15 at 02:47
  • @Phil I'm not using jquery or ajax – thesecretmaster Sep 17 '15 at 02:47
  • Read the answers before you dismiss the duplicate vote just because you *think* it doesn't match what you're after based on the title. Also, if you want to access a remote document via JavaScript, you're most probably going to use AJAX – Phil Sep 17 '15 at 02:49
  • If you have code that you **always** want to run on a particular page, you can use greasemonkey in Firefox or tampermonkey in Chrome or YSOOL in IE/Edge - these addons inject **your** script into **any** webpage ... P.S. YSOOL = `your're s%*t out of luck` in IE/Edge :p – Jaromanda X Sep 17 '15 at 04:35

1 Answers1

0

Web browsers (by design) explicitly prohibit what you are trying to achieve. The JavaScript can only run on a page that originated from the same server.

The only way to "run" the code from a different source is via eval()

The way eval() works is you provide is with a JavaScript "text" that it will dynamically execute.

As mentioned in the comments - eval() is very evil.

eval() executes code provided as a text. For instance:

var code = "var i = 10; alert(i);";
eval(code);

The above lines will pop an alert window displaying "10".

The main point of the answer is: you cannot do what you are hoping to achieve.

YePhIcK
  • 5,816
  • 2
  • 27
  • 52
  • Your last statement doesn't sound correct at all. I'm not even sure what you're trying to say there – Phil Sep 17 '15 at 02:53
  • Get JavaScript code as text and then feed it into `eval()` – YePhIcK Sep 17 '15 at 02:55
  • 1
    1: **eval** is _evil_ ... 2: **eval** for anything more than a dozen lines is _evil and stupid_ and you may as well use the console or **scratchpad** in firefox, and 3: **eval** is _evil_ – Jaromanda X Sep 17 '15 at 04:39
  • If as you say the only way is with `eval()` could you please explain how that could be done? Otherwise this answer isn't very helpful. – thesecretmaster Sep 17 '15 at 10:05
  • Could I copy the page and run the js on a copy? – thesecretmaster Sep 17 '15 at 22:33
  • What do you mean "copy the page"? Load the page from an external source? In that case be mindful that you would also need to copy any number of "dependent" files (css, js, images - to name the few), not to mention that that external page may very well depend on some back-end (i.e. a database) infrastructure. In general you can not predict what a "copied" page would do if you try to run a script from it. – YePhIcK Sep 18 '15 at 14:24
  • @YePhIcK Could I just download the js and html then run my js on that copy? – thesecretmaster Sep 29 '15 at 01:39
  • You could try and it might even work for the **simplest** of cases. But before you start coding that implementation try that method "manually" - just pick a web page at random, "download it" to your local disk and then try to "run" it and see if that works. If it fails analyze the dependencies (find out what is missing) and download those as well. Continue until that page works. Once you tried this for a few pages you'll see the pattern that may help you design the actual algorithm. Good luck :) – YePhIcK Sep 29 '15 at 08:34