0

So I'm using the '*' selector because the id and key appear in 'script', but the URL to be replaced appears in an 'a'. My Javascript knowledge is rather limited (though I can google), but I do have a more extensive history with regular expressions.

In any case, when I run this on the applicable page, parts of the page don't finish loading, so I was wondering what I'm doing wrong. I get the feeling there is more than one thing that could be improved here, but I don't know where to look to figure that out.

For what it's worth, the script does what I intend it to do, besides messing up other parts of the page.

$('*').html(function (i, text) {
    var idRegex = /"id":(\d{6,})/i;
    var keyRegex = /"access_key":"(key[-\w\d]*)"/i;
    var id = idRegex.exec(text);
    var key = keyRegex.exec(text);
    if (id !== null && key !== null && id !== undefined && key !== undefined)
        var new_link = "http://www.replacement-page.com/test?document_id=" + id[1] + "&access_key=" + key[1];
    if (new_link !== undefined)
        return text.replace(/https?:\/\/www\.example-page\.com\/replaced-url/gi, new_link);
});

Edit: Adding these two things to the code [seemed to] make it work

// @grant       GM_log
// ==/UserScript==
/*- The @grant directive is needed to work around a design change introduced in GM 1.0,
It restores the sandbox.
*/

var my_jquery = jQuery;
jQuery.noConflict(true);
var $ = my_jquery, jQuery = my_jquery;

Edit 2: In actuality, adding that broke the script. That did help me, however, come up with a way to solve it by making it two functions with a passed global variable. And more specifically, that they were not acting upon the other script on the page. The final script is as follows:

// ==UserScript==
// @name        Scribd Print and Download
// @description Changes "Upload" link to open a printable viewer (print to PDF)
// @author      Inserio
// @include     http://*.scribd.com/doc/*
// @include     https://*.scribd.com/doc/*
// @version     1.6
// @require     https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js
// ==/UserScript==

var new_link;

$('script').html(function (i, text) {
    var idRegex = /"id":(\d{6,})/i;
    var keyRegex = /"access_key":"(key[-\w\d]*)"/i;
    var id = idRegex.exec(text);
    var key = keyRegex.exec(text);
    if (id !== null && key !== null && id !== undefined && key !== undefined)
        new_link = "http://d1.scribdassets.com/ScribdViewer.swf?document_id=" + id[1] + "&access_key=" + key[1];
    // if (new_link !== undefined)
        // return text.replace(/https?:\/\/www\.scribd\.com\/upload-document/gi, new_link);
        // Matches the "Upload" link on the page.
        // Click it to open the new page in a viewer that will allow printing to PDF
});

$('div').html(function (i, text) {
    if (new_link !== undefined)
        return text.replace(/https?:\/\/www\.scribd\.com\/upload-document/gi, new_link);
});
Shenk
  • 352
  • 4
  • 12
  • Also, you need to provide enough of the script and the target page for us to duplicate the problem. Read [this answer](http://stackoverflow.com/a/31086018/) and provide ALL the required information. – Brock Adams Dec 08 '15 at 05:05
  • @BrockAdams Thank you. Reading your answer on [that](http://stackoverflow.com/questions/12146445/jquery-in-greasemonkey-1-0-conflicts-with-websites-using-jquery) page helped me understand the issue, and adding the lines by WackGet has made it work on pages that had the issue. – Shenk Dec 08 '15 at 06:35
  • Glad you got your script working. – Brock Adams Dec 08 '15 at 06:52

0 Answers0