Talk:Location hack
GM_eval vs. eval(s, unsafeWindow) vs. LocationHack
GM_eval()
will not be implemented.
- The second argument of eval was removed.
- Running
eval()
in chrome scope is a very dangerous thing to do. - 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:
- Whoops, true that I missed adding a comment there. But it was because I created Reading Content Globals to contain this topic (and added it to the "see also" section). It works consistently, and well, and does not involve polling. Arantius 13:17, 27 September 2010 (UTC)
- 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 and testcase below. Cacycle 00:53, 5 September 2010 (UTC)
// ==UserScript== // @name location hack polling // @include *wiki* // ==/UserScript== window.Polling = function() { if (globalNode.value != '') { GM_log(globalNode.value); } else { GM_log('polling'); setTimeout(Polling, 10); } } window.globalNode = document.createElement('textarea'); globalNode.id = 'globalNode' document.body.appendChild(globalNode); location.href = 'javascript:document.getElementById(\'globalNode\').value = skin; void(0);'; Polling();
Alternate solution sketch for reading page scope variables
While I don't know why Arantius removed the location hack helper (even the tersest change message would be useful), I can guess that it wasn't a dependable solution.
I think some asynchronous solution might be more backwards-and-forwards compatible and dependable, even though the API is less friendly to javascript beginners, and the code size explodes. Maybe something like this (untested):
// Query page javascript for the identifier "name", and call callback(value), // when found, or undefined, if not found or some error occurred. This works // only for values that can be JSON serialized -- numbers, strings, booleans, // null, or nested structures like Arrays and Objects that only contain above // mentioned types of data. function queryContentVar(name, callback) { // makes a random 20-char lowercase id function random() { var rand = ; while (rand.length < 20) rand += String.fromCharCode(97 + Math.random() * 26 | 0); return rand; } // Creates a mostly anonymous <meta> tag in the <head> section, giving it a // secret id we'll use for messaging back and forth with content space, and // fires pageInit() in the page scope (to listen and respond to queries from // us about variables) and registers reply() for answers from it at this end. function privInit() { var node = document.createElement('meta'), secret, head = document.getElementsByTagName('head')[0]; node.id = secret = random(); node.addEventListener(id, reply, false); head.appendChild(node); location.href = 'javascript:('+ pageInit +')('+ JSON.stringify(secret) +')'; return node; } // Grabs the meta tag, anonymizes it and listens for incoming queries from us. function pageInit(secret) { // Replaces given <meta content="variable.name"> with <meta content="value"> // (after JSON encoding it) and then alerts the caller to grab the response. function reply() { var name = node.getAttribute('content'), mesg = document.createEvent('Events'), val; try { val = eval(node.getAttribute('content')); val = JSON.stringify(val); } catch(e) { val = ; } node.setAttribute('content', val); mesg.initEvent(secret, true, false); node.dispatchEvent(mesg); } var node = document.getElementById(secret); node.addEventListener(secret + '?', reply, false); node.removeAttribute('id'); } var self = queryContentVar, node = self.node = self.node || privInit(), id = self.secret = self.secret || node.id, mesg = document.createEvent('Events'); mesg.initEvent(secret + '?', true, false); node.setAttribute('content', name); self.callback = callback; node.dispatchEvent(mesg); // Picks up the response from content space, decodes it, passes it on to given // response callback and resets everything (just in case) to handle new calls. function reply() { var cb = self.callback, is = node.getAttribute('content'); if (cb) cb(is === ? undefined : JSON.parse(is)); node.removeAttribute('name'); delete self.callback; } }
--Ecmanaut 23:58, 26 September 2010 (UTC)
coach uk bag coach bag outlet online
Yesterday and baeing The german language Creek Lin showed up In spite of the cloudy day once the rainwater while, this individual were adament on the trip after this, the ultimate Three months a long way absent, the street got 8 . 5 several hours. He or she had been associated with his or her companion Wu Lin as well as the various other the German. Prior to which, they begin coming from Yunnan Shangri-La, completely across the Yangtze Lake driving, riding Four,Thousand a long way throughout Sixty six days and nights.
The german language rider present his or her vacation records to the Pond Lin, the present 4,500 kilometer trip four or five times a year in mere his / her ride with time.
coach pink bag small coach bag
The other day and baeing The german language Creek Lin showed up Inspite of the cloudy evening if the rain when, this individual was adament on the experience next, the last Ninety days a long way apart, the road got Eight and a half hrs. He was together with his / her associate Wu Lin and the various other a new German. Ahead of that will, they begin through Yunnan Shangri-La, entirely along the Yangtze River operating, driving Several,1000 mls in 66 days and nights.
German riders demonstrate their take a trip notes for the Water Lin, the current Four,Thousand kilometers journey 4 to 5 occasions per year within his or her journey with time.
coach bag black coach bag
Yesterday and baeing In german Creek Lin came Despite the cloudy morning when the rainwater whenever, he were adament around the experience following this, the ultimate Ninety days mls aside, the street took 8 . 5 hours. This individual ended up being combined with his / her associate Wu Lin and the other a The german language. Ahead of that will, they begin through Yunnan Shangri-La, all the way up across the Yangtze Lake riding, operating Some,Thousand kilometers inside Sixty six days and nights.
The german language participant display his take a trip records to the Lake Lin, the existing Several,Thousand kilometres trip 4 to 5 occasions 12 months in only his / her experience soon enough.
coach on sale coach hand bag
The other day and baeing The german language Creek Lin appeared Despite the overcast day once the rainwater while, he was adamant around the journey after that, the ultimate Three months a long way apart, the path took 8 and a half several hours. This individual had been together with their associate Wu Lin and also the other the The german language. Prior to that will, they begin through Yunnan Shangri-La, completely down the Yangtze Water operating, using Some,1000 mls within 66 times.
The german language driver show his or her journey paperwork for the River Lin, the actual 4,500 kilometres ride four or five instances annually within his ride in time.