Google Chrome“应用程序快捷方式”:如何自动载入JavaScript? [英] Google Chrome "Application Shortcut": How to auto-load JavaScript?

查看:129
本文介绍了Google Chrome“应用程序快捷方式”:如何自动载入JavaScript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

b $ b Google chrome有一项功能,可让您创建网页快捷方式,并使它们看起来像传统的桌面应用程序。



例如,twitter mobile的捷径可能是

  C:\ Users \< username> ; \AppData\Local\Google\Chrome\Application\chrome.exe --app = https://mobile.twitter.com/ 
$ b $ 此应用程序的文件图标存储在

  C:\\ \\ Users \< username> \AppData\Local\Google\Chrome\User Data\Default\Web Applications\mobile.twitter.com\https_80 

我的问题

使用此功能已经有一段时间了,我似乎记住您可以将您自己的JavaScript文件添加到应用程序加载时包含的应用程序文件夹中。但是,我找不到任何讨论此功能的文档,但我99%确定它存在。



有没有人有任何细节,如果此功能可用,需要创建?



澄清

Chrome的应用程序快捷方式,我希望这个网页每x秒刷新一次。然而,我无法控制这个网页。



我确信在老版本的Chrome中这个曾经是可能的...除非我生气。 Chrome浏览器扩展程序及其内容脚本也会在Chrome以应用程序模式启动时加载。 / h3>

因此,您可以创建一个简单的扩展,它将在页面中注入JavaScript代码,如下所示:

1。创建一个 manifest.json 文件:
$ b

  {
name:在twitter mobile上运行代码,
version: 1.0,
manifest_version:2,
content_scripts:[{
js:[contentscript.js],
matches:[ http://mobile.twitter.com/*]
}],
web_accessible_resources:[script.js]
}



2。内容脚本



然后,创建一个名为 contentscript.js 的文件,并添加所需的JavaScript代码。 />
此脚本包含在匹配页面的每次加载中。通过文档对象的所有DOM方法都可以直接使用。但是, window document.defaultView do not 指向窗口页面中的对象 [source]



如果您想访问全局方法或属性,您必须动态创建< script> ,并将其注入页面中(请参阅构建Chrome扩展 - 使用内容脚本在页面中注入代码)。



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。将被注入的脚本



然后,创建一个名为 script.js 的文件,并将其放入与 manifest.json contentscript.js 相同的文件夹。 script.js 中的代码就好像它是受影响页面的真实部分一样执行。



内容脚本可用 此处


Introduction
Google chrome has a feature that allows you to create shortcuts to web pages and make them appear like traditional desktop applications.

For example, a shortcut to twitter mobile might be

C:\Users\<username>\AppData\Local\Google\Chrome\Application\chrome.exe  --app=https://mobile.twitter.com/

The file icon for this app is stored in

C:\Users\<username>\AppData\Local\Google\Chrome\User Data\Default\Web Applications\mobile.twitter.com\https_80

My Question
It's been a while since I've used this feature and I seem to remember that you could add your own JavaScript files to the app folder which were included when the application was loaded. However, I cannot find any documentation that discusses this feature but I'm 99% certain it exists.

Does anyone have any details if this feature is available and what files I need to create?

Clarification

I'm basically opening a webpage using Chrome's "Application Shortcut" and I want this webpage to refresh every x seconds. However, I do not have control over this webpage.

I'm sure in older versions of Chrome this used to be possible... unless I'm going mad.

解决方案

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": ["http://mobile.twitter.com/*"]
    }],
    "web_accessible_resources": ["script.js"]
}

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.

The reference for content scripts is available here.

这篇关于Google Chrome“应用程序快捷方式”:如何自动载入JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆