2 // @name Userscript @run-at example.
5 // @description This script demonstrates @runat by listing which resources
6 // the current page has loaded.
8 // @run-at document-start
12 * Copyright (c) 2011 The Chromium Authors. All rights reserved.
13 * Use of this source code is governed by a BSD-style license that can be
14 * found in the LICENSE file.
21 * Called whenever the page loads a subresource.
22 * Logs all loaded URLs in the urls var.
24 document.addEventListener('beforeload', function(evt) {
33 * Called when the window is finished loading.
34 * Loops through the urls var and prints its contents to the page DOM.
36 window.addEventListener('load', function() {
41 // Create and style inserted DOM elements.
42 var urls_dom = document.createElement('ul');
43 urls_dom.style.cssText = [
44 'margin: 4px 0 !important;',
45 'padding: 0 !important;',
47 'word-wrap: break-word;',
51 var wrap_dom = document.createElement('div');
52 wrap_dom.style.cssText = [
53 'background-color: #ff7357;',
54 'background-image: -webkit-repeating-linear-gradient(' +
55 '-45deg, transparent, transparent 35px,' +
56 'rgba(0,0,0,.1) 35px, rgba(0,0,0,.1) 70px);',
61 var title_dom = document.createElement('strong');
62 title_dom.textContent = (count > 1) ?
63 'This page has loaded the following resources:' :
64 'This page loaded the following resource:';
65 wrap_dom.appendChild(title_dom);
66 wrap_dom.appendChild(urls_dom);
68 // Render each url as a list item containing a link.
69 for (var url in urls) {
70 var url_dom = document.createElement('li');
71 var link_dom = document.createElement('a');
72 var times_dom = document.createElement('span');
73 url_dom.style.cssText = [
74 'list-style-type: disc;',
75 'margin: 0 0 0 30px !important;',
76 'padding: 2px !important'
78 link_dom.setAttribute('href', url);
79 link_dom.setAttribute('target', '_blank');
80 link_dom.textContent = url;
81 link_dom.style.cssText = [
82 'color: #000 !important;'
84 times_dom.textContent = (urls[url] > 1) ?
85 ' (' + urls[url] + ' times)' :
87 url_dom.appendChild(link_dom);
88 url_dom.appendChild(times_dom);
89 urls_dom.appendChild(url_dom);
92 // Insert the created DOM into the page.
93 document.body.style.position = 'relative';
94 document.body.parentElement.insertBefore(wrap_dom, document.body);