I am writing a Chrome Extension to be executed when the user is looking at a panorama image in Google Maps, and I am having problems getting wheel movements to be propagated to Google Maps. I want this to propagate so the user can zoom the image from within my extension.
I have tried to ignore the event completely in my injection script, but that doesn't work. I then tried to capture the event and then pass it on via JavaScript, and that doesn't work either. The event is always captured, but passing it on doesn't work.
When the cursor escapes the scope of my Extension (because the mouse is moved quickly outside of my 'div'), the wheel event is being passed to Google Maps correctly.
I have tried the capture with
expandingBox = document.createElement('div');
expandingBox.addEventListener('wheel', mouseWheel, false);
and
expandingBox = document.createElement('div');
expandingBox.addEventListener('wheel', mouseWheel, true);
and
$(document).addEventListener('wheel', mouseWheel, true);
and
$(document).addEventListener('wheel', mouseWheel, false);
The capture function is very simple:
function mouseWheel( e ) {
// alert("Wheel was moved");
$(document).trigger("wheel", e);
return true;
}
I have also tried with "return false", with '$(window)', and '$(window.parent)' in the function mouseWheel. All to no avail.
Is it even possible to do what I am attempting to do?
A minimal implementation that tests this functionality is:
manifest.json
{
"name": "Wheel Test",
"version": "0.3",
"description": "See if I can read and pass a Wheel event",
"background": {
"persistent": false,
"scripts": ["background.js", "jquery-3.3.1.min.js" ]
},
"default_locale": "en",
"permissions": [
"activeTab",
"notifications",
"<all_urls>"
],
"browser_action":{
"default_icon": {
"20": "data/icons/QRC-Ninja-20.png"
}
},
"manifest_version": 2
}
background.js
"use strict";
function injectCode( tab ){
chrome.tabs.insertCSS(tab.id, { file: 'data/inject/inject.css' } );
chrome.tabs.executeScript(tab.id , { file: 'jquery-3.3.1.min.js'});
chrome.tabs.executeScript(tab.id , { file: 'data/inject/inject.js'});
}
// Listen for a click on the icon. On that click, inject the necessary code and get going.
chrome.browserAction.onClicked.addListener( injectCode );
inject.css
.expanding-box {
box-sizing: border-box;
position: fixed;
z-index: 2147483645;
border: 0px solid;
width: 0;
height: 0;
}
inject.js
'use strict';
var guide = window.guide;
var expandingBox = null;
try {
guide.remove();
} catch (e) {}
guide = (function () {
function stop(){
guide.remove();
}
function mouseWheel( e ) {
// alert("Wheel was moved");
var result = expandingBox.dispatchEvent( e );
return true;
}
return {
install: function () {
expandingBox = document.createElement('div');
expandingBox.setAttribute('class', 'expanding-box');
expandingBox.setAttribute('id', 'expandingBox');
document.body.appendChild(expandingBox);
//
// Initial position is the top left of the window
//
expandingBox.style.borderWidth = '4px';
expandingBox.style.borderColor = 'red';
expandingBox.style.width = '200px';
expandingBox.style.height = '200px';
expandingBox.addEventListener('wheel', mouseWheel, false);
document.addEventListener('mouseup', stop, false);
},
remove: function () {
expandingBox.removeEventListener('wheel', mouseWheel, false);
document.removeEventListener('mouseup', stop, false);
if (expandingBox && expandingBox.parentNode) {
expandingBox.parentNode.removeChild(expandingBox);
}
}
};
})();
guide.install();
Regarding the question "What is the purpose?" The purpose of the code is to analyse an area of interest that the user points to.
Thanks in advance for your help!
In the meantime, I discovered that fotijr has provided an answer that worked.