add webextension

This commit is contained in:
Jan Petykiewicz 2017-04-06 18:11:25 -07:00
commit 8bdba90edb
2 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,70 @@
/*
Unused
Listen for messages from the page.
If the message was from the page script, show an alert.
window.addEventListener("message", function(event) {
if (event.source == window &&
event.data.direction &&
event.data.direction == "from-page-script") {
alert("Content script received message: \"" + event.data.message + "\"");
}
});
*/
function handleMoonslicer(message) {
var result = message.querySelector('div.sheet-result')
var rx = /">([0-9]+)<\/span>/
var h0str = result.children[0].children[0].children[0].getAttribute("title");
var h1str = result.children[2].children[0].children[0].getAttribute("title");
var hit0 = Number(h0str.match(rx)[1]);
var hit1 = Number(h1str.match(rx)[1]);
var input_area = document.getElementById('textchat-input');
var textarea = input_area.querySelector("textarea");
var submit_button = input_area.querySelector("button.btn");
console.log('Hit0: ' + hit0 + ' Hit1: ' + hit1);
var old_text = textarea.value;
if (hit0 == 20 || hit1 == 20) {
textarea.value = "20! +[[1d6]] ice";
submit_button.click();
}
if (hit0 == 19 || hit1 == 19) {
textarea.value = "19! +[[1d4]] ice";
submit_button.click();
}
if (hit0 == 2 || hit1 == 2) {
textarea.value = "2! [[floor(1d4/2)]] ice to self";
submit_button.click();
}
if (hit0 == 1 || hit1 == 1) {
textarea.value = "1! [[1d6]] ice to self";
submit_button.click();
}
textarea.value = old_text;
}
/*
Send a message to the page script.
*/
function newMessageHandler(event) {
var message = event.target;
if (message.classList.contains('you')) {
var labelElem = message.querySelector('div.sheet-rolltemplate-atk div.sheet-label');
var label = labelElem.querySelector('span a');
if (label && label.innerHTML == 'Moonslicer') {
handleMoonslicer(message);
};
};
}
/*
Add messagePageScript() as a listener to click events on
the "from-content-script" element.
*/
var textchat = document.getElementById("textchat");
var content = textchat.getElementsByClassName("content")[0];
content.addEventListener("DOMNodeInserted", newMessageHandler);

View File

@ -0,0 +1,19 @@
{
"manifest_version": 2,
"name": "Test roll20",
"description": "Visit https://mdn.github.io/webextensions-examples/content-script-page-script-messaging.html for the demo. See https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Examples#page-to-extension-messaging",
"version": "1.0",
"homepage_url": "https://github.com/mdn/webextensions-examples/tree/master/page-to-extension-messaging",
"icons": {
"48": "icons/message-48.png"
},
"content_scripts": [
{
"matches": ["*://app.roll20.net/editor*"],
"js": ["content-script.js"]
}
]
}