0

I'm writing a Chrome extension. I'm trying to get my script to open a new tab and enter text in the search, but I am having problems with synchronization it seems. Here is my code:

helloworld.html:

<html>
  <h1 id="test"></h1>

  <head>

  <script src="navigate.js"></script>

  </head>
</html>

manifest.json

{
  "name": "Sonic",
  "version": "0.1",
  "manifest_version": 2,
  "description": "My first Chrome extension.",
  "permissions": [ "tabs" ],

  "browser_action": {
    "default_icon": "Sonic_Sprite.png",
    "default_popup": "helloworld.html"
  },

  "content_scripts": [
    {
      "matches": ["http://stackoverflow.com/questions/*/*"],
      "js": ["jquery-1.11.3.js", "content.js"]
    }
  ]
}

content.js

var raw_title = document.getElementsByClassName("question-hyperlink")[0];

var kickass = window.open("https://kickass.unblocked.pe/");

$(kickass.document).ready( function() {
    kickass.alert("hello");
    var s = kickass.document.getElementById("contentSearch");
    alert(s);
});

I am getting unpredictable behavior from jquery's ready() function. Maybe 20% of the times I load the page it gives me an alert.

Is this a common error, and if so what can be done to fix it? I played around with onload before moving on to jquery, and it produced even worse results in terms of triggering the alert.

Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
  • Do you mean you've refreshed the main page when the pop-up was already open? Check [strWindowName](https://developer.mozilla.org/en-US/docs/Web/API/Window/open) argument, and notice, that it should vary every time you open a window, to really open a new window. – Teemu Jan 07 '16 at 12:08
  • I refresh the page to re-run all the code. In the scenario where everything works I don't do any refreshing. I use window.open() with only a URL, with the intent of creating a new tab/window, which works just fine. I don't understand where strWindowName come it to this. – Harald Nordgren Jan 07 '16 at 12:40
  • "with the intent of creating a new tab/window" Opening a window without a name actually ruins your goal. Without a diffrent name, a yet opened window is not re-opened, it's just focused, that's why it looks like `ready()` was firing randomly. – Teemu Jan 07 '16 at 12:43
  • Where does it state that window.open() follows this behavior? I played around with strWindowName -- trying time as in this example: http://forums.asp.net/t/1052977.aspx?blank+strWindowName+problem -- but the result is the same. Every time I refresh the stackoverflow page, or open a new tab and paste the URL, the script runs and opens a (seemingly) new tab with https://kickass.unblocked.pe/ , The problem is that this very rarely triggers `ready()`. – Harald Nordgren Jan 07 '16 at 13:03
  • Okay, it's not the the triggering of `ready()` that is the problem, after all. But rather the kickass object in its own ready handler. If I change to simply `alert("hello")` then it works every time. As soon as I try to invoke the object that I just made sure was ready the I get unpredictable results. The code below it doesn't run either, which I assume just means that Javascript crashes/aborts execution when I try to call `alert()` on an invalid object. – Harald Nordgren Jan 07 '16 at 13:19

1 Answers1

0

I don't know this is the solution but this should work

var raw_title = document.getElementsByClassName("question-hyperlink")[0];

var kickass = window.open("https://kickass.unblocked.pe/");
var firstload= "false";
localStorage.setItem("someVarName", someVarName)
$(kickass.document).ready( function() {
var someVarName = localStorage.getItem("someVarName");
if(someVarName=="false")
{
    kickass.alert("hello");
    var s = kickass.document.getElementById("contentSearch");
    alert(s);
}
else
{
//page is not loading first time do the stuff you want to
}
});

Read Localstorage here

When you want to remove that from localstorage just call like this

var someVarName = localStorage.removeItem(getItem("someVarName"))
Community
  • 1
  • 1
Just code
  • 13,553
  • 10
  • 51
  • 93
  • I copied you extra lines verbatim into mine. Using the name `someVarName`, and doing nothing in the else block. It appears however that `ready()` does work -- if I do `alert("hello")` then I always get a response. The kickass object appears to be the one causing problems. (See my reply to Teemu's comment above.) – Harald Nordgren Jan 07 '16 at 13:25
  • [You need session storage. use session storage and let me know](http://stackoverflow.com/a/5523174/2630817) – Just code Jan 07 '16 at 13:31
  • No luck with sessionStorage either. Doing `setItem("someVarName", someVarName)` with an undeclared variable crashes the code, but I worked around that by declaring it on the line before and then removing `var` in the `ready()` handler. This doesn't solve my main problem, though. The code always goes to the else block for localStorage/sessionStorage, and the kickass obejct is still invalid it its ready handler. – Harald Nordgren Jan 07 '16 at 13:55
  • I am not getting your requirement now. can you explain? – Just code Jan 07 '16 at 15:48
  • I simply want to work with the kickass object, do `getElementById()`, then fill the textarea, do a search and then take it from there. Without calling ready() I was not able do actions on the kickass object, so my assumption was that I needed to load the page properly first. – Harald Nordgren Jan 07 '16 at 16:39
  • After intensive googling, it appears that the Same-origin policy (https://en.wikipedia.org/wiki/Same-origin_policy) is the main culprit here. Some user here on SO even called it a design flaw to try to write scripts that communicate between domains. Oh well. – Harald Nordgren Jan 07 '16 at 17:01
  • what you come out with then? – Just code Jan 08 '16 at 05:11