v8/tools/link_clicker.extension/background.js
Camillo Bruni 2bd86a074e [tools] Add link_click chrome extension
This extension imitates user-interaction on a page by randomly clicking on
links matching a given regexp pattern.

Change-Id: I210bebf36ce6e3a3c785953010ce21528093d1af
NOTRY=true

Change-Id: I210bebf36ce6e3a3c785953010ce21528093d1af
Reviewed-on: https://chromium-review.googlesource.com/500247
Commit-Queue: Camillo Bruni <cbruni@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#45201}
2017-05-09 15:25:21 +00:00

75 lines
2.0 KiB
JavaScript

// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(function linkClickerBackgroundScript() {
// time in ms.
let minInterval = 1*1000;
let maxInterval = 20*1000;
let pattern = /.*/;
let enabled = false;
let animateIconIntervalId;
// ===========================================================================
chrome.runtime.onMessage.addListener(function(msg, sender, response) {
let result;
if (msg.type == 'update') result = updateFromMessage(msg);
if (msg.type == 'get') result = getValues();
response(result);
});
// ===========================================================================
function updateFromMessage(msg) {
console.log(msg);
minInterval = Number(msg.minInterval)
maxInterval = Number(msg.maxInterval);
if (maxInterval < minInterval) {
let tmpMin = Math.min(minInterval, maxInterval);
maxInterval = Math.max(minInterval, maxInterval);
minInterval = tmpMin;
}
pattern = new RegExp(msg.pattern);
enabled = Boolean(msg.enabled);
updateTabs();
scheduleIconAnimation();
return getValues();
}
function getValues() {
return {
type: 'update',
minInterval: minInterval,
maxInterval: maxInterval,
pattern: pattern.source,
enabled: enabled
}
}
function updateTabs() {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
let message = getValues();
for (let i = 0; i < tabs.length; ++i) {
chrome.tabs.sendMessage(tabs[i].id, message);
}
});
}
let animationIndex = 0;
function animateIcon() {
animationIndex = (animationIndex + 1) % 4;
chrome.browserAction.setBadgeText( { text: ".".repeat(animationIndex) } );
}
function scheduleIconAnimation() {
chrome.browserAction.setBadgeText( { text: "" } );
clearInterval(animateIconIntervalId);
if (enabled) {
animateIconIntervalId = setInterval(animateIcon, 500);
}
}
})();