1// Copyright 2017 the V8 project authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5(function linkClickerContentScript() { 6 // time in ms 7 let minInterval; 8 let maxInterval; 9 let pattern; 10 let enabled; 11 let timeoutId; 12 13 // Initialize variables. 14 chrome.runtime.sendMessage({type:'get'}, function(msg) { 15 if (msg.type == 'update') updateFromMessage(msg); 16 }); 17 18 chrome.runtime.onMessage.addListener( 19 function(msg, sender, sendResponse) { 20 if (msg.type == 'update') updateFromMessage(msg); 21 }); 22 23 function findAllLinks() { 24 let links = document.links; 25 let results = new Set(); 26 for (let i = 0; i < links.length; i++) { 27 let href = links[i].href; 28 if (!href) continue; 29 if (href && href.match(pattern)) results.add(href); 30 } 31 return Array.from(results); 32 } 33 34 function updateFromMessage(msg) { 35 console.log(msg); 36 minInterval = Number(msg.minInterval) 37 maxInterval = Number(msg.maxInterval); 38 pattern = new RegExp(msg.pattern); 39 enabled = Boolean(msg.enabled); 40 if (enabled) schedule(); 41 } 42 43 function followLink() { 44 if (!enabled) return; 45 let links = findAllLinks(); 46 if (links.length <= 5) { 47 // navigate back if the page has not enough links 48 window.history.back() 49 console.log("navigate back"); 50 } else { 51 let link = links[Math.round(Math.random() * (links.length-1))]; 52 console.log(link); 53 window.location.href = link; 54 // Schedule in case we just followed an anchor. 55 schedule(); 56 } 57 } 58 59 function schedule() { 60 clearTimeout(timeoutId); 61 let delta = maxInterval - minInterval; 62 let duration = minInterval + (Math.random() * delta); 63 console.log(duration); 64 timeoutId = setTimeout(followLink, duration); 65 } 66})(); 67