Google Chrome "Application Shortcut": How to auto-load JavaScript?
Solution 1:
Chrome extensions and their content scripts are also loaded when Chrome starts in the App mode.
So, you can create a simple extension, which injects JavaScript code in the page as follows:
1. Create a manifest.json
file
{
"name": "Run code on twitter mobile",
"version": "1.0",
"manifest_version": 2,
"content_scripts": [
{
"js": [
"contentscript.js"
],
"matches": [
"https://mobile.twitter.com/*"
]
}
],
"web_accessible_resources": [
{
"resources": [
"script.js"
],
"matches": [
"https://mobile.twitter.com/*"
]
}
]
}
2. The content script
Then, create a file called contentscript.js
, and add the desired JavaScript code.
This script is included at every load of the matched page. All DOM methods, via the document
object is directly available. However, window
and document.defaultView
do not point to the window
object in the page [source].
If you want to access global methods or properties, you have to dynamically create a <script>
, and inject it in the page (see Building a Chrome Extension - Inject code in a page using a Content script).
contentscript.js
var s = document.createElement('script');
s.src = chrome.extension.getURL('script.js');
(document.head||document.documentElement).appendChild(s);
s.onload = function() {
s.parentNode.removeChild(s);
};
3. The script which will be injected
Then, create a file called script.js
, and place it in the same folder as manifest.json
and contentscript.js
. The code in script.js
executes as if it was a true part of the affected page.
Reference for content scripts.
Reference for web accessible resources.