Skip to content

Commit

Permalink
1.6 - Egy utolsó borzalmas hack az újraírás előtt :S
Browse files Browse the repository at this point in the history
- Chrome CORB javítás
- a lábjegyzeteknél nincs exception
- WordPress beállításoknál a számok már számok maradnak
  • Loading branch information
molnarm committed Apr 25, 2020
1 parent dbcc273 commit a107edd
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 60 deletions.
18 changes: 12 additions & 6 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ var concat = require('gulp-concat');
const del = require("del");
const zip = require("gulp-zip");

const debug = require("gulp-debug");

// PATHS

const sources = "src/";
Expand All @@ -17,6 +15,7 @@ const packages = "packages/";
const allFiles = "**/*";
const notJsFiles = "**/!(*.js)"

const apiJsFile = sources + "api.js";
const mainJsFile = "igemutato.js";
const minMainJsFile = "igemutato.min.js";
const cssFile = "igemutato.css";
Expand All @@ -25,6 +24,7 @@ const startJsFile = "start.js";

const browserDir = "webextension/";
const browserZipFile = "extension.zip";
const backgroundJs = 'background.js';

const webDir = "web/";

Expand All @@ -51,8 +51,14 @@ gulp.task("clean-browser", function () {
return del(output + browserDir);
});

gulp.task("copy-src-browser", function () {
return gulp.src([sources + browserDir + allFiles, sources + mainJsFile])
gulp.task("transform-js-browser", function (){
return gulp.src([apiJsFile, sources + browserDir + backgroundJs])
.pipe(concat(backgroundJs))
.pipe(gulp.dest(output + browserDir))
});

gulp.task("copy-src-browser", ["transform-js-browser"], function () {
return gulp.src([sources + browserDir + '**/!(' + backgroundJs + ')', sources + mainJsFile])
.pipe(gulp.dest(output + browserDir));
});

Expand All @@ -73,7 +79,7 @@ gulp.task("clean-web", function () {
});

gulp.task("transform-js-web", function () {
return gulp.src([sources + mainJsFile, sources + webDir + startJsFile])
return gulp.src([ apiJsFile, sources + mainJsFile, sources + webDir + startJsFile])
.pipe(concat(mainJsFile))
.pipe(gulp.dest(output + webDir));
});
Expand All @@ -99,7 +105,7 @@ gulp.task("copy-src-wordpress", function () {
});

gulp.task("transform-js-wordpress", ["copy-src-wordpress"], function () {
return gulp.src([sources + mainJsFile, sources + wordPressDir + startJsFile])
return gulp.src([apiJsFile, sources + mainJsFile, sources + wordPressDir + startJsFile])
.pipe(concat(mainJsFile))
.pipe(gulp.dest(output + wordPressDir));
});
Expand Down
50 changes: 50 additions & 0 deletions src/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
var Szentiras_API = function(){
// lekérdezések kellékei
var xmlhttp;

// https://www.html5rocks.com/en/tutorials/cors/
function createCORSRequest(method, target) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, target, true);
}
else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, target);
}
else {
xhr = null;
}
return xhr;
}

function request(api, ige, forditas, callback){
xmlhttp && xmlhttp.abort();

var src = api + ige + '/' + forditas,
success = function () {
try {
if (xmlhttp.readyState === 4) {
if( xmlhttp.status === 200) {
callback(JSON.parse(xmlhttp.responseText));
return;
}
else{
callback(null);
}
}
}
catch (ex) {
console && console.log && console.log(ex.message);
}
};

xmlhttp = createCORSRequest('GET', src);
xmlhttp.onreadystatechange = success;
xmlhttp.send();
}

return {
request: request
};
}();
66 changes: 22 additions & 44 deletions src/igemutato.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var Szentiras = (function () {
// timeoutok
linkTimeout, tipTimeout,
// lekérdezések kellékei
xmlhttp, cache = {},
callApi, cache = {},
// aktuális adatok
forditas, ige,
// DOM elemek
Expand Down Expand Up @@ -112,53 +112,21 @@ var Szentiras = (function () {
return a;
}

// https://www.html5rocks.com/en/tutorials/cors/
function createCORSRequest(method, target) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
xhr.open(method, target, true);
}
else if (typeof XDomainRequest != "undefined") {
xhr = new XDomainRequest();
xhr.open(method, target);
}
else {
xhr = null;
}
return xhr;
}

// Betölti a hivatkozott szöveget
function fetch() {
xmlhttp && xmlhttp.abort();

if (cache[forditas] && cache[forditas][ige]) {
addContent(cache[forditas][ige]);
szoveg.scrollTop = 0;
show(cache[forditas][ige]);
return;
}

var src = api + ige + '/' + forditas,
success = function () {
try {
if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
show(JSON.parse(xmlhttp.responseText));
return;
}
}
catch (ex) {
console && console.log && console.log(ex.message);
}
if (xmlhttp.readyState !== 0) {
szoveg.textContent = 'A betöltés sikertelen :-(';
}
};

xmlhttp = createCORSRequest('GET', src);

xmlhttp.onreadystatechange = success;

xmlhttp.send();
callApi(api, ige, forditas, function(result) {
if(!result){
szoveg.textContent = 'A betöltés sikertelen :-(';
return;
}

show(result);
});
}

// Feldolgozza a JSON választ
Expand All @@ -176,7 +144,7 @@ var Szentiras = (function () {
var versek = json.valasz.versek;
addContent(versek);
cache[forditas] || (cache[forditas] = {});
cache[forditas][ige] = versek;
cache[forditas][ige] = json;
szoveg.scrollTop = 0;
return;
}
Expand All @@ -200,6 +168,9 @@ var Szentiras = (function () {
szoveg.removeChild(szoveg.firstChild);
}
for (i = 0; i < versek.length; i++) {
if(!versek[i].szoveg)
continue;

vers = versek[i].szoveg.trim();
if (config.showNumbers) {
szamok = versszam(versek[i]);
Expand All @@ -221,6 +192,8 @@ var Szentiras = (function () {
szoveg.appendChild(d.createTextNode((vers.replace(/<[^>]+>/g, ' ') + ' ').replace(/\s+/g, ' ')));
}
}

szoveg.scrollTop = 0;
}

function addElements(root, nodes) {
Expand Down Expand Up @@ -362,8 +335,13 @@ var Szentiras = (function () {
keres(element);
}

function patchApi(makeRequest) {
callApi = makeRequest;
}

return {
setConfig: setConfig,
start: start
start: start,
patchApi: patchApi
};
})();
1 change: 0 additions & 1 deletion src/web/.gitignore

This file was deleted.

1 change: 1 addition & 0 deletions src/web/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ css.setAttribute("href", 'https://molnarm.github.io/igemutato.min.css');
document.getElementsByTagName("head")[0].appendChild(css);

window.igemutato && window.igemutato.config && Szentiras.setConfig(window.igemutato.config);
Szentiras.patchApi(Szentiras_API.request)
Szentiras.start(document.body);
7 changes: 7 additions & 0 deletions src/webextension/background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.contentScriptQuery == "apiRequest") {
Szentiras_API.request(request.api, request.ige, request.forditas, sendResponse);
return true;
}
});
6 changes: 6 additions & 0 deletions src/webextension/content.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function request(api, ige, forditas, callback){
chrome.runtime.sendMessage({contentScriptQuery: "apiRequest", api: api, ige: ige, forditas: forditas}, callback);
}


chrome.storage.sync.get('config', function(result) {
if(result && result.config)
Szentiras.setConfig(result.config);
Expand All @@ -11,5 +16,6 @@ chrome.storage.sync.get('config', function(result) {
css.setAttribute("href", chrome.extension.getURL('igemutato.css'));
document.getElementsByTagName("head")[0].appendChild(css);

Szentiras.patchApi(request);
Szentiras.start(document.body);
});
5 changes: 4 additions & 1 deletion src/webextension/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Szentírás",
"description": "Bibliai hivatkozások beillesztése a Szentírás.hu oldalról",
"version": "1.5",
"version": "1.6",
"icons": {
"128": "icon.png"
},
Expand All @@ -21,6 +21,9 @@
"all_frames": true
}
],
"background": {
"scripts": [ "background.js" ]
},
"web_accessible_resources": [
"igemutato.css"
]
Expand Down
12 changes: 6 additions & 6 deletions src/wordpress/igemutato.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/*
Plugin Name: Igemutató
Description: Az oldal tartalmában található szentírási hivatkozásokat jeleníti meg felugró szövegbuborékban.
Version: 1.5
Version: 1.6
Author: Molnár Márton
License: GPL
*/
Expand Down Expand Up @@ -138,11 +138,11 @@ public function admin_options() {
public function validate_options($options) {
$defaults = Igemutato::get_default_settings();

$options['tipW'] = (!is_numeric($options['tipW']) || $options['tipW'] < 100) ? $defaults['tipW'] : $options['tipW'];
$options['tipH'] = (!is_numeric($options['tipH']) || $options['tipH'] < 50) ? $defaults['tipH'] : $options['tipH'];
$options['fontSize'] = (!is_numeric($options['fontSize']) || $options['fontSize'] < 5) ? $defaults['fontSize'] : $options['fontSize'];
$options['tipShow'] = (!is_numeric($options['tipShow']) || $options['tipShow'] < 0) ? $defaults['tipShow'] : $options['tipShow'];
$options['tipHide'] = (!is_numeric($options['tipHide']) || $options['tipHide'] < 0) ? $defaults['tipHide'] : $options['tipHide'];
$options['tipW'] = (!is_int($options['tipW']) || $options['tipW'] < 100) ? $defaults['tipW'] : intval($options['tipW']);
$options['tipH'] = (!is_int($options['tipH']) || $options['tipH'] < 50) ? $defaults['tipH'] : intval($options['tipH']);
$options['fontSize'] = (!is_int($options['fontSize']) || $options['fontSize'] < 5) ? $defaults['fontSize'] : intval($options['fontSize']);
$options['tipShow'] = (!is_int($options['tipShow']) || $options['tipShow'] < 0) ? $defaults['tipShow'] : intval($options['tipShow']);
$options['tipHide'] = (!is_int($options['tipHide']) || $options['tipHide'] < 0) ? $defaults['tipHide'] : intval($options['tipHide']);
$options['forditas'] = (!in_array($options['forditas'], array('SZIT', 'KNB', 'KG', 'UF', 'RUF', 'BD', 'STL'))) ? $defaults['forditas'] : $options['forditas'];
$options['excludeTags'] = isset($options['excludeTags']) ? $options['excludeTags'] : $defaults['excludeTags'];
$options['enableFormatting'] = !empty($options['enableFormatting']);
Expand Down
4 changes: 2 additions & 2 deletions src/wordpress/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Contributors: molnarm
Tags: bible, biblia, szentírás, tooltip
Plugin URI: http://molnarm.github.io/igemutato/
Requires at least: 2.7
Tested up to: 4.9
Stable tag: 1.5
Tested up to: 5.4
Stable tag: 1.6
License: GPLv2
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down
1 change: 1 addition & 0 deletions src/wordpress/start.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
window.igemutato && window.igemutato.config && Szentiras.setConfig(window.igemutato.config);
Szentiras.patchApi(Szentiras_API.request)
Szentiras.start(document.body);

0 comments on commit a107edd

Please sign in to comment.