如果您想了解javascript–Chrome扩展程序弹出窗口有2到3秒的延迟的相关知识,那么本文是一篇不可错过的文章,我们将对chrome扩展程序崩溃进行全面详尽的解释,并且为您提供关于Chrome
如果您想了解javascript – Chrome扩展程序弹出窗口有2到3秒的延迟的相关知识,那么本文是一篇不可错过的文章,我们将对chrome扩展程序崩溃进行全面详尽的解释,并且为您提供关于Chrome扩展程序JavaScript注入、Chrome扩展程序不会从弹出文件中加载我的JavaScript、JavaScript Chrome扩展程序消息传递:未发送响应、JavaScript click()方法仅在Chrome扩展程序中使用一次的有价值的信息。
本文目录一览:- javascript – Chrome扩展程序弹出窗口有2到3秒的延迟(chrome扩展程序崩溃)
- Chrome扩展程序JavaScript注入
- Chrome扩展程序不会从弹出文件中加载我的JavaScript
- JavaScript Chrome扩展程序消息传递:未发送响应
- JavaScript click()方法仅在Chrome扩展程序中使用一次
javascript – Chrome扩展程序弹出窗口有2到3秒的延迟(chrome扩展程序崩溃)
点击扩展程序的浏览器操作和弹出式渲染之间有2到3秒的延迟.我相信这是因为Chrome在显示弹出窗口之前等待很多JS运行之前.
奇怪的是,当我打开应用程序作为标签时,它立即加载(它不是一个特别沉重的JS应用程序!)它只显示弹出窗口的大量延迟.
如果没有从根本上改变我的扩展的架构,有没有办法我可以获得一些快速的胜利来提高弹出窗口的加载性能?我能推迟什么?
这是我的manifest.json文件:
"background": { "page": "index.html" },"browser_action": { "default_icon": { "19": "img/icon19.png","38": "img/icon38.png" },"default_title": "Super Simple Tasks","default_popup": "index.html?popup=true" }
该应用程序在$(document).ready中做了一些事情:在正文上设置一个类,将一些东西放入控制台,检查存储类型,并检查我们是否有互联网连接.
注意:如果您更喜欢在每次加载时运行的JavaScript,here is the compiled JS code.除了我在下面提到的内容之外,还有更多内容.
$(document).ready -> setPopupClass() standardLog() checkStorageMethod() checkOnline() $new_task_input = $('#new-task') $link_input = $('#add-link-input') initialize()
初始化然后设置应用程序:它获取任务数组并检查它是否为空,它向Google Analytics发送事件,在必要时从旧版本运行迁移,显示任务,并执行一些DOM操作.
initialize = -> window.storageType.get DB.db_key,(allTasks) -> if allTasks == null allTasks = Arrays.default_data window.storageType.set(DB.db_key,allTasks) ga 'send','hitType': 'event' 'eventCategory': 'Data' 'eventAction': 'Task count' 'eventValue': allTasks.length Migrations.run(allTasks) Views.showTasks(allTasks) $new_task_input.focus() setTimeout (-> $('#main-content').addClass('content-show') ),150
解决方法
checkOnline = -> online = navigator.onLine return online
解决方案是将此(以及依赖它的另一个方法)置于超时中,以便JS的其余部分可以运行并停止阻止弹出窗口显示:
$(document).ready -> setPopupClass() standardLog() checkStorageMethod() window.tourRunning = false document.onkeydown = keyboardShortcuts tour = createtour() # This is badwrong initialize() setTimeout (-> checkOnline() changeEmptyStateImage(online) ),100
Chrome扩展程序JavaScript注入
这里有几个问题。
内容脚本在DOMContentLoaded(by default)之后运行,但这绝不能保证页面之后不会动态运行其脚本。许多现代网页在基于requestAnimationFrame和setTimeout的回调构建UI或响应网络请求而获取新鲜数据时都这样做。
getElementsByClassName
返回实时集合:
-
console.log(aGrid[0])
证明脚本运行时为空 - 然后您的脚本完成
- 然后运行页面脚本并动态添加元素
- 然后您在devtools控制台中扩展了集合
- 然后devtools查找元素并找到它们at that exact moment
解决方案。
可能的解决方案是使用MutationObserver等待元素:
let aGrid = document.getElementsByClassName('fc-time-grid-event');
if (aGrid[0]) {
onGridAdded();
} else {
new MutationObserver((mutations,observer) => {
if (aGrid[0]) {
observer.disconnect();
onGridAdded();
}
}).observe(document,{childList: true,subtree: true});
}
function onGridAdded() {
console.log([...aGrid]);
// use aGrid here
}
更多示例:link。
Chrome扩展程序不会从弹出文件中加载我的JavaScript
我正在为论坛构建Chrome扩展程序,但问题是我的popup.html的JavaScript不会做任何事情.我在顶部添加了alert(“popup.js running …”)并且它确实出现但是我的弹出窗口根本没有显示.这是一个问题,因为弹出页面将需要JavaScript.我有点迷茫,所以我假设我只是遗漏了阻止我的JavaScript运行的东西.我听说AdBlock扩展程序会阻止它运行但我删除了它仍然无法正常工作.有人看到问题吗?
的manifest.json
{
"name": "Riggy",
"short_name": "Riggy",
"description": "Create your own Roblox Forum signature with Riggy!",
"version": "0.0.1",
"manifest_version": 2,
"browser_action": {
"default_popup": "popup/popup.html"
},
"permissions": [
"storage"
],
"content_scripts": [
{
"matches": ["http://www.roblox.com/*"],
"js": ["scripts/jquery.js", "scripts/content.js"]
}
]
}
popup.html
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="popup.css" />
<script type="text/javascript" src="scripts/jquery.js"></script>
</head>
<body>
<span>Riggy</span><br />
<span>Signature: </span><input name="siggy" id="siggy" value="Riggy is greatness!" />
<span id="output">[output]</span>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>
popup.js
alert("popup.js running");
$(document).on("ready", function() {
var siggy = $("#siggy");
var output = $("#output");
function message(text) {
output.html(text);
}
siggy.change(function() {
chrome.storage.sync.set({"siggy": siggy.val()}, function() {
message("Saved signature.");
});
});
message("Riggy is ready!");
});
解决方法:
我有一个与我的扩展相同的问题,我相信在我将它添加到清单文件后它已修复.
的manifest.json
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"
更多信息:http://developer.chrome.com/extensions/contentSecurityPolicy.html.
JavaScript Chrome扩展程序消息传递:未发送响应
我正在尝试在内容脚本和扩展名之间传递消息
这是我的内容脚本
chrome.runtime.sendMessage({type: "getUrls"}, function(response) { console.log(response)});
在后台脚本中
chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { if (request.type == "getUrls"){ getUrls(request, sender, sendResponse) }});function getUrls(request, sender, sendResponse){ var resp = sendResponse; $.ajax({ url: "http://localhost:3000/urls", method: ''GET'', success: function(d){ resp({urls: d}) } });}
现在,如果我在getUrls
函数中的ajax调用之前发送响应,则响应已成功发送,但是在ajax调用的成功方法中,当我发送响应时它不会发送响应,而在进行调试时,我可以看到该sendResponse
函数代码中的端口为null
。
答案1
小编典典从文档中chrome.runtime.onMessage.addListener
:
当事件侦听器返回时,此函数将变为无效,除非您从事件侦听器返回true表示您希望异步发送响应(这将使消息通道向另一端开放,直到调用sendResponse为止)。
因此,您只需要return true;
在调用后添加getUrls
即可指示将异步调用响应函数。
JavaScript click()方法仅在Chrome扩展程序中使用一次
问题是只有第一个.click()事件才会触发下载.后续的.click()事件将被忽略.
这里的manifest.json:
{ "name": "Simple File Downloader","version": "0.1","permissions": ["contextMenus","http://*/"],"background": { "persistent": false,"scripts": ["sample.js"] },"content_security_policy": "script-src 'self'; object-src 'self'","manifest_version": 2 }
这里是sample.js:
function onClickHandler(info,tab) { var a = document.createElement('a'); a.href = 'http://or.cdn.sstatic.net/chat/so.mp3'; a.download = 'so.mp3'; document.body.appendChild(a); a.click(); // this click triggers the download // this timeout violates content security policy // setTimeout(a,300); a.click(); // this click doesn't do anything document.body.removeChild(a); a = document.createElement('a'); a.href = 'http://or.cdn.sstatic.net/chat/so.mp3'; a.download = 'so.mp3'; document.body.appendChild(a); a.click(); // this click doesn't do anything either document.body.removeChild(a); }; chrome.contextMenus.onClicked.addListener(onClickHandler); chrome.runtime.onInstalled.addListener(function() { chrome.contextMenus.create({"title": "Download File","id":"download_file"}); });
我试过了:
>使用FileSaver.js在Chrome Extension write to file system中描述的下载文件的不同方法,结果完全相同,第一个文件被下载,第二个文件没有
>如Is it possible to make two .click method calls in javascript所述添加超时,导致内容安全策略违规,我试图使用Content-Security-Policy error in google chrome extension making中描述的方法解决,但没有成功
>使用JQuery click event works only once中描述的jQuery .live方法,也没有成功,但我并不是100%确定我正确地实现了这个(如果人们认为这种方法应该解决它,可以稍后发布代码)
很惊讶为什么简单地保存多个文件很难.感谢任何帮助.
解决方法
function clicker(el,clickCount) { var mousedownEvent; while(clickCount--) { mousedownEvent = document.createEvent("MouseEvent"); mousedownEvent.initMouseEvent("click",true,window,null,false,null); el.dispatchEvent(mousedownEvent); } } clicker(a,3); // your anchor 'a' gets clicked on 3 times.
但是,在Chrome中使用此方法时,您会收到来自浏览器的警告,询问“此网站正在尝试下载多个文件.您要允许此操作吗?[拒绝] [允许]”.因此,如果您在扩展程序的后台页面中执行此操作,则后台页面会收到警告,用户无法看到它,因此用户无法单击“允许”.
(粗略/讨厌)解决方法是创建一个“点击”锚点的选项卡.像这样的东西:
function _anchorDownloader(url,filename) { var timeout = 500; return 'javascript:\'<!doctype html><html>'+ '<head></head>' + '<script>' + 'function initDownload() {'+ 'var el = document.getElementById("anchor");'+ 'el.click();' + 'setTimeout(function() { window.close(); },' + timeout + ');' + '}'+ '</script>' + '<body onload="initDownload()">' + '<a id="anchor" href="' + url + '" download="'+ filename + '"></a>'+ '</body>' + '</html>\''; }; function downloadResource(info,tab) { // ... chrome.tabs.create( { 'url' : _anchorDownloader( url,filename ),'active' : false } ); // ... } chrome.contextMenus.create({"title": "Save Image…","contexts":["image"],"onclick": downloadResource });
为此,扩展必须将“tabs”作为manifest.json中的权限.您可以调整超时以关闭选项卡,但是,如果您关闭它太快,则不会发生下载.
关于javascript – Chrome扩展程序弹出窗口有2到3秒的延迟和chrome扩展程序崩溃的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于Chrome扩展程序JavaScript注入、Chrome扩展程序不会从弹出文件中加载我的JavaScript、JavaScript Chrome扩展程序消息传递:未发送响应、JavaScript click()方法仅在Chrome扩展程序中使用一次的相关信息,请在本站寻找。
本文标签: