Javascript 通过Greasemonkey实现Google自定义搜索API

Javascript 通过Greasemonkey实现Google自定义搜索API,javascript,greasemonkey,google-search,google-search-api,Javascript,Greasemonkey,Google Search,Google Search Api,我想用Greasemonkey实现Google自定义搜索API,但到目前为止,我的试验大多失败。该代码的目标是将自定义搜索框插入现有站点(我正试图为MATLAB的文档页面执行此操作,但插入的代码应能真正用于任何站点)。我尝试过网上搜索推荐的许多方法(主要是关于在Greasemonkey中实现JQuery或Google语言api),但没有一种方法适用于定制搜索api 我认为变量范围可能存在一些问题,但如果有人对如何使其工作有任何建议,请告诉我 // Inject the Google API lo

我想用Greasemonkey实现Google自定义搜索API,但到目前为止,我的试验大多失败。该代码的目标是将自定义搜索框插入现有站点(我正试图为MATLAB的文档页面执行此操作,但插入的代码应能真正用于任何站点)。我尝试过网上搜索推荐的许多方法(主要是关于在Greasemonkey中实现JQuery或Google语言api),但没有一种方法适用于定制搜索api

我认为变量范围可能存在一些问题,但如果有人对如何使其工作有任何建议,请告诉我

// Inject the Google API loader script
var script = document.createElement('script'); 
script.src = 'http://www.google.com/jsapi?callback=gLoaded';  // Call gLoaded() when google api loader is ready.
script.type = "text/javascript"; 
document.getElementsByTagName('head')[0].appendChild(script); 

// Create the <div> tag for the search API to draw the search box
var elmBody = document.getElementsByTagName('Body')[0];
var gSearch = document.createElement('div');
gSearch.id = 'g_search';
elmBody.appendChild(gSearch);

// Let w be the shorthand for unsafeWindow under the Greasemonkey sandbox
var w = unsafeWindow;

// Load the search api using the Google API loader
w.gLoaded= function()
{ w.google.load('search','1', {"callback" : searchLoaded}); } ; // Run searchLoaded() when search API is ready

// Setup the custom search API and draw the search box
searchLoaded = function(){ 
google = w.google; // unsafeWindow
alert(google);                                   // :debug_1
alert(google.search);                            // :debug_2
alert(google.search.CurrentLocale);              // :debug_3
var mySearch= new google.search.CustomSearchControl('012907575288767046387:tuvzi3yqdkq');
alert(mySearch)                                  // :debug_4
mySearch.setResultSetSize(google.search.Search.FILTERED_CSE_RESULTSET);
mySearch.draw('g_search');  // Draw the search box to the <div id='g_search'>
} 
//注入Google API加载程序脚本
var script=document.createElement('script');
script.src=http://www.google.com/jsapi?callback=gLoaded';  // 当google api加载程序准备就绪时调用gLoaded()。
script.type=“text/javascript”;
document.getElementsByTagName('head')[0].appendChild(脚本);
//为搜索API创建标记以绘制搜索框
var elmBody=document.getElementsByTagName('Body')[0];
var gSearch=document.createElement('div');
gSearch.id='g_search';
elmBody.appendChild(gSearch);
//让w作为Greasemonkey沙箱下未安全窗口的简写
var w=不安全窗口;
//使用谷歌api加载器加载搜索api
w、 gLoaded=函数()
{w.google.load('search','1',{“callback”:searchLoaded});};//搜索API就绪时运行searchLoaded()
//设置自定义搜索API并绘制搜索框
searchLoaded=函数(){
google=w.google;//取消安全窗口
警报(谷歌);/:debug_1
警报(google.search);/:debug_2
警报(google.search.CurrentLocale);/:debug_3
var mySearch=new google.search.CustomSearchControl('012907575288767046387:tuvzi3yqdkq');
警报(mySearch)/:debug_4
mySearch.setResultSetSize(google.search.search.FILTERED\u CSE\u RESULTSET);
mySearch.draw('g_search');//将搜索框绘制到
} 
  • debug_1:返回有效的对象
  • debug_2:返回一个有效的对象
  • debug_3:返回有效字符串('en')
  • debug_3:返回未定义的
  • 同样地,我尝试过让searchLoaded->w.searchLoaded并删除语句(google=w.google),但在这种情况下,所有调试返回未定义的
有趣的是,当我使用并通过命令行重新分配gLoaded()和searchLoaded()非Greasemonkey对应函数(没有unsafeWindow问题)时,一切都正常工作。一个可爱的搜索框显示在它应该在的地方

除了任何让它工作的建议,我想知道

  • 为什么google.search.CurrentLocale返回了有效字符串,而作为构造函数的google.search.CustomSearchControl()无法加载

  • 当我将searchLoaded指定为unsafeWindow.searchLoaded(参见上面的最后一个注释)时,google对象对函数不再可见,即使默认情况下它们应该在窗口范围内。然而,当我在javascript外壳下为函数分配了这些非常相同的值时,一切都正常了!Greasemonkey是否在某种程度上屏蔽了这些变量,即使我已经明确定义了要在窗口范围内的函数

  • 我尝试过不同方案的变体(location hack、@required、google.setOnLoadCallback…),但没有一个适合我

    请让我知道任何。。。我的意思是,任何建议,我都没有想法了

    谢谢

    基本上…

    var script = document.createElement('script'); 
    script.type = "text/javascript"; 
    script.innerHTML = (<><![CDATA[
    
    // YOUR CODE GOES HERE
    
    ]]></>).toString();
    document.getElementsByTagName('head')[0].appendChild(script);
    
    var script=document.createElement('script');
    script.type=“text/javascript”;
    script.innerHTML=().toString();
    document.getElementsByTagName('head')[0].appendChild(脚本);
    
    将代码编写为普通脚本,而不是GM脚本。
    我的意思是,删除所有
    unsafeWindow
    引用和相关内容。
    这将使脚本在正确的范围内运行。

    出现此问题的原因是
    google.search.CustomSearchControl
    使用了GM范围内未定义的变量J和K。

    谢谢!代码使用这个技巧非常有效。但是,我想问一下,在CDATA中包含和的原因是什么?我尝试了使用和不使用该集合,代码以任何方式工作。从技术上讲,CDATA应该始终位于标记内,这分别是我的根标记的开始和结束