Talk:Location hack: Difference between revisions

From GreaseSpot Wiki
Jump to navigationJump to search
m (Reverted edits by 117.40.196.156 (Talk) to last revision by Arantius)
(polling)
Line 16: Line 16:
As written that example should never have worked.
As written that example should never have worked.
[[User:Arantius|Arantius]] 12:41, 16 September 2009 (EDT)
[[User:Arantius|Arantius]] 12:41, 16 September 2009 (EDT)
== Polling ==
[[User:Arantius|Arantius]] has removed the following passage that I had added:
: It is possible that the location.href code is excuted (much) later than the readout code in the following lines. This breaks the location hack. (Observed in Firefox 3.6.8)
This happened with Firefox 3.6.8 and broke the Wikipedia editor [http://en.wikipedia.org/wiki/User:Cacycle/wikEd wikEd]. The only work around was to poll the location hack element after having set location.href. Independent of the poll delay time the results arrives usually with the 1st or 2nd poll. I have to apply the location hack during the page loading period before the load event fires. Event triggering instead of polling was not possible because during the page loading none of the tested events fired (e.g. click) (Arantius also reverted that remark [http://wiki.greasespot.net/index.php?title=Generate_Click_Events&curid=1943&diff=5592&oldid=5591]). Please see the current working code below. [[User:Cacycle|Cacycle]] 22:54, 3 September 2010 (UTC)
<pre>
//
// WikEdGetGlobal: parse global variables into hash, uses Greasemonkey 'location hack' (code copied to wikEdDiff.js)
//
window.WikEdGetGlobal = function(globalNameArray) {
// work around sandboxing using Greasemonkey location hack
    if ( (wikEdGreasemonkey == true) && (typeof(JSON) == 'object') ) {
        var scheduled = 0;
        for (var i = 0; i < globalNameArray.length; i ++) {
            var globalName = globalNameArray[i];
// create single use textarea for text exchange
            var globalNode = document.createElement('textarea');
            globalNode.id = 'wikEdGetGlobalNode' + wikEdGetGlobalNodeId;
            globalNode.style.display = 'none';
            wikEdGetGlobalNodeRefs[wikEdGetGlobalNodeId] = globalNode;
            document.body.appendChild(globalNode);
// set location href to execute code in global context
            location.href = 'javascript:'
            + 'var node = document.getElementById(\'wikEdGetGlobalNode' + wikEdGetGlobalNodeId + '\');'
            + 'if (typeof(' + globalName + ') != \'undefined\') {'
            + '  node.value = JSON.stringify( { \'' + globalName + '\': ' + globalName + ' } );'
            + '}'
            + 'else {'
            + '  node.value = \'\\n\''
            + '}'
            + 'void(0);';
            scheduled ++;
            wikEdGetGlobalNodeId ++;
        }
// start polling for the arrival of asynchronous results
        if (scheduled > 0) {
            WikEdGetGlobalPolling();
        }
    }
    return;
};
//
// WikEdGetGlobalPolling: poll location hack DOM elements for asynchronously arriving content
//  in Firefox 3.6.8 usually the 1st or 2nd poll independently of delay time
window.WikEdGetGlobalPolling = function() {
    var scheduled = 0;
    for (var i = 0; i < wikEdGetGlobalNodeId; i ++) {
      var node = wikEdGetGlobalNodeRefs[i];
      if (node != null) {
          scheduled ++;
          var nodeValue = node.value;
          if (nodeValue != '') {
              if (nodeValue != '\n') {
                    var obj = JSON.parse(nodeValue);
                    for (var key in obj) {
                        if (obj.hasOwnProperty(key) == true) {
                          wikEdWikiGlobals[key] = obj[key];
                        }
                    }
                }
                document.body.removeChild(node);
                wikEdGetGlobalNodeRefs[i] = undefined;
                scheduled --;
            }
        }
    }
// no longer waiting for nodes
    if (scheduled == 0) {
        wikEdGetGlobalNodeId = 0;
    }
// schedule next poll (ms)
    else {
        window.setTimeout(WikEdGetGlobalPolling, 10);
    }
    return;
}
</pre>

Revision as of 22:54, 3 September 2010

GM_eval vs. eval(s, unsafeWindow) vs. LocationHack

GM_eval() will not be implemented.

  1. The second argument of eval was removed.
  2. Running eval() in chrome scope is a very dangerous thing to do.
  3. I'm not confident that it actually worked to solve the problems that the location hack does. (I'm confident that it does not, on FF 3.0.13 on Linux. Perhaps it did in older versions, before it was removed.)

Location Hack Broken

-Removed-

Fromp: You are confused about what the location hack is supposed to accomplish. It's for letting a user script call into a function (for example) defined in the page. Your example which I just removed was doing the opposite. As written that example should never have worked. Arantius 12:41, 16 September 2009 (EDT)

Polling

Arantius has removed the following passage that I had added:

It is possible that the location.href code is excuted (much) later than the readout code in the following lines. This breaks the location hack. (Observed in Firefox 3.6.8)

This happened with Firefox 3.6.8 and broke the Wikipedia editor wikEd. The only work around was to poll the location hack element after having set location.href. Independent of the poll delay time the results arrives usually with the 1st or 2nd poll. I have to apply the location hack during the page loading period before the load event fires. Event triggering instead of polling was not possible because during the page loading none of the tested events fired (e.g. click) (Arantius also reverted that remark [1]). Please see the current working code below. Cacycle 22:54, 3 September 2010 (UTC)

//
// WikEdGetGlobal: parse global variables into hash, uses Greasemonkey 'location hack' (code copied to wikEdDiff.js)
//

window.WikEdGetGlobal = function(globalNameArray) {

// work around sandboxing using Greasemonkey location hack
    if ( (wikEdGreasemonkey == true) && (typeof(JSON) == 'object') ) {
        var scheduled = 0;
        for (var i = 0; i < globalNameArray.length; i ++) {
            var globalName = globalNameArray[i];

// create single use textarea for text exchange
            var globalNode = document.createElement('textarea');
            globalNode.id = 'wikEdGetGlobalNode' + wikEdGetGlobalNodeId;
            globalNode.style.display = 'none';
            wikEdGetGlobalNodeRefs[wikEdGetGlobalNodeId] = globalNode;
            document.body.appendChild(globalNode);

// set location href to execute code in global context
            location.href = 'javascript:'
            + 'var node = document.getElementById(\'wikEdGetGlobalNode' + wikEdGetGlobalNodeId + '\');'
            + 'if (typeof(' + globalName + ') != \'undefined\') {'
            + '  node.value = JSON.stringify( { \'' + globalName + '\': ' + globalName + ' } );'
            + '}'
            + 'else {'
            + '  node.value = \'\\n\''
            + '}'
            + 'void(0);';
            scheduled ++;
            wikEdGetGlobalNodeId ++;
        }

// start polling for the arrival of asynchronous results
        if (scheduled > 0) {
            WikEdGetGlobalPolling();
        }
    }
    return;
};


//
// WikEdGetGlobalPolling: poll location hack DOM elements for asynchronously arriving content
//   in Firefox 3.6.8 usually the 1st or 2nd poll independently of delay time

window.WikEdGetGlobalPolling = function() {
    var scheduled = 0;
    for (var i = 0; i < wikEdGetGlobalNodeId; i ++) {
      var node = wikEdGetGlobalNodeRefs[i];
      if (node != null) {
          scheduled ++;
          var nodeValue = node.value;
          if (nodeValue != '') {
              if (nodeValue != '\n') {
                    var obj = JSON.parse(nodeValue);
                    for (var key in obj) {
                        if (obj.hasOwnProperty(key) == true) {
                           wikEdWikiGlobals[key] = obj[key];
                        }
                    }
                }
                document.body.removeChild(node);
                wikEdGetGlobalNodeRefs[i] = undefined;
                scheduled --;
            }
        }
    }

// no longer waiting for nodes
    if (scheduled == 0) {
        wikEdGetGlobalNodeId = 0;
    }

// schedule next poll (ms)
    else {
        window.setTimeout(WikEdGetGlobalPolling, 10);
    }
    return;
}