Last active
August 29, 2015 14:04
-
-
Save RutledgePaulV/11f477f342da4a8af842 to your computer and use it in GitHub Desktop.
Sahi Browser Function Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This utility allows us to ignore using Sahi's <browser> </browser> tags for defining functions that should be run on the browser. | |
* Instead we can simply call these utilities to pass functions back and forth between the two, which is super useful. | |
*/ | |
var $Browser = (function ($Browser) { | |
/** | |
* Adds a function to the window object on the browser side of things. | |
* | |
* @param $key - The name that should be assigned to the function on the browser. | |
* @param $func - The function that should be used. | |
*/ | |
$Browser.addFunction = function ($key, $func) { | |
if (!_condition(window.hasOwnProperty($key))) { | |
_call(window[$key] = $func); | |
} else { | |
_fail("Function already exists on window object."); | |
} | |
}; | |
/** | |
* Calls a function on the window object and passes along any provided arguments after the key. | |
* | |
* @param $key {string} The name of the function you want to call. | |
* @param [$args] {...} Any arguments that need to be provided to the function you are calling. | |
* | |
* @returns {*} The result of the function you defined, (or nothing if a void function). | |
*/ | |
$Browser.callFunction = function ($key, $args) { | |
if (_condition(window.hasOwnProperty($key))) { | |
var $args = [].splice.call(arguments, 1); | |
return _fetch(window.callFunction($key, $args)); | |
} else { | |
_fail("Requested function did not exist on the window object."); | |
} | |
}; | |
/** | |
* Redefines a function on the window object using the provided key. This is just to avoid developer stupidity. | |
* | |
* @param $key {string} The name of the function you want to redefine | |
* @param $func {Function} The function to be defined for the given key. | |
*/ | |
$Browser.redefineFunction = function ($key, $func) { | |
if (_condition(window.hasOwnProperty($key))) { | |
_call(window[$key] = $func); | |
} else { | |
_fail("The function does not yet exist and therefore cannot be redefined."); | |
} | |
}; | |
/** | |
* Removes a function definition for a given key on the browser. | |
* | |
* @param $key {string} The name of the function to be removed. | |
*/ | |
$Browser.removeFunction = function ($key) { | |
if (_condition(window.hasOwnProperty($key))) { | |
_call(window[$key] = undefined); | |
} else { | |
_fail("The function did not exist and so cannot be removed."); | |
} | |
}; | |
return $Browser; | |
})($Browser || {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Browser.addFunction("sibling", function (node) { | |
return node.nextElementSibling; | |
}); | |
var $results = $Browser.callFunction("sibling",_link({id:"_sahi_ignore_1"})); | |
_highlight($results); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function provides a work around to avoid using these browser tags elsewhere, instead we can call browser side functions | |
* straight from a sahi script file and get the appropriate result on the proxy side. | |
*/ | |
<browser> | |
function callFunction($name, $args){ | |
return window[$name].apply(window, $args); | |
} | |
</browser> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment