Tampermonkey 中的 Google 图表无法正常工作
感谢这个社区的所有帮助。我已经成功地通过TampermonKey在某些网页中添加了新的JS功能。现在,我正在尝试显示一个在网页上注入的Google图表。 Chrome浏览器。我一直在尝试几个月,没有快乐。我将所有内容剥离到了最简单的TM脚本,以在Google主页上显示饼图,URL匹配。 DIV是可见的,但没有图表。 JS控制台没有相关错误。
如果不使用TampermonKey,该图表会很好地使其很好。
https://jsfiddle.net/_pirateX/ue88hus0/
I have tried window.onload in various positions ,并在各个地方移动了Google Chart Loader。我尝试使用函数名称drawchart设置负载回调,但没有更改。
任何人都可以协助并告诉我为什么这不起作用。我将永远感激不已。
// @name Chart Example1
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description try to take over the world!
// @author You
// @require https://www.gstatic.com/charts/loader.js
// @match https://www.google.com
// ==/UserScript==
/* global google */
'use strict';
google.charts.load("current", {packages:["corechart"]});
window.onload = function() {
var myWrapper = document.createElement('div');
myWrapper.style.position = 'fixed';
myWrapper.id = "myChart";
myWrapper.style.width = '600px';
myWrapper.style.height = '300px';;
myWrapper.style.right = '1em';
myWrapper.style.top= '10em';
myWrapper.style.background = 'rgba(255, 255, 255, 1)';
myWrapper.style.border = '2px solid grey';
document.body.append(myWrapper);
//put here code
google.charts.setOnLoadCallback(function() { drawChart()});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Group');
data.addColumn('number', 'Gender');
data.addRows([
['Males', 10],
['Females', 5]
]);
var options = {'title':'Gender distribution',
'width':300,
'height':300};
var chart = new google.visualization.PieChart(document.getElementById('myChart'));
chart.draw(data, options);
}
}
Thanks to this community for all the great help. I have been successful adding new JS functionality to some webpages with Tampermonkey. Now I am trying to show a Google Chart injected on a webpage. Chrome browser. I have been trying on and off for several months, and no joy. I stripped everything down to the simplest TM script to show a pie chart, URL matching on Google home page. The DIV is visible but no chart is rendered. No related errors in JS console.
The chart renders fine if NOT using Tampermonkey.
https://jsfiddle.net/_pirateX/ue88hus0/
I have tried window.onload in various positions, and moved the Google chart loader in various places. I have tried to set the load callback just using the function name drawChart, but no change.
Can anyone please assist and tell me why this is not working. I would be eternally grateful.
// @name Chart Example1
// @namespace http://tampermonkey.net/
// @version 1.0.0
// @description try to take over the world!
// @author You
// @require https://www.gstatic.com/charts/loader.js
// @match https://www.google.com
// ==/UserScript==
/* global google */
'use strict';
google.charts.load("current", {packages:["corechart"]});
window.onload = function() {
var myWrapper = document.createElement('div');
myWrapper.style.position = 'fixed';
myWrapper.id = "myChart";
myWrapper.style.width = '600px';
myWrapper.style.height = '300px';;
myWrapper.style.right = '1em';
myWrapper.style.top= '10em';
myWrapper.style.background = 'rgba(255, 255, 255, 1)';
myWrapper.style.border = '2px solid grey';
document.body.append(myWrapper);
//put here code
google.charts.setOnLoadCallback(function() { drawChart()});
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Group');
data.addColumn('number', 'Gender');
data.addRows([
['Males', 10],
['Females', 5]
]);
var options = {'title':'Gender distribution',
'width':300,
'height':300};
var chart = new google.visualization.PieChart(document.getElementById('myChart'));
chart.draw(data, options);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
google的load语句默认会等待页面加载,
因此您可以使用 load 语句来代替
window.onload
尝试将代码放入 load 语句返回的承诺中...
google's load statement will wait for the page to load by default,
so you can use the load statement, in place of
window.onload
try placing the code inside the promise the load statement returns...
显然,TM加载/使用Google Loader.js的方式存在一些不相容性。在GitHub存储库中,我收到了TM开发人员的答复,如下所示:
@unwrap将包括在下一个版本中。同时,测试表明,通过JS加载Google Loader.js也有效:
随着我了解有关@UnWrap的更多信息,我将更新此答案。目前,我的图表正在渲染yay!谢谢大家。
Apparently there is some incompatibility in the way TM loads/uses Google loader.js. In Github repository, I got a reply from the developer of TM as follows:
@unwrap will be included in next release. In the meantime, testing revealed that loading Google loader.js via JS is also effective:
I will update this answer as I learn more about @unwrap. For now, my chart is rendering yay! Thanks everyone.