我正在写一个chrome扩展程序,它将为facebook中的特定文本框启用音译。
我已经使用脚本标签在background.html中加载https://www.google.com/jsapi
这是我尝试使用ajax和通用方式加载的内容脚本中使用的代码。
当我检查它说谷歌未定义。
/*
$.ajax({
url: "https://www.google.com/jsapi",
dataType: "script",
});
*/
var script = document.createElement("script");
script.setAttribute('type','text/javascript');
script.setAttribute('src','https://www.google.com/jsapi?'+(new Date()).getTime());
document.body.appendChild(script);
$(document).ready(function()
{
alert(google)
if(window.location.href.indexOf('facebook.com'))
yes_it_is_facebook();
})
function yes_it_is_facebook()
{
// document.getElementsByName('xhpc_message_text')[0].id = 'facebook_tamil_writer_textarea';
// alert(document.getElementsByName('xhpc_message').length)
google.load("elements", "1", { packages: "transliteration" });
google.setOnLoadCallback(onLoad);
}
function onLoad()
{
var options = {
sourceLanguage:
google.elements.transliteration.LanguageCode.ENGLISH,
destinationLanguage:
[google.elements.transliteration.LanguageCode.HINDI],
shortcutKey: 'ctrl+g',
transliterationEnabled: true
};
var control = new google.elements.transliteration.TransliterationControl(options);
control.makeTransliteratable(['facebook_tamil_writer_textarea']);
}
Run Code Online (Sandbox Code Playgroud)
我在manifest.json内容脚本数组中有https://www.google.com/jsapi。
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["js/jquery-1.7.2.min.js", "js/myscript.js", "https://www.google.com/jsapi"]
} …Run Code Online (Sandbox Code Playgroud)
我正在编写一个 Chrome 扩展程序,为用户访问的某些页面添加功能。
为此,我需要注入页面需要能够调用的一些变量和函数。
这些变量/函数是在内容脚本中生成的。
但是,由于内容脚本在隐蔽的环境中运行,因此主机页面无法访问它。
根据这篇文章:
http://code.google.com/chrome/extensions/content_scripts.html#host-page-communication
内容脚本和主机页面可以通过添加事件通过 DOM 进行通信。
但这是一种可怕的做事方式,我真的很想看到一些轻松注入方法/变量的方法。
有这样的可能吗?
谢谢!
javascript google-chrome google-chrome-extension content-script
好的,所以我想要的是覆盖选项卡中已经存在的方法,我将使用的是默认的警报功能.在JS函数中覆盖它会非常容易.加上
window.alert = function(){
//Do Something
}
Run Code Online (Sandbox Code Playgroud)
但问题是,当我尝试使用chrome.tabs.executeScript("window.alert = function() { };");它时不起作用.我试图通过在我想要覆盖该功能的选项卡中使用Chrome中的控制台手动执行此操作,我在日志中键入了覆盖功能并按下了输入,完成后,警报功能被覆盖,但我无法做到这是通过Chrome扩展程序.
当您添加executeScript时,它似乎创建了一个除了选项卡DOM内部的Javascript之外的Javascript,因为我可以创建具有选项卡DOM中已存在的函数名称的函数.
有没有办法让executeScript在选项卡DOM中编写脚本,所以它实际上可以覆盖页面生成的.js文件所写的任何函数?
谢谢!
我在我的网页html代码中有这个js函数.
function update() {
document.getElementById( "textbox ").value = updatetext;
}
Run Code Online (Sandbox Code Playgroud)
当我从chrome控制台执行"update()"时,它可以工作.
但是如果我从chrome扩展程序执行,
chrome.tabs.executeScript(tab.id, {code: "update();"}, function(result){});
Run Code Online (Sandbox Code Playgroud)
它说没有定义更新.但如果我用"alert('ok')替换",它就有效.
然后我执行
eval("update()")
Run Code Online (Sandbox Code Playgroud)
在Chrome扩展程序内容脚本中.它还说"未定义更新".
那么我该怎么做才能在网页上调用js函数呢?
我遇到了将内容脚本插入到由history.pushState和ajax调用更改的页面中的问题.我在stackoverflow上找到了类似的主题,但该解决方案对我不起作用(该解决方案是使用chrome.webNavigation.onHistoryStateUpdated和"popstate"事件).
这是我的清单的一个片段:
"content_scripts": [
{
"matches": ["https://vk.com/audios*", "https://vk.com/al_audio.php*"],
"js": ["jquery-2.1.4.min.js", "getListOfSongs.js"]
}
]
Run Code Online (Sandbox Code Playgroud)
chrome.webNavigation.onHistoryStateUpdated仅当我导航到另一个页面时才有效,如果我按顺序多次导航到同一页面没有任何反应.例如:它工作时
1)首次访问https://vk.com/audios* - 首页或重新加载
2)转到https://vk.com/some_other_page - ajax调用
3)转到https://vk.com/audios* - ajax电话
它不起作用
1)首次访问https://vk.com/audios* - 首页或重新加载
2)再次转到https://vk.com/audios* - ajax调用,此时内容脚本没有注入
3)再次转到https://vk.com/audios* - ajax调用,at这一点内容脚本不是注入等等
每次我第二次点击同一页面时依次生成以下请求:
https://vk.com/al_audio.php?__query=audios*********&_ ref = left_nav&_smt = audio%3A2&al = -1&al_id =********&_ rndVer = 60742
(请求参数可能有所不同)
此外 JQuery的.ajaxComplete不赶在这种情况下,任何事件.
并且pushState不会触发"popstate"事件,因此我无法使用window.onpopstate事件
我可能会使用chrome.webNavigation.onDOMContentLoaded和chrome.webNavigation.onCompleted,但是当我重新加载页面时,这些事件发生的时间超过一次,因此脚本将被多次注入.
这种情况的最佳解决方案是什么?
这是来自我的contentScript.js 的代码:
function loadScript(script_url)
{
var head= document.getElementsByTagName('head')[0];
var script= document.createElement('script');
script.type= 'text/javascript';
script.src= chrome.extension.getURL('mySuperScript.js');
head.appendChild(script);
someFunctionFromMySuperScript(request.widgetFrame);// ReferenceError: someFunctionFromMySuperScript is not defined
}
Run Code Online (Sandbox Code Playgroud)
但是从注入的脚本调用函数时出现错误:
ReferenceError: someFunctionFromMySuperScript 未定义
有没有办法在不修改mySuperScript.js 的情况下调用这个函数?
我正在尝试制作Chrome扩展程序,通过寻找关键字的网站进行解析,然后使用按钮替换这些关键字.但是,当我更改文本时,图像路径会损坏.
// This is a content script (isolated environment)
// It will have partial access to the chrome API
// TODO
// Consider adding a "run_at": "document_end" in the manifest...
// don't want to run before full load
// Might also be able to do this via the chrome API
console.log("Scraper Running");
var keywords = ["sword", "gold", "yellow", "blue", "green", "china", "civil", "state"];
// This will match the keywords with the page textx
// Will also create the necessary buttons …Run Code Online (Sandbox Code Playgroud) 我想通过 Google Chrome 扩展程序或简单的 JavaScript 在外部以编程方式编辑我的 Google Doc,并在 Google Doc 中实时(实时)查看更改。当这个问题出现时,我正在寻找可以编辑 Google 文档并以编程方式保存更改的 Chrome 扩展程序。在我的研究过程中,我遇到了Grammarly。他们如何近乎实时地将拼写更正应用到 Google Doc 中,这给我留下了深刻的印象。你可以像这样重现它:
然后 Grammarly 将更新 Google Doc。我注意到了什么:
[{"commands":[{"ty":"ds","si":229,"ei":232}, {"ty":"is","ibi":229,"s":"Test"}]}]
Run Code Online (Sandbox Code Playgroud)
我已经尝试了以下解决方案:
javascript google-api google-docs google-chrome-extension google-apps-script
我已经构建了 chrome 扩展,它可以从社交媒体页面中获取新闻源。但是,我想保留用户关注的特定社交媒体帐户的帖子,而不进行注入但进行过滤。问题是,有时(如果不是大多数时候)帖子可能不在预加载的提要中,并且所有社交媒体公司都使用延迟加载。因此,我真的在寻找创造性的解决方案来解决这个有趣的问题。
我尝试过的不成功的方法:
javascript ×9
ajax ×2
client ×1
dom ×1
google-api ×1
google-docs ×1
html ×1
jquery ×1
lazy-loading ×1
url ×1
vk ×1