2017-10-05 51 views
0

如何编写我的GreaseMonkey脚本让我访问另一个网站(www.sharelatex.com)上的授权/登录窗口?我的理解是GM_xmlhttpRequest将执行此功能,并且在GM_xmlhttpRequest成功加载后,当我获得“OK”状态时,不会出现登录窗口。我应该使用不同的功能吗?此登录必须以编程方式完成,以便userscript可以“捕获”成功登录后附加到重定向URL的令牌号码。这个令牌号码将在Mendeley API调用中用于从我的Mendeley帐户下载所需的文件(使用隐式授权流程)。如何使用GreaseMonkey脚本以编程方式打开登录窗口以进行隐式授权

背景信息:我正在尝试构建一个GreaseMonkey脚本,它将向我的www.sharelatex.com帐户添加一个按钮,当它被推送时,将自动从www.mendeley.com上的帐户下载一个文件, API。与按钮关联的代码还应该考虑使用API​​所需的登录和身份验证要求。我已经注册我与Mendeley应用,接收到“客户端ID”号(0000,出于说明的目的),其我已用于构建下面的网址:

var urlAuth = "https://api.mendeley.com/oauth/authorize?client_id=0000&redirect_uri=http://localhost&response_type=token&scope=all" 

如果我手动直接输入上述URL进我的浏览器作为URL地址,我被带到一个登录/授权页面,看起来像下面,这正是我想看到的,但以编程方式而不是手动: Click here to view authentication/login window 以下是我的故障GreaseMonkey userscript :

// ==UserScript== 
// ... skipping over irrelevant lines ... 
// @grant  GM_xmlhttpRequest 
// ==/UserScript== 
var urlAuth = "https://api.mendeley.com/oauth/authorize?client_id=0000&redirect_uri=http://localhost&response_type=token&scope=all" 
/* *************** CREATE BUTTON ************************* */ 
var input = document.createElement("input"); 
input.type = "button"; 
input.value="Update bibtex"; 
input.onclick = getBib; 
input.setAttribute("style", "font-size:18px; position:absolute; bottom:10px;left:10px;"); 
document.body.appendChild(input); 
/* ================================================================ */ 
function getBib() 
{  
GM_xmlhttpRequest({ 
method: 'GET', 
url: urlAuth, 
onload: function(reply) { alert(reply.statusText) } 
} 

预警指示es确定状态,但没有登录窗口出现。当我做的:

alert(urlAuth) 

在onload部分中,我手动复制/粘贴这似乎在警告框到浏览器的地址区域,浏览器带我到合适的登录/授权窗口,所以URL本身很好。

GM_xmlhttpRequest为什么不把我带到登录屏幕?我误解了GM_xmlhttpRequest的功能,而应该使用不同的功能?我已花了大约2个月的时间试图弄清楚这一点,通过数百篇关于OAuth2,userscripts,Mendeley API等主题的参考文献进行研究。几个例子:http://userscripts-mirror.org/scripts/review/292038(很有前途,因为它是唯一的GreaseMonkey/Mendeley用户输出那里但不幸的是不执行OAuth2的东西), https://salesforce.stackexchange.com/questions/76397/accessing-salesforce-rest-api-through-greasemonkey-script(提供的答案从来没有解决如何获得登录窗口的问题)。

+0

答案在于'window.open'和'postMessage' 。有关示例,请参阅https://meta.stackexchange.com/a/293498/148310。 –

+0

感谢您的回复,@BrockAdams。我几乎逐字地输入了中的示例代码,并且有许多后续问题(我将在新的单独的帖子中提问)。为了得到一个弹出窗口来登录,下面是我所做的(显式地让其他人也可以受益):'authWindow = window。打开( urlAuth,“登录”, “resizeable,scrollbars,status,toolbar,dependent,width = 660,height = 480”)其中urlAuth在上面定义。 – PMM

+0

@BrockAdams,如果你有机会,将非常感谢你看看我在这里发布的后续问题:https://stackoverflow.com/questions/46615950/my-redirect-uri-results-in-an-unable -to-连接警告。我认为这里的问题是令人满意的回答,这就是为什么我打开了一个新的问题/职位。 – PMM

回答

0

虽然OAuth2过程的主题涉及多个步骤,但这个问题主要集中在一个步骤上:如何通过GreaseMonkey用户脚本获得授权/登录窗口。答案(见上面的Brock Adams的评论)由meta.stackexchange.com/a/293498/148310中提供的示例给出。更具体地说,登录窗口由window.open函数生成,如以下示例所示(请参见下面的行#21):

// ==UserScript== 
<other needed info in the metadata block should be included here> 
// @match  https://stackexchange.com/oauth/login_success* 
// ==/UserScript== 

var rootUrl = "https://api.mendeley.com/oauth/authorize?" 
// the rootUrl should point to whatever API service you are trying to use. 
// I am using Mendeley, but could be i.e, facebook, YouTube, Twitter, Google, etc. 
var clientId = "0000" // needs to be the number you got when you registered your app 
// through whatever API service you want to use 
var redirectUrl = "https://stackexchange.com/oauth/login_success" 
// The above directs where the login page will be redirected after successful 
//  log-in/authorization. 
// This URL needs to point to a real page. 
// Also make sure that whatever is given for the redirectUrl is also 
//  listed in the @match statement in the metadata block section at the top, 
//  but with an astericks at the end (to allow for the token number to be 
//  attached as a hash fragment, as part of the OAuth2 process) 
var other = "response_type=token&scope=all" 
var urlAuth = rootUrl + clientId + "&" + redirectUrl + "&" + other 
authWindow = window.open (urlAuth, "Log in", "resizeable, scrollbars, status, toolbar, 
      dependent, width=660,height=480") 
// tailor window sizing, etc. to your own aesthetics 
相关问题