I'm writing an app to try and modify a page using JavaScript and I'm having trouble figuring out where to put the JavaScript I want to run. I am using the KRL specific jQuery handle of $K.
There are two ways to put javascript directly onto a webpage inside of a KRL ruleset. Through an emit block or an external resource. Let's discuss emit blocks first.
Emits can appear in the action block of a rule, or in a ruleset's global block. You can use an emit with either a heredoc or clownhats , but clownhats are preferred.
Here's an example of emitting javascript in a rule:
rule emitter {
select when web pageview "exampley.com"
{
emit <|
$K("body").append("Hello from a rule.");
|>;
}
}
Here's an example of emitting javascript in a global block:
global {
emit <|
$K("body").append("Hello from the global block.");
|>;
}
The second way to place javascript on a page in KRL is to use an external resource .
I'm going to refer you to the linked docs for the specifics, but you include an external javascript resource in the meta block of your ruleset like so:
use javascript resource "url-to-resource"
Let's say I have the following is the content of a javascript file located at my-personal-website.com/awesome.js
:
$K("body").append("Hello from an external resource.");
To use that in a ruleset:
meta {
// normal meta stuff...
use javascript resource "my-personal-website.com/awesome.js"
}
Here's a complete ruleset showing the 2 different ways to place javascript on a page in a KRL ruleset:
ruleset a369x151 {
meta {
name "fun-with-javascript"
description <<
fun-with-javascript
>>
author "AKO"
logging on
use javascript resource "http://dl.dropbox.com/u/4917848/js/example-external-resource.js"
}
global {
emit <|
$K("body").append("<br/>Hello from the global block.");
|>;
}
rule emitter {
select when web pageview "exampley.com"
{
emit <|
$K("body").append("<br/>Hello from a rule.");
|>;
}
}
}
Action shot after running app on exampley.com :
You can try it out yourself by getting the app bookmarklet here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.