MediaWiki:Copyvio Search Chromium Extension

From WikiSkripta

Copyvio Search je extenze pro Google Chrome usnadňující vyhledávání copyvií.

Instalujte zde

Návod

Pro použití klikněte na tlačítko s logem WikiSkript vedle adresního řádku. Otevře se nové okno se záložkami, v každé je vyhledaný určitý úsek článku pomocí Googlu. Pro zavírání jednotlivých záložek je výhodné použít klávesovou zkratku CTRL+W.

Zdrojový kód

manifest.json

{
        "name": "WikiSkripta Copyvio Search Extension",
        "short_name": "WSPlagiCheck",
        "description": "Extension that simplifies the search of text sources used on the project WikiSkripta.eu",
        "version": "0.0.0.2",
        "manifest_version": 2,
        "icons": {
                "16": "icon16.png",
                "48": "icon48.png",
                "128": "icon128.png"
        },
        "browser_action": {
                "default_icon": "icon16.png"
        },
        "permissions" : [
                "tabs",
                "https://www.wikiskripta.eu/*"
        ],
        "content_scripts": [ {
                "matches": [ "https://www.wikiskripta.eu/*" ],
                "js": ["contentscript.js"]
        } ],
        "background": {
                "scripts": ["eventPage.js"]
        }
}

contentscript.js

// wait for the request from eventPage.js
chrome.extension.onRequest.addListener(function(request, sender, callback) {
    if (request.action == 'getSource') {
        callback(document.getElementById('bodyContent').innerHTML); // return the text of the article
    }
});


eventPage.js

/*************************
 * configuration
 * ***********************/

// maximal count of words per each search (Google's max. limit = 32 words/search)
var maxSearchedWords = 10;




// run script if clicked on browser action
chrome.browserAction.onClicked.addListener(function () {
  getSourceHTML();
});

function getSourceHTML () {
  chrome.tabs.getSelected(null, function(tab) { // get tab id
    // send request to contentscript.js which delivers the HTML source code
    chrome.tabs.sendRequest(tab.id, {action : 'getSource'}, function(source) {
      run(source);
    });
  });
}

function run(sourceHTML) {
  var text = sourceHTML;

  // get just plain text:
  text = text.replace(/<head[\s\S]*?<\/head>/gmi, ''); // get rid of the page's head
  text = text.replace(/<script[\s\S]*?<\/script>/gmi, ''); // get rid of javascript
  text = text.replace(/<table[\s\S]*?<\/table>/gmi, ''); // get rid of tables
  text = text.replace(/<span[\s\S]*?<\/span>/gmi, ''); // get rid of headlines and metadata
  text = text.replace(/<div[\s\S]*?<\/div>/gmi, ''); // get rid of metadata and images
  text = text.replace(/<(?:.|\n)*?>/gm, ''); // get rid of html tags
  text = text.replace(/\t/g, ''); // replace tabs and multiple whitespaces with a single whitespace

  // split into lines
  var lines = text.split("\n");
  var line = "";
  text = "";
  urls = new Array();

  for (i in lines) {
    line = lines[i];
    // not an empty string and longer than 30 characters:
    if (!line.match(/^\s*$/) && line.length > 30) {

      // trim lines to a given max. length of words to keep search sensitive enough
      lineSplit = line.split(" ");
      if (lineSplit.length > maxSearchedWords) {
        // generate random beginning of the search inside a line
        var upperRange = lineSplit.length-maxSearchedWords;
        var randBegin = Math.floor(Math.random() * (upperRange + 1));
        var end = maxSearchedWords+randBegin;

        lineSplit = lineSplit.slice(randBegin, end);
      }
      line = lineSplit.join(" ");

      // fill the url array with search requests for Google
      url = "\"" + line + "\" -site:www.wikiskripta.eu -wikiskripta";
      urls.push("http://www.google.com/#pbx=1&q=" + encodeURIComponent(url));
      text += lines[i] + "<br />";
    }
  }
  createWindow(urls);
}

function createWindow(urls) {
  // open a new window with tabs for each google result
  chrome.windows.create({"url": urls});
}